init_aware_tile_service_unittest.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/query_tiles/internal/init_aware_tile_service.h"
  5. #include <memory>
  6. #include "base/callback_helpers.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/test/task_environment.h"
  9. #include "components/query_tiles/internal/tile_service_impl.h"
  10. #include "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. using testing::_;
  14. using testing::InSequence;
  15. using testing::Invoke;
  16. using testing::StrictMock;
  17. namespace query_tiles {
  18. namespace {
  19. class MockInitializableTileService : public InitializableTileService {
  20. public:
  21. MockInitializableTileService() = default;
  22. ~MockInitializableTileService() override = default;
  23. void Initialize(SuccessCallback callback) override {
  24. init_callback_ = std::move(callback);
  25. }
  26. void InvokeInitCallback(bool success) {
  27. DCHECK(init_callback_);
  28. std::move(init_callback_).Run(success);
  29. }
  30. // InitializableTileService implementation.
  31. MOCK_METHOD(void, GetQueryTiles, (GetTilesCallback), (override));
  32. MOCK_METHOD(void, GetTile, (const std::string&, TileCallback), (override));
  33. MOCK_METHOD(void,
  34. StartFetchForTiles,
  35. (bool, BackgroundTaskFinishedCallback),
  36. (override));
  37. MOCK_METHOD(void, CancelTask, (), (override));
  38. MOCK_METHOD(void, PurgeDb, (), (override));
  39. MOCK_METHOD(Logger*, GetLogger, (), (override));
  40. MOCK_METHOD(void, SetServerUrl, (const std::string&), (override));
  41. MOCK_METHOD(void, OnTileClicked, (const std::string&), (override));
  42. MOCK_METHOD(void,
  43. OnQuerySelected,
  44. (const absl::optional<std::string>&, const std::u16string&),
  45. (override));
  46. // Callback stubs.
  47. MOCK_METHOD(void, GetTilesCallbackStub, (TileList), ());
  48. MOCK_METHOD(void, TileCallbackStub, (absl::optional<Tile>), ());
  49. MOCK_METHOD(void, BackgroundTaskFinishedCallbackStub, (bool), ());
  50. private:
  51. SuccessCallback init_callback_;
  52. };
  53. class InitAwareTileServiceTest : public testing::Test {
  54. public:
  55. InitAwareTileServiceTest() : mock_service_(nullptr) {}
  56. ~InitAwareTileServiceTest() override = default;
  57. protected:
  58. void SetUp() override {
  59. auto mock_service =
  60. std::make_unique<StrictMock<MockInitializableTileService>>();
  61. mock_service_ = mock_service.get();
  62. init_aware_service_ =
  63. std::make_unique<InitAwareTileService>(std::move(mock_service));
  64. ON_CALL(*mock_service_, GetQueryTiles(_))
  65. .WillByDefault(Invoke([](GetTilesCallback callback) {
  66. std::move(callback).Run({Tile()});
  67. }));
  68. ON_CALL(*mock_service_, GetTile(_, _))
  69. .WillByDefault(Invoke([](const std::string&, TileCallback callback) {
  70. std::move(callback).Run(Tile());
  71. }));
  72. ON_CALL(*mock_service_, StartFetchForTiles(_, _))
  73. .WillByDefault(
  74. Invoke([](bool, BackgroundTaskFinishedCallback callback) {
  75. std::move(callback).Run(true);
  76. }));
  77. }
  78. protected:
  79. TileService* init_aware_service() {
  80. DCHECK(init_aware_service_);
  81. return init_aware_service_.get();
  82. }
  83. MockInitializableTileService* mock_service() {
  84. DCHECK(mock_service_);
  85. return mock_service_;
  86. }
  87. void InvokeInitCallback(bool success) {
  88. mock_service_->InvokeInitCallback(success);
  89. }
  90. void GetQueryTiles() {
  91. auto callback =
  92. base::BindOnce(&MockInitializableTileService::GetTilesCallbackStub,
  93. base::Unretained(mock_service_));
  94. init_aware_service()->GetQueryTiles(std::move(callback));
  95. }
  96. void GetTile() {
  97. auto callback =
  98. base::BindOnce(&MockInitializableTileService::TileCallbackStub,
  99. base::Unretained(mock_service_));
  100. init_aware_service()->GetTile("id", std::move(callback));
  101. }
  102. void StartFetchForTiles() {
  103. auto callback = base::BindOnce(
  104. &MockInitializableTileService::BackgroundTaskFinishedCallbackStub,
  105. base::Unretained(mock_service_));
  106. init_aware_service()->StartFetchForTiles(false /*is_from_reduced_mode*/,
  107. std::move(callback));
  108. }
  109. void RunUntilIdle() { task_environment_.RunUntilIdle(); }
  110. private:
  111. base::test::TaskEnvironment task_environment_;
  112. raw_ptr<MockInitializableTileService> mock_service_;
  113. std::unique_ptr<InitAwareTileService> init_aware_service_;
  114. };
  115. // API calls invoked after successful initialization should just pass through.
  116. TEST_F(InitAwareTileServiceTest, AfterInitSuccessPassThrough) {
  117. InvokeInitCallback(true /*success*/);
  118. {
  119. InSequence sequence;
  120. EXPECT_CALL(*mock_service(), GetQueryTiles(_));
  121. EXPECT_CALL(*mock_service(), GetTile(_, _));
  122. EXPECT_CALL(*mock_service(), StartFetchForTiles(false, _));
  123. }
  124. EXPECT_CALL(*mock_service(), GetTilesCallbackStub(TileList({Tile()})));
  125. EXPECT_CALL(*mock_service(), TileCallbackStub(absl::make_optional<Tile>()));
  126. EXPECT_CALL(*mock_service(), BackgroundTaskFinishedCallbackStub(true));
  127. GetQueryTiles();
  128. GetTile();
  129. StartFetchForTiles();
  130. RunUntilIdle();
  131. }
  132. // API calls invoked after failed initialization should not pass through.
  133. TEST_F(InitAwareTileServiceTest, AfterInitFailureNotPassThrough) {
  134. InvokeInitCallback(false /*success*/);
  135. {
  136. InSequence sequence;
  137. EXPECT_CALL(*mock_service(), GetQueryTiles(_)).Times(0);
  138. EXPECT_CALL(*mock_service(), GetTile(_, _)).Times(0);
  139. EXPECT_CALL(*mock_service(), StartFetchForTiles(_, _)).Times(0);
  140. }
  141. EXPECT_CALL(*mock_service(), GetTilesCallbackStub(TileList()));
  142. EXPECT_CALL(*mock_service(), TileCallbackStub(absl::optional<Tile>()));
  143. EXPECT_CALL(*mock_service(), BackgroundTaskFinishedCallbackStub(false));
  144. GetQueryTiles();
  145. GetTile();
  146. StartFetchForTiles();
  147. RunUntilIdle();
  148. }
  149. // API calls invoked before successful initialization should be flushed through.
  150. TEST_F(InitAwareTileServiceTest, BeforeInitSuccessFlushedThrough) {
  151. {
  152. InSequence sequence;
  153. EXPECT_CALL(*mock_service(), GetQueryTiles(_));
  154. EXPECT_CALL(*mock_service(), GetTile(_, _));
  155. EXPECT_CALL(*mock_service(), StartFetchForTiles(false, _));
  156. }
  157. EXPECT_CALL(*mock_service(), GetTilesCallbackStub(TileList({Tile()})));
  158. EXPECT_CALL(*mock_service(), TileCallbackStub(absl::make_optional<Tile>()));
  159. EXPECT_CALL(*mock_service(), BackgroundTaskFinishedCallbackStub(true));
  160. GetQueryTiles();
  161. GetTile();
  162. StartFetchForTiles();
  163. InvokeInitCallback(true /*success*/);
  164. RunUntilIdle();
  165. }
  166. // API calls invoked before failed initialization should not be flushed through.
  167. TEST_F(InitAwareTileServiceTest, BeforeInitFailureNotFlushedThrough) {
  168. {
  169. InSequence sequence;
  170. EXPECT_CALL(*mock_service(), GetQueryTiles(_)).Times(0);
  171. EXPECT_CALL(*mock_service(), GetTile(_, _)).Times(0);
  172. EXPECT_CALL(*mock_service(), StartFetchForTiles(_, _)).Times(0);
  173. }
  174. EXPECT_CALL(*mock_service(), GetTilesCallbackStub(TileList()));
  175. EXPECT_CALL(*mock_service(), TileCallbackStub(absl::optional<Tile>()));
  176. EXPECT_CALL(*mock_service(), BackgroundTaskFinishedCallbackStub(false));
  177. GetQueryTiles();
  178. GetTile();
  179. StartFetchForTiles();
  180. InvokeInitCallback(false /*success*/);
  181. RunUntilIdle();
  182. }
  183. } // namespace
  184. } // namespace query_tiles