Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: chrome/browser/ui/webui/sync_internals_message_handler_unittest.cc

Issue 2910773003: [Sync] Replace manual DI with SetTestingFactory. (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/ui/webui/sync_internals_message_handler.h" 5 #include "chrome/browser/ui/webui/sync_internals_message_handler.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "chrome/browser/sync/profile_sync_service_factory.h"
13 #include "chrome/browser/sync/user_event_service_factory.h"
12 #include "chrome/test/base/testing_profile.h" 14 #include "chrome/test/base/testing_profile.h"
13 #include "components/browser_sync/browser_sync_switches.h" 15 #include "components/browser_sync/browser_sync_switches.h"
14 #include "components/sync/driver/about_sync_util.h" 16 #include "components/sync/driver/about_sync_util.h"
15 #include "components/sync/driver/fake_sync_service.h" 17 #include "components/sync/driver/fake_sync_service.h"
16 #include "components/sync/driver/sync_service.h" 18 #include "components/sync/driver/sync_service.h"
17 #include "components/sync/js/js_test_util.h" 19 #include "components/sync/js/js_test_util.h"
18 #include "content/public/browser/site_instance.h" 20 #include "content/public/browser/site_instance.h"
19 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/test_browser_thread_bundle.h" 22 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "content/public/test/test_web_ui.h" 23 #include "content/public/test/test_web_ui.h"
22 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
23 25
24 using base::DictionaryValue; 26 using base::DictionaryValue;
25 using base::ListValue; 27 using base::ListValue;
26 using base::Value; 28 using base::Value;
27 using syncer::SyncService; 29 using syncer::SyncService;
28 using syncer::SyncServiceObserver; 30 using syncer::SyncServiceObserver;
29 using syncer::TypeDebugInfoObserver; 31 using syncer::TypeDebugInfoObserver;
30 32
31 namespace { 33 namespace {
32 34
33 class TestableSyncInternalsMessageHandler : public SyncInternalsMessageHandler { 35 class TestableSyncInternalsMessageHandler : public SyncInternalsMessageHandler {
34 public: 36 public:
35 TestableSyncInternalsMessageHandler( 37 TestableSyncInternalsMessageHandler(
36 content::WebUI* web_ui, 38 content::WebUI* web_ui,
37 SyncServiceProvider sync_service_provider,
38 AboutSyncDataDelegate about_sync_data_delegate) 39 AboutSyncDataDelegate about_sync_data_delegate)
39 : SyncInternalsMessageHandler(std::move(sync_service_provider), 40 : SyncInternalsMessageHandler(std::move(about_sync_data_delegate)) {
40 std::move(about_sync_data_delegate)) {
41 set_web_ui(web_ui); 41 set_web_ui(web_ui);
42 } 42 }
43 }; 43 };
44 44
45 class TestSyncService : public syncer::FakeSyncService { 45 class TestSyncService : public syncer::FakeSyncService {
46 public: 46 public:
47 void AddObserver(SyncServiceObserver* observer) override { 47 void AddObserver(SyncServiceObserver* observer) override {
48 ++add_observer_count_; 48 ++add_observer_count_;
49 } 49 }
50 50
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 private: 85 private:
86 int add_observer_count_ = 0; 86 int add_observer_count_ = 0;
87 int remove_observer_count_ = 0; 87 int remove_observer_count_ = 0;
88 int add_type_debug_info_observer_count_ = 0; 88 int add_type_debug_info_observer_count_ = 0;
89 int remove_type_debug_info_observer_count_ = 0; 89 int remove_type_debug_info_observer_count_ = 0;
90 syncer::MockJsController js_controller_; 90 syncer::MockJsController js_controller_;
91 base::Callback<void(std::unique_ptr<base::ListValue>)> 91 base::Callback<void(std::unique_ptr<base::ListValue>)>
92 get_all_nodes_callback_; 92 get_all_nodes_callback_;
93 }; 93 };
94 94
95 static std::unique_ptr<KeyedService> BuildTestSyncService(
96 content::BrowserContext* context) {
97 return base::MakeUnique<TestSyncService>();
98 }
99
95 class SyncInternalsMessageHandlerTest : public ::testing::Test { 100 class SyncInternalsMessageHandlerTest : public ::testing::Test {
96 protected: 101 protected:
97 SyncInternalsMessageHandlerTest() { 102 SyncInternalsMessageHandlerTest() {
98 site_instance_ = content::SiteInstance::Create(&profile_); 103 site_instance_ = content::SiteInstance::Create(&profile_);
99 web_contents_.reset(content::WebContents::Create( 104 web_contents_.reset(content::WebContents::Create(
100 content::WebContents::CreateParams(&profile_, site_instance_.get()))); 105 content::WebContents::CreateParams(&profile_, site_instance_.get())));
101 web_ui_.set_web_contents(web_contents_.get()); 106 web_ui_.set_web_contents(web_contents_.get());
102 test_sync_service_ = base::MakeUnique<TestSyncService>(); 107 test_sync_service_ = static_cast<TestSyncService*>(
108 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
109 &profile_, &BuildTestSyncService));
103 handler_.reset(new TestableSyncInternalsMessageHandler( 110 handler_.reset(new TestableSyncInternalsMessageHandler(
104 &web_ui_, 111 &web_ui_,
105 base::BindRepeating(&SyncInternalsMessageHandlerTest::sync_service,
106 base::Unretained(this)),
107 base::BindRepeating( 112 base::BindRepeating(
108 &SyncInternalsMessageHandlerTest::ConstructAboutInformation, 113 &SyncInternalsMessageHandlerTest::ConstructAboutInformation,
109 base::Unretained(this)))); 114 base::Unretained(this))));
110 } 115 }
111 116
112 std::unique_ptr<DictionaryValue> ConstructAboutInformation( 117 std::unique_ptr<DictionaryValue> ConstructAboutInformation(
113 SyncService* service, 118 SyncService* service,
114 version_info::Channel channel) { 119 version_info::Channel channel) {
115 ++about_sync_data_delegate_call_count_; 120 ++about_sync_data_delegate_call_count_;
116 last_delegate_sync_service_ = service; 121 last_delegate_sync_service_ = service;
(...skipping 25 matching lines...) Expand all
142 147
143 std::string fake_value; 148 std::string fake_value;
144 EXPECT_TRUE(root_dictionary->GetString("fake_key", &fake_value)); 149 EXPECT_TRUE(root_dictionary->GetString("fake_key", &fake_value));
145 EXPECT_EQ("fake_value", fake_value); 150 EXPECT_EQ("fake_value", fake_value);
146 } 151 }
147 152
148 void ValidateEmptyAboutInfoCall() { 153 void ValidateEmptyAboutInfoCall() {
149 EXPECT_TRUE(web_ui_.call_data().empty()); 154 EXPECT_TRUE(web_ui_.call_data().empty());
150 } 155 }
151 156
152 TestSyncService* test_sync_service() { return test_sync_service_.get(); } 157 TestingProfile* profile() { return &profile_; }
158
159 TestSyncService* test_sync_service() { return test_sync_service_; }
153 160
154 TestableSyncInternalsMessageHandler* handler() { return handler_.get(); } 161 TestableSyncInternalsMessageHandler* handler() { return handler_.get(); }
155 162
156 int CallCountWithName(const std::string& function_name) { 163 int CallCountWithName(const std::string& function_name) {
157 int count = 0; 164 int count = 0;
158 for (const auto& call_data : web_ui_.call_data()) { 165 for (const auto& call_data : web_ui_.call_data()) {
159 if (call_data->function_name() == function_name) { 166 if (call_data->function_name() == function_name) {
160 count++; 167 count++;
161 } 168 }
162 } 169 }
163 return count; 170 return count;
164 } 171 }
165 172
166 int about_sync_data_delegate_call_count() const { 173 int about_sync_data_delegate_call_count() const {
167 return about_sync_data_delegate_call_count_; 174 return about_sync_data_delegate_call_count_;
168 } 175 }
169 176
170 const SyncService* last_delegate_sync_service() const { 177 const SyncService* last_delegate_sync_service() const {
171 return last_delegate_sync_service_; 178 return last_delegate_sync_service_;
172 } 179 }
173 180
174 void ResetSyncService() { test_sync_service_.reset(); }
175
176 void ResetHandler() { handler_.reset(); } 181 void ResetHandler() { handler_.reset(); }
177 182
178 private: 183 private:
179 SyncService* sync_service() { return test_sync_service_.get(); }
180
181 content::TestBrowserThreadBundle thread_bundle_; 184 content::TestBrowserThreadBundle thread_bundle_;
182 TestingProfile profile_; 185 TestingProfile profile_;
183 content::TestWebUI web_ui_;
184 scoped_refptr<content::SiteInstance> site_instance_; 186 scoped_refptr<content::SiteInstance> site_instance_;
185 std::unique_ptr<content::WebContents> web_contents_; 187 std::unique_ptr<content::WebContents> web_contents_;
186 std::unique_ptr<TestSyncService> test_sync_service_; 188 content::TestWebUI web_ui_;
189 TestSyncService* test_sync_service_;
187 std::unique_ptr<TestableSyncInternalsMessageHandler> handler_; 190 std::unique_ptr<TestableSyncInternalsMessageHandler> handler_;
188 int about_sync_data_delegate_call_count_ = 0; 191 int about_sync_data_delegate_call_count_ = 0;
189 SyncService* last_delegate_sync_service_ = nullptr; 192 SyncService* last_delegate_sync_service_ = nullptr;
190 }; 193 };
191 194
192 TEST_F(SyncInternalsMessageHandlerTest, AddRemoveObservers) { 195 TEST_F(SyncInternalsMessageHandlerTest, AddRemoveObservers) {
193 ListValue empty_list; 196 ListValue empty_list;
194 197
195 EXPECT_EQ(0, test_sync_service()->add_observer_count()); 198 EXPECT_EQ(0, test_sync_service()->add_observer_count());
196 handler()->HandleRegisterForEvents(&empty_list); 199 handler()->HandleRegisterForEvents(&empty_list);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 // Deregistration should not repeat, no counts should increase. 234 // Deregistration should not repeat, no counts should increase.
232 ResetHandler(); 235 ResetHandler();
233 EXPECT_EQ(1, test_sync_service()->add_observer_count()); 236 EXPECT_EQ(1, test_sync_service()->add_observer_count());
234 EXPECT_EQ(1, test_sync_service()->add_type_debug_info_observer_count()); 237 EXPECT_EQ(1, test_sync_service()->add_type_debug_info_observer_count());
235 EXPECT_EQ(1, test_sync_service()->remove_observer_count()); 238 EXPECT_EQ(1, test_sync_service()->remove_observer_count());
236 EXPECT_EQ(1, test_sync_service()->remove_type_debug_info_observer_count()); 239 EXPECT_EQ(1, test_sync_service()->remove_type_debug_info_observer_count());
237 } 240 }
238 241
239 TEST_F(SyncInternalsMessageHandlerTest, AddRemoveObserversSyncDisabled) { 242 TEST_F(SyncInternalsMessageHandlerTest, AddRemoveObserversSyncDisabled) {
240 // Simulate completely disabling sync by flag or other mechanism. 243 // Simulate completely disabling sync by flag or other mechanism.
241 ResetSyncService(); 244 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory(profile(),
245 nullptr);
242 246
243 ListValue empty_list; 247 ListValue empty_list;
244 handler()->HandleRegisterForEvents(&empty_list); 248 handler()->HandleRegisterForEvents(&empty_list);
245 handler()->HandleRegisterForPerTypeCounters(&empty_list); 249 handler()->HandleRegisterForPerTypeCounters(&empty_list);
246 handler()->DisallowJavascript(); 250 handler()->DisallowJavascript();
247 // Cannot verify observer methods on sync services were not called, because 251 // Cannot verify observer methods on sync services were not called, because
248 // there is no sync service. Rather, we're just making sure the handler hasn't 252 // there is no sync service. Rather, we're just making sure the handler hasn't
249 // performed any invalid operations when the sync service is missing. 253 // performed any invalid operations when the sync service is missing.
250 } 254 }
251 255
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 TEST_F(SyncInternalsMessageHandlerTest, SendAboutInfo) { 298 TEST_F(SyncInternalsMessageHandlerTest, SendAboutInfo) {
295 handler()->AllowJavascriptForTesting(); 299 handler()->AllowJavascriptForTesting();
296 handler()->OnStateChanged(nullptr); 300 handler()->OnStateChanged(nullptr);
297 EXPECT_EQ(1, about_sync_data_delegate_call_count()); 301 EXPECT_EQ(1, about_sync_data_delegate_call_count());
298 EXPECT_NE(nullptr, last_delegate_sync_service()); 302 EXPECT_NE(nullptr, last_delegate_sync_service());
299 ValidateAboutInfoCall(); 303 ValidateAboutInfoCall();
300 } 304 }
301 305
302 TEST_F(SyncInternalsMessageHandlerTest, SendAboutInfoSyncDisabled) { 306 TEST_F(SyncInternalsMessageHandlerTest, SendAboutInfoSyncDisabled) {
303 // Simulate completely disabling sync by flag or other mechanism. 307 // Simulate completely disabling sync by flag or other mechanism.
304 ResetSyncService(); 308 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory(profile(),
309 nullptr);
305 310
306 handler()->AllowJavascriptForTesting(); 311 handler()->AllowJavascriptForTesting();
307 handler()->OnStateChanged(nullptr); 312 handler()->OnStateChanged(nullptr);
308 EXPECT_EQ(1, about_sync_data_delegate_call_count()); 313 EXPECT_EQ(1, about_sync_data_delegate_call_count());
309 EXPECT_EQ(nullptr, last_delegate_sync_service()); 314 EXPECT_EQ(nullptr, last_delegate_sync_service());
310 ValidateAboutInfoCall(); 315 ValidateAboutInfoCall();
311 } 316 }
312 317
313 } // namespace 318 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/sync_internals_message_handler.cc ('k') | components/browser_sync/profile_sync_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698