1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109

ash / birch / test_birch_client.h [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_BIRCH_TEST_BIRCH_CLIENT_H_
#define ASH_BIRCH_TEST_BIRCH_CLIENT_H_

#include <memory>
#include <vector>

#include "ash/birch/birch_client.h"
#include "ash/birch/birch_data_provider.h"
#include "ash/birch/birch_item.h"
#include "base/files/scoped_temp_dir.h"
#include "ui/base/models/image_model.h"

namespace ash {
class BirchModel;

// A test birch data provider that runs the data fetched callback with saved
// items when receives a data fetch request.
template <typename T>
class TestBirchDataProvider : public BirchDataProvider {
 public:
  using DataFetchedCallback =
      base::RepeatingCallback<void(const std::vector<T>&)>;

  TestBirchDataProvider(DataFetchedCallback data_fetched_callback,
                        const std::string& pref_name);
  TestBirchDataProvider& operator=(const TestBirchDataProvider&) = delete;
  ~TestBirchDataProvider() override;

  void set_items(const std::vector<T>& items) { items_ = items; }

  void ClearItems();

  // Trigger data provider changed callback.
  void RunDataProviderChangedCallback();

  // BirchDataProvider:
  void RequestBirchDataFetch() override;

 private:
  DataFetchedCallback data_fetched_callback_;
  const std::string pref_name_;
  std::vector<T> items_;
};

// A test birch client that returns the specific items to birch model.
class TestBirchClient : public BirchClient {
 public:
  explicit TestBirchClient(BirchModel* birch_model);
  TestBirchClient(const TestBirchClient&) = delete;
  TestBirchClient& operator=(const TestBirchClient&) = delete;
  ~TestBirchClient() override;

  void SetCalendarItems(const std::vector<BirchCalendarItem>& items);
  void SetFileSuggestItems(const std::vector<BirchFileItem>& items);
  void SetRecentTabsItems(const std::vector<BirchTabItem>& items);
  void SetLastActiveItems(const std::vector<BirchLastActiveItem>& items);
  void SetMostVisitedItems(const std::vector<BirchMostVisitedItem>& items);
  void SetReleaseNotesItems(const std::vector<BirchReleaseNotesItem>& items);
  void SetSelfShareItems(const std::vector<BirchSelfShareItem>& items);
  void SetLostMediaItems(const std::vector<BirchLostMediaItem>& items);

  // Clear all items.
  void Reset();

  // BirchClient:
  BirchDataProvider* GetCalendarProvider() override;
  BirchDataProvider* GetFileSuggestProvider() override;
  BirchDataProvider* GetRecentTabsProvider() override;
  BirchDataProvider* GetLastActiveProvider() override;
  BirchDataProvider* GetMostVisitedProvider() override;
  BirchDataProvider* GetSelfShareProvider() override;
  BirchDataProvider* GetLostMediaProvider() override;
  BirchDataProvider* GetReleaseNotesProvider() override;
  void WaitForRefreshTokens(base::OnceClosure callback) override;
  base::FilePath GetRemovedItemsFilePath() override;
  void RemoveFileItemFromLauncher(const base::FilePath& path) override {}
  void GetFaviconImage(
      const GURL& url,
      const bool is_page_url,
      base::OnceCallback<void(const ui::ImageModel&)> callback) override {}
  ui::ImageModel GetChromeBackupIcon() override;

 private:
  void HandleCalendarFetch(const std::vector<BirchCalendarItem>& items);

  std::unique_ptr<TestBirchDataProvider<BirchCalendarItem>> calendar_provider_;
  std::unique_ptr<TestBirchDataProvider<BirchFileItem>> file_provider_;
  std::unique_ptr<TestBirchDataProvider<BirchTabItem>> tab_provider_;
  std::unique_ptr<TestBirchDataProvider<BirchLastActiveItem>>
      last_active_provider_;
  std::unique_ptr<TestBirchDataProvider<BirchMostVisitedItem>>
      most_visited_provider_;
  std::unique_ptr<TestBirchDataProvider<BirchSelfShareItem>>
      self_share_provider_;
  std::unique_ptr<TestBirchDataProvider<BirchLostMediaItem>>
      lost_media_provider_;
  std::unique_ptr<TestBirchDataProvider<BirchReleaseNotesItem>>
      release_notes_provider_;

  base::ScopedTempDir test_dir_;
};

}  // namespace ash

#endif  // ASH_BIRCH_TEST_BIRCH_CLIENT_H_