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

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

Issue 2909283003: [Sync] Split UserEventService into interface and impl, add fake impl, add unit tests. (Closed)
Patch Set: Added a NoOp service to handle OffTheRecord. 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
« no previous file with comments | « chrome/browser/ui/webui/sync_internals_message_handler.cc ('k') | components/sync/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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" 12 #include "chrome/browser/sync/profile_sync_service_factory.h"
13 #include "chrome/browser/sync/user_event_service_factory.h" 13 #include "chrome/browser/sync/user_event_service_factory.h"
14 #include "chrome/test/base/testing_profile.h" 14 #include "chrome/test/base/testing_profile.h"
15 #include "components/browser_sync/browser_sync_switches.h" 15 #include "components/browser_sync/browser_sync_switches.h"
16 #include "components/sync/driver/about_sync_util.h" 16 #include "components/sync/driver/about_sync_util.h"
17 #include "components/sync/driver/fake_sync_service.h" 17 #include "components/sync/driver/fake_sync_service.h"
18 #include "components/sync/driver/sync_service.h" 18 #include "components/sync/driver/sync_service.h"
19 #include "components/sync/js/js_test_util.h" 19 #include "components/sync/js/js_test_util.h"
20 #include "components/sync/user_events/fake_user_event_service.h"
20 #include "content/public/browser/site_instance.h" 21 #include "content/public/browser/site_instance.h"
21 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
22 #include "content/public/test/test_browser_thread_bundle.h" 23 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "content/public/test/test_web_ui.h" 24 #include "content/public/test/test_web_ui.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 26
26 using base::DictionaryValue; 27 using base::DictionaryValue;
27 using base::ListValue; 28 using base::ListValue;
28 using base::Value; 29 using base::Value;
30 using syncer::FakeUserEventService;
29 using syncer::SyncService; 31 using syncer::SyncService;
30 using syncer::SyncServiceObserver; 32 using syncer::SyncServiceObserver;
31 using syncer::TypeDebugInfoObserver; 33 using syncer::TypeDebugInfoObserver;
32 34
33 namespace { 35 namespace {
34 36
35 class TestableSyncInternalsMessageHandler : public SyncInternalsMessageHandler { 37 class TestableSyncInternalsMessageHandler : public SyncInternalsMessageHandler {
36 public: 38 public:
37 TestableSyncInternalsMessageHandler( 39 TestableSyncInternalsMessageHandler(
38 content::WebUI* web_ui, 40 content::WebUI* web_ui,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 syncer::MockJsController js_controller_; 92 syncer::MockJsController js_controller_;
91 base::Callback<void(std::unique_ptr<base::ListValue>)> 93 base::Callback<void(std::unique_ptr<base::ListValue>)>
92 get_all_nodes_callback_; 94 get_all_nodes_callback_;
93 }; 95 };
94 96
95 static std::unique_ptr<KeyedService> BuildTestSyncService( 97 static std::unique_ptr<KeyedService> BuildTestSyncService(
96 content::BrowserContext* context) { 98 content::BrowserContext* context) {
97 return base::MakeUnique<TestSyncService>(); 99 return base::MakeUnique<TestSyncService>();
98 } 100 }
99 101
102 static std::unique_ptr<KeyedService> BuildFakeUserEventService(
103 content::BrowserContext* context) {
104 return base::MakeUnique<FakeUserEventService>();
105 }
106
100 class SyncInternalsMessageHandlerTest : public ::testing::Test { 107 class SyncInternalsMessageHandlerTest : public ::testing::Test {
101 protected: 108 protected:
102 SyncInternalsMessageHandlerTest() { 109 SyncInternalsMessageHandlerTest() {
103 site_instance_ = content::SiteInstance::Create(&profile_); 110 site_instance_ = content::SiteInstance::Create(&profile_);
104 web_contents_.reset(content::WebContents::Create( 111 web_contents_.reset(content::WebContents::Create(
105 content::WebContents::CreateParams(&profile_, site_instance_.get()))); 112 content::WebContents::CreateParams(&profile_, site_instance_.get())));
106 web_ui_.set_web_contents(web_contents_.get()); 113 web_ui_.set_web_contents(web_contents_.get());
107 test_sync_service_ = static_cast<TestSyncService*>( 114 test_sync_service_ = static_cast<TestSyncService*>(
108 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( 115 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
109 &profile_, &BuildTestSyncService)); 116 &profile_, &BuildTestSyncService));
117 fake_user_event_service_ = static_cast<FakeUserEventService*>(
118 browser_sync::UserEventServiceFactory::GetInstance()
119 ->SetTestingFactoryAndUse(&profile_, &BuildFakeUserEventService));
110 handler_.reset(new TestableSyncInternalsMessageHandler( 120 handler_.reset(new TestableSyncInternalsMessageHandler(
111 &web_ui_, 121 &web_ui_,
112 base::BindRepeating( 122 base::BindRepeating(
113 &SyncInternalsMessageHandlerTest::ConstructAboutInformation, 123 &SyncInternalsMessageHandlerTest::ConstructAboutInformation,
114 base::Unretained(this)))); 124 base::Unretained(this))));
115 } 125 }
116 126
117 std::unique_ptr<DictionaryValue> ConstructAboutInformation( 127 std::unique_ptr<DictionaryValue> ConstructAboutInformation(
118 SyncService* service, 128 SyncService* service,
119 version_info::Channel channel) { 129 version_info::Channel channel) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 } 161 }
152 162
153 void ValidateEmptyAboutInfoCall() { 163 void ValidateEmptyAboutInfoCall() {
154 EXPECT_TRUE(web_ui_.call_data().empty()); 164 EXPECT_TRUE(web_ui_.call_data().empty());
155 } 165 }
156 166
157 TestingProfile* profile() { return &profile_; } 167 TestingProfile* profile() { return &profile_; }
158 168
159 TestSyncService* test_sync_service() { return test_sync_service_; } 169 TestSyncService* test_sync_service() { return test_sync_service_; }
160 170
171 FakeUserEventService* fake_user_event_service() {
172 return fake_user_event_service_;
173 }
174
161 TestableSyncInternalsMessageHandler* handler() { return handler_.get(); } 175 TestableSyncInternalsMessageHandler* handler() { return handler_.get(); }
162 176
163 int CallCountWithName(const std::string& function_name) { 177 int CallCountWithName(const std::string& function_name) {
164 int count = 0; 178 int count = 0;
165 for (const auto& call_data : web_ui_.call_data()) { 179 for (const auto& call_data : web_ui_.call_data()) {
166 if (call_data->function_name() == function_name) { 180 if (call_data->function_name() == function_name) {
167 count++; 181 count++;
168 } 182 }
169 } 183 }
170 return count; 184 return count;
171 } 185 }
172 186
173 int about_sync_data_delegate_call_count() const { 187 int about_sync_data_delegate_call_count() const {
174 return about_sync_data_delegate_call_count_; 188 return about_sync_data_delegate_call_count_;
175 } 189 }
176 190
177 const SyncService* last_delegate_sync_service() const { 191 const SyncService* last_delegate_sync_service() const {
178 return last_delegate_sync_service_; 192 return last_delegate_sync_service_;
179 } 193 }
180 194
181 void ResetHandler() { handler_.reset(); } 195 void ResetHandler() { handler_.reset(); }
182 196
183 private: 197 private:
184 content::TestBrowserThreadBundle thread_bundle_; 198 content::TestBrowserThreadBundle thread_bundle_;
185 TestingProfile profile_; 199 TestingProfile profile_;
186 scoped_refptr<content::SiteInstance> site_instance_; 200 scoped_refptr<content::SiteInstance> site_instance_;
187 std::unique_ptr<content::WebContents> web_contents_; 201 std::unique_ptr<content::WebContents> web_contents_;
188 content::TestWebUI web_ui_; 202 content::TestWebUI web_ui_;
189 TestSyncService* test_sync_service_; 203 TestSyncService* test_sync_service_;
204 FakeUserEventService* fake_user_event_service_;
190 std::unique_ptr<TestableSyncInternalsMessageHandler> handler_; 205 std::unique_ptr<TestableSyncInternalsMessageHandler> handler_;
191 int about_sync_data_delegate_call_count_ = 0; 206 int about_sync_data_delegate_call_count_ = 0;
192 SyncService* last_delegate_sync_service_ = nullptr; 207 SyncService* last_delegate_sync_service_ = nullptr;
193 }; 208 };
194 209
195 TEST_F(SyncInternalsMessageHandlerTest, AddRemoveObservers) { 210 TEST_F(SyncInternalsMessageHandlerTest, AddRemoveObservers) {
196 ListValue empty_list; 211 ListValue empty_list;
197 212
198 EXPECT_EQ(0, test_sync_service()->add_observer_count()); 213 EXPECT_EQ(0, test_sync_service()->add_observer_count());
199 handler()->HandleRegisterForEvents(&empty_list); 214 handler()->HandleRegisterForEvents(&empty_list);
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory(profile(), 323 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory(profile(),
309 nullptr); 324 nullptr);
310 325
311 handler()->AllowJavascriptForTesting(); 326 handler()->AllowJavascriptForTesting();
312 handler()->OnStateChanged(nullptr); 327 handler()->OnStateChanged(nullptr);
313 EXPECT_EQ(1, about_sync_data_delegate_call_count()); 328 EXPECT_EQ(1, about_sync_data_delegate_call_count());
314 EXPECT_EQ(nullptr, last_delegate_sync_service()); 329 EXPECT_EQ(nullptr, last_delegate_sync_service());
315 ValidateAboutInfoCall(); 330 ValidateAboutInfoCall();
316 } 331 }
317 332
333 TEST_F(SyncInternalsMessageHandlerTest, WriteUserEvent) {
334 ListValue args;
335 args.AppendString("1000000000000000000");
336 args.AppendString("-1");
337 handler()->HandleWriteUserEvent(&args);
338
339 ASSERT_EQ(1u, fake_user_event_service()->GetRecordedUserEvents().size());
340 const sync_pb::UserEventSpecifics& event =
341 *fake_user_event_service()->GetRecordedUserEvents().begin();
342 EXPECT_EQ(1000000000000000000, event.event_time_usec());
343 EXPECT_EQ(-1, event.navigation_id());
344 }
345
346 TEST_F(SyncInternalsMessageHandlerTest, WriteUserEventBadParse) {
347 ListValue args;
348 args.AppendString("123abc");
349 args.AppendString("");
350 handler()->HandleWriteUserEvent(&args);
351
352 ASSERT_EQ(1u, fake_user_event_service()->GetRecordedUserEvents().size());
353 const sync_pb::UserEventSpecifics& event =
354 *fake_user_event_service()->GetRecordedUserEvents().begin();
355 EXPECT_EQ(0, event.event_time_usec());
356 EXPECT_EQ(0, event.navigation_id());
357 }
358
318 } // namespace 359 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/sync_internals_message_handler.cc ('k') | components/sync/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698