| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/search/one_google_bar/one_google_bar_service.h" | 5 #include "chrome/browser/search/one_google_bar/one_google_bar_service.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/optional.h" | 13 #include "base/optional.h" |
| 14 #include "chrome/browser/search/one_google_bar/one_google_bar_data.h" | 14 #include "chrome/browser/search/one_google_bar/one_google_bar_data.h" |
| 15 #include "chrome/browser/search/one_google_bar/one_google_bar_fetcher.h" | 15 #include "chrome/browser/search/one_google_bar/one_google_bar_fetcher.h" |
| 16 #include "components/signin/core/browser/account_tracker_service.h" | 16 #include "components/signin/core/browser/account_tracker_service.h" |
| 17 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 17 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 18 #include "components/signin/core/browser/fake_signin_manager.h" | 18 #include "components/signin/core/browser/fake_signin_manager.h" |
| 19 #include "components/signin/core/browser/test_signin_client.h" | 19 #include "components/signin/core/browser/test_signin_client.h" |
| 20 #include "components/sync_preferences/testing_pref_service_syncable.h" | 20 #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 23 |
| 24 using testing::Eq; | 24 using testing::Eq; |
| 25 using testing::InSequence; |
| 25 using testing::StrictMock; | 26 using testing::StrictMock; |
| 26 | 27 |
| 27 class FakeOneGoogleBarFetcher : public OneGoogleBarFetcher { | 28 class FakeOneGoogleBarFetcher : public OneGoogleBarFetcher { |
| 28 public: | 29 public: |
| 29 void Fetch(OneGoogleCallback callback) override { | 30 void Fetch(OneGoogleCallback callback) override { |
| 30 callbacks_.push_back(std::move(callback)); | 31 callbacks_.push_back(std::move(callback)); |
| 31 } | 32 } |
| 32 | 33 |
| 33 size_t GetCallbackCount() const { return callbacks_.size(); } | 34 size_t GetCallbackCount() const { return callbacks_.size(); } |
| 34 | 35 |
| 35 void RespondToAllCallbacks(const base::Optional<OneGoogleBarData>& data) { | 36 void RespondToAllCallbacks(const base::Optional<OneGoogleBarData>& data) { |
| 36 for (OneGoogleCallback& callback : callbacks_) { | 37 for (OneGoogleCallback& callback : callbacks_) { |
| 37 std::move(callback).Run(data); | 38 std::move(callback).Run(data); |
| 38 } | 39 } |
| 39 callbacks_.clear(); | 40 callbacks_.clear(); |
| 40 } | 41 } |
| 41 | 42 |
| 42 private: | 43 private: |
| 43 std::vector<OneGoogleCallback> callbacks_; | 44 std::vector<OneGoogleCallback> callbacks_; |
| 44 }; | 45 }; |
| 45 | 46 |
| 46 class MockOneGoogleBarServiceObserver : public OneGoogleBarServiceObserver { | 47 class MockOneGoogleBarServiceObserver : public OneGoogleBarServiceObserver { |
| 47 public: | 48 public: |
| 48 MOCK_METHOD0(OnOneGoogleBarDataChanged, void()); | 49 MOCK_METHOD0(OnOneGoogleBarDataChanged, void()); |
| 50 MOCK_METHOD0(OnOneGoogleBarFetchFailed, void()); |
| 49 }; | 51 }; |
| 50 | 52 |
| 51 class OneGoogleBarServiceTest : public testing::Test { | 53 class OneGoogleBarServiceTest : public testing::Test { |
| 52 public: | 54 public: |
| 53 OneGoogleBarServiceTest() | 55 OneGoogleBarServiceTest() |
| 54 : signin_client_(&pref_service_), | 56 : signin_client_(&pref_service_), |
| 55 signin_manager_(&signin_client_, &account_tracker_) { | 57 signin_manager_(&signin_client_, &account_tracker_) { |
| 56 SigninManagerBase::RegisterProfilePrefs(pref_service_.registry()); | 58 SigninManagerBase::RegisterProfilePrefs(pref_service_.registry()); |
| 57 SigninManagerBase::RegisterPrefs(pref_service_.registry()); | 59 SigninManagerBase::RegisterPrefs(pref_service_.registry()); |
| 58 | 60 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 EXPECT_THAT(service()->one_google_bar_data(), Eq(data)); | 100 EXPECT_THAT(service()->one_google_bar_data(), Eq(data)); |
| 99 | 101 |
| 100 // Fulfill the second request. | 102 // Fulfill the second request. |
| 101 OneGoogleBarData other_data; | 103 OneGoogleBarData other_data; |
| 102 other_data.bar_html = "<div>Different!</div>"; | 104 other_data.bar_html = "<div>Different!</div>"; |
| 103 fetcher()->RespondToAllCallbacks(other_data); | 105 fetcher()->RespondToAllCallbacks(other_data); |
| 104 EXPECT_THAT(service()->one_google_bar_data(), Eq(other_data)); | 106 EXPECT_THAT(service()->one_google_bar_data(), Eq(other_data)); |
| 105 } | 107 } |
| 106 | 108 |
| 107 TEST_F(OneGoogleBarServiceTest, NotifiesObserverOnChanges) { | 109 TEST_F(OneGoogleBarServiceTest, NotifiesObserverOnChanges) { |
| 110 InSequence s; |
| 111 |
| 108 ASSERT_THAT(service()->one_google_bar_data(), Eq(base::nullopt)); | 112 ASSERT_THAT(service()->one_google_bar_data(), Eq(base::nullopt)); |
| 109 | 113 |
| 110 StrictMock<MockOneGoogleBarServiceObserver> observer; | 114 StrictMock<MockOneGoogleBarServiceObserver> observer; |
| 111 service()->AddObserver(&observer); | 115 service()->AddObserver(&observer); |
| 112 | 116 |
| 113 // Empty result from a fetch doesn't change anything (it's already empty), so | 117 // Empty result from a fetch should result in a "fetch failed" notification. |
| 114 // should not result in a notification. | 118 // However, the actual data doesn't change anything (it's already empty), so |
| 119 // it should not result in a "data changed". |
| 115 service()->Refresh(); | 120 service()->Refresh(); |
| 121 EXPECT_CALL(observer, OnOneGoogleBarFetchFailed()); |
| 116 fetcher()->RespondToAllCallbacks(base::nullopt); | 122 fetcher()->RespondToAllCallbacks(base::nullopt); |
| 117 | 123 |
| 118 // Non-empty response should result in a notification. | 124 // Non-empty response should result in a notification. |
| 119 service()->Refresh(); | 125 service()->Refresh(); |
| 120 OneGoogleBarData data; | 126 OneGoogleBarData data; |
| 121 data.bar_html = "<div></div>"; | 127 data.bar_html = "<div></div>"; |
| 122 EXPECT_CALL(observer, OnOneGoogleBarDataChanged()); | 128 EXPECT_CALL(observer, OnOneGoogleBarDataChanged()); |
| 123 fetcher()->RespondToAllCallbacks(data); | 129 fetcher()->RespondToAllCallbacks(data); |
| 124 EXPECT_THAT(service()->one_google_bar_data(), Eq(data)); | 130 EXPECT_THAT(service()->one_google_bar_data(), Eq(data)); |
| 125 | 131 |
| 126 // Non-empty but identical response should not result in another notification. | 132 // Non-empty but identical response should not result in another notification. |
| 127 service()->Refresh(); | 133 service()->Refresh(); |
| 128 OneGoogleBarData identical_data = data; | 134 OneGoogleBarData identical_data = data; |
| 129 fetcher()->RespondToAllCallbacks(identical_data); | 135 fetcher()->RespondToAllCallbacks(identical_data); |
| 130 | 136 |
| 131 // Different response should result in a notification. | 137 // Different response should result in a notification. |
| 132 service()->Refresh(); | 138 service()->Refresh(); |
| 133 OneGoogleBarData other_data; | 139 OneGoogleBarData other_data; |
| 134 data.bar_html = "<div>Different</div>"; | 140 data.bar_html = "<div>Different</div>"; |
| 135 EXPECT_CALL(observer, OnOneGoogleBarDataChanged()); | 141 EXPECT_CALL(observer, OnOneGoogleBarDataChanged()); |
| 136 fetcher()->RespondToAllCallbacks(other_data); | 142 fetcher()->RespondToAllCallbacks(other_data); |
| 137 EXPECT_THAT(service()->one_google_bar_data(), Eq(other_data)); | 143 EXPECT_THAT(service()->one_google_bar_data(), Eq(other_data)); |
| 138 | 144 |
| 139 // Finally, an empty response should result in a notification now. | 145 // Finally, an empty response should result in a notification now. |
| 140 service()->Refresh(); | 146 service()->Refresh(); |
| 141 EXPECT_CALL(observer, OnOneGoogleBarDataChanged()); | 147 EXPECT_CALL(observer, OnOneGoogleBarDataChanged()); |
| 148 EXPECT_CALL(observer, OnOneGoogleBarFetchFailed()); |
| 142 fetcher()->RespondToAllCallbacks(base::nullopt); | 149 fetcher()->RespondToAllCallbacks(base::nullopt); |
| 143 EXPECT_THAT(service()->one_google_bar_data(), Eq(base::nullopt)); | 150 EXPECT_THAT(service()->one_google_bar_data(), Eq(base::nullopt)); |
| 144 | 151 |
| 145 service()->RemoveObserver(&observer); | 152 service()->RemoveObserver(&observer); |
| 146 } | 153 } |
| 147 | 154 |
| 148 #if !defined(OS_CHROMEOS) | 155 #if !defined(OS_CHROMEOS) |
| 149 | 156 |
| 150 // Like OneGoogleBarServiceTest, but it has a FakeSigninManager (rather than | 157 // Like OneGoogleBarServiceTest, but it has a FakeSigninManager (rather than |
| 151 // FakeSigninManagerBase), so it can simulate sign-in and sign-out. | 158 // FakeSigninManagerBase), so it can simulate sign-in and sign-out. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 data.bar_html = "<div></div>"; | 217 data.bar_html = "<div></div>"; |
| 211 fetcher()->RespondToAllCallbacks(data); | 218 fetcher()->RespondToAllCallbacks(data); |
| 212 ASSERT_THAT(service()->one_google_bar_data(), Eq(data)); | 219 ASSERT_THAT(service()->one_google_bar_data(), Eq(data)); |
| 213 | 220 |
| 214 // Sign out. This should clear the cached data. | 221 // Sign out. This should clear the cached data. |
| 215 SignOut(); | 222 SignOut(); |
| 216 EXPECT_THAT(service()->one_google_bar_data(), Eq(base::nullopt)); | 223 EXPECT_THAT(service()->one_google_bar_data(), Eq(base::nullopt)); |
| 217 } | 224 } |
| 218 | 225 |
| 219 #endif // OS_CHROMEOS | 226 #endif // OS_CHROMEOS |
| OLD | NEW |