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

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

Issue 2898723003: [Sync] Migrate SyncInternalsMessageHandler off CallJavascriptFunctionUnsafe. (Closed)
Patch Set: Fix Android compile. Created 3 years, 7 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/test/base/testing_profile.h" 12 #include "chrome/test/base/testing_profile.h"
13 #include "components/browser_sync/browser_sync_switches.h" 13 #include "components/browser_sync/browser_sync_switches.h"
14 #include "components/sync/driver/about_sync_util.h" 14 #include "components/sync/driver/about_sync_util.h"
15 #include "components/sync/driver/fake_sync_service.h"
15 #include "components/sync/driver/sync_service.h" 16 #include "components/sync/driver/sync_service.h"
17 #include "components/sync/js/js_test_util.h"
16 #include "content/public/browser/site_instance.h" 18 #include "content/public/browser/site_instance.h"
17 #include "content/public/browser/web_contents.h" 19 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/test_browser_thread_bundle.h" 20 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "content/public/test/test_web_ui.h" 21 #include "content/public/test/test_web_ui.h"
20 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
21 23
24 using base::DictionaryValue;
25 using base::ListValue;
26 using base::Value;
27 using syncer::SyncService;
28 using syncer::SyncServiceObserver;
29 using syncer::TypeDebugInfoObserver;
30
22 namespace { 31 namespace {
23 32
24 class TestableSyncInternalsMessageHandler : public SyncInternalsMessageHandler { 33 class TestableSyncInternalsMessageHandler : public SyncInternalsMessageHandler {
25 public: 34 public:
26 explicit TestableSyncInternalsMessageHandler( 35 TestableSyncInternalsMessageHandler(
27 content::WebUI* web_ui, 36 content::WebUI* web_ui,
28 std::unique_ptr<AboutSyncDataExtractor> about_sync_data_extractor) 37 SyncServiceProvider sync_service_provider,
29 : SyncInternalsMessageHandler(std::move(about_sync_data_extractor)) { 38 AboutSyncDataDelegate about_sync_data_delegate)
39 : SyncInternalsMessageHandler(std::move(sync_service_provider),
40 std::move(about_sync_data_delegate)) {
30 set_web_ui(web_ui); 41 set_web_ui(web_ui);
31 } 42 }
32 }; 43 };
33 44
34 class FakeExtractor : public AboutSyncDataExtractor { 45 class TestSyncService : public syncer::FakeSyncService {
35 public: 46 public:
36 std::unique_ptr<base::DictionaryValue> ConstructAboutInformation( 47 void AddObserver(SyncServiceObserver* observer) override {
37 syncer::SyncService* service, 48 ++add_observer_count_;
38 SigninManagerBase* signin) override {
39 call_count_++;
40 last_service_ = service;
41 last_signin_ = signin;
42 std::unique_ptr<base::DictionaryValue> dictionary(
43 new base::DictionaryValue());
44 dictionary->SetString("fake_key", "fake_value");
45 return dictionary;
46 } 49 }
47 50
48 int call_count() const { return call_count_; } 51 void RemoveObserver(SyncServiceObserver* observer) override {
49 syncer::SyncService* last_service() const { return last_service_; } 52 ++remove_observer_count_;
50 SigninManagerBase* last_signin() const { return last_signin_; } 53 }
54
55 void AddTypeDebugInfoObserver(TypeDebugInfoObserver* observer) override {
56 ++add_type_debug_info_observer_count_;
57 }
58
59 void RemoveTypeDebugInfoObserver(TypeDebugInfoObserver* observer) override {
60 ++remove_type_debug_info_observer_count_;
61 }
62
63 base::WeakPtr<syncer::JsController> GetJsController() override {
64 return js_controller_.AsWeakPtr();
65 }
66
67 int add_observer_count() const { return add_observer_count_; }
68 int remove_observer_count() const { return remove_observer_count_; }
69 int add_type_debug_info_observer_count() const {
70 return add_type_debug_info_observer_count_;
71 }
72 int remove_type_debug_info_observer_count() const {
73 return remove_type_debug_info_observer_count_;
74 }
51 75
52 private: 76 private:
53 int call_count_ = 0; 77 int add_observer_count_ = 0;
54 syncer::SyncService* last_service_ = nullptr; 78 int remove_observer_count_ = 0;
55 SigninManagerBase* last_signin_ = nullptr; 79 int add_type_debug_info_observer_count_ = 0;
80 int remove_type_debug_info_observer_count_ = 0;
81 syncer::MockJsController js_controller_;
56 }; 82 };
57 83
58 class SyncInternalsMessageHandlerTest : public ::testing::Test { 84 class SyncInternalsMessageHandlerTest : public ::testing::Test {
59 protected: 85 protected:
60 SyncInternalsMessageHandlerTest() { 86 SyncInternalsMessageHandlerTest() {
61 site_instance_ = content::SiteInstance::Create(&profile_); 87 site_instance_ = content::SiteInstance::Create(&profile_);
62 web_contents_.reset(content::WebContents::Create( 88 web_contents_.reset(content::WebContents::Create(
63 content::WebContents::CreateParams(&profile_, site_instance_.get()))); 89 content::WebContents::CreateParams(&profile_, site_instance_.get())));
64 web_ui_.set_web_contents(web_contents_.get()); 90 web_ui_.set_web_contents(web_contents_.get());
65 fake_extractor_ = new FakeExtractor(); 91 test_sync_service_ = base::MakeUnique<TestSyncService>();
66 handler_.reset(new TestableSyncInternalsMessageHandler( 92 handler_.reset(new TestableSyncInternalsMessageHandler(
67 &web_ui_, std::unique_ptr<FakeExtractor>(fake_extractor_))); 93 &web_ui_,
94 base::BindRepeating(&SyncInternalsMessageHandlerTest::sync_service,
95 base::Unretained(this)),
96 base::BindRepeating(
97 &SyncInternalsMessageHandlerTest::ConstructAboutInformation,
98 base::Unretained(this))));
99 }
100
101 std::unique_ptr<DictionaryValue> ConstructAboutInformation(
102 SyncService* service,
103 version_info::Channel channel) {
104 ++about_sync_data_delegate_call_count_;
105 last_delegate_sync_service_ = service;
106 auto dictionary = base::MakeUnique<DictionaryValue>();
107 dictionary->SetString("fake_key", "fake_value");
108 return dictionary;
68 } 109 }
69 110
70 void ValidateAboutInfoCall() { 111 void ValidateAboutInfoCall() {
71 const auto& data_vector = web_ui_.call_data(); 112 const auto& data_vector = web_ui_.call_data();
72 ASSERT_FALSE(data_vector.empty()); 113 ASSERT_FALSE(data_vector.empty());
73 EXPECT_EQ(1u, data_vector.size()); 114 EXPECT_EQ(1u, data_vector.size());
74 115
75 const content::TestWebUI::CallData& call_data = *data_vector[0]; 116 const content::TestWebUI::CallData& call_data = *data_vector[0];
76 117
77 EXPECT_EQ(syncer::sync_ui_util::kDispatchEvent, call_data.function_name()); 118 EXPECT_EQ(syncer::sync_ui_util::kDispatchEvent, call_data.function_name());
78 119
79 const base::Value* arg1 = call_data.arg1(); 120 const Value* arg1 = call_data.arg1();
80 ASSERT_TRUE(arg1); 121 ASSERT_TRUE(arg1);
81 std::string event_type; 122 std::string event_type;
82 EXPECT_TRUE(arg1->GetAsString(&event_type)); 123 EXPECT_TRUE(arg1->GetAsString(&event_type));
83 EXPECT_EQ(syncer::sync_ui_util::kOnAboutInfoUpdated, event_type); 124 EXPECT_EQ(syncer::sync_ui_util::kOnAboutInfoUpdated, event_type);
84 125
85 const base::Value* arg2 = call_data.arg2(); 126 const Value* arg2 = call_data.arg2();
86 ASSERT_TRUE(arg2); 127 ASSERT_TRUE(arg2);
87 128
88 const base::DictionaryValue* root_dictionary = nullptr; 129 const DictionaryValue* root_dictionary = nullptr;
89 ASSERT_TRUE(arg2->GetAsDictionary(&root_dictionary)); 130 ASSERT_TRUE(arg2->GetAsDictionary(&root_dictionary));
90 131
91 std::string fake_value; 132 std::string fake_value;
92 EXPECT_TRUE(root_dictionary->GetString("fake_key", &fake_value)); 133 EXPECT_TRUE(root_dictionary->GetString("fake_key", &fake_value));
93 EXPECT_EQ("fake_value", fake_value); 134 EXPECT_EQ("fake_value", fake_value);
94 } 135 }
95 136
96 SyncInternalsMessageHandler* handler() { return handler_.get(); } 137 void ValidateEmptyAboutInfoCall() {
97 FakeExtractor* fake_extractor() { return fake_extractor_; } 138 EXPECT_TRUE(web_ui_.call_data().empty());
139 }
140
141 TestSyncService* test_sync_service() { return test_sync_service_.get(); }
142
143 TestableSyncInternalsMessageHandler* handler() { return handler_.get(); }
144
145 int CallCountWithName(const std::string& function_name) {
146 int count = 0;
147 for (const auto& call_data : web_ui_.call_data()) {
148 if (call_data->function_name() == function_name) {
149 count++;
150 }
151 }
152 return count;
153 }
154
155 int about_sync_data_delegate_call_count() {
156 return about_sync_data_delegate_call_count_;
157 }
158
159 SyncService* last_delegate_sync_service() {
160 return last_delegate_sync_service_;
161 }
162
163 void ResetSyncService() { test_sync_service_.reset(); }
164
165 void ResetHandler() { handler_.reset(); }
98 166
99 private: 167 private:
168 SyncService* sync_service() { return test_sync_service_.get(); }
169
100 content::TestBrowserThreadBundle thread_bundle_; 170 content::TestBrowserThreadBundle thread_bundle_;
101 TestingProfile profile_; 171 TestingProfile profile_;
102 content::TestWebUI web_ui_; 172 content::TestWebUI web_ui_;
103 scoped_refptr<content::SiteInstance> site_instance_; 173 scoped_refptr<content::SiteInstance> site_instance_;
104 std::unique_ptr<content::WebContents> web_contents_; 174 std::unique_ptr<content::WebContents> web_contents_;
105 std::unique_ptr<SyncInternalsMessageHandler> handler_; 175 std::unique_ptr<TestSyncService> test_sync_service_;
176 std::unique_ptr<TestableSyncInternalsMessageHandler> handler_;
106 177
107 // Non-owning pointer to the about information the handler uses. This 178 int about_sync_data_delegate_call_count_ = 0;
108 // extractor is owned by the handler. 179 SyncService* last_delegate_sync_service_ = nullptr;
109 FakeExtractor* fake_extractor_;
110 }; 180 };
111 181
112 } // namespace 182 TEST_F(SyncInternalsMessageHandlerTest, AddRemoveObservers) {
183 ListValue empty_list;
113 184
114 TEST_F(SyncInternalsMessageHandlerTest, SendAboutInfoWithService) { 185 EXPECT_EQ(0, test_sync_service()->add_observer_count());
186 handler()->HandleRegisterForEvents(&empty_list);
187 EXPECT_EQ(1, test_sync_service()->add_observer_count());
188
189 EXPECT_EQ(0, test_sync_service()->add_type_debug_info_observer_count());
190 handler()->HandleRegisterForPerTypeCounters(&empty_list);
191 EXPECT_EQ(1, test_sync_service()->add_type_debug_info_observer_count());
192
193 EXPECT_EQ(0, test_sync_service()->remove_observer_count());
194 EXPECT_EQ(0, test_sync_service()->remove_type_debug_info_observer_count());
195 ResetHandler();
196 EXPECT_EQ(1, test_sync_service()->remove_observer_count());
197 EXPECT_EQ(1, test_sync_service()->remove_type_debug_info_observer_count());
198
199 // Add calls should never have increased since the initial subscription.
200 EXPECT_EQ(1, test_sync_service()->add_observer_count());
201 EXPECT_EQ(1, test_sync_service()->add_type_debug_info_observer_count());
202 }
203
204 TEST_F(SyncInternalsMessageHandlerTest, AddRemoveObserversDisallowJavascript) {
205 ListValue empty_list;
206
207 EXPECT_EQ(0, test_sync_service()->add_observer_count());
208 handler()->HandleRegisterForEvents(&empty_list);
209 EXPECT_EQ(1, test_sync_service()->add_observer_count());
210
211 EXPECT_EQ(0, test_sync_service()->add_type_debug_info_observer_count());
212 handler()->HandleRegisterForPerTypeCounters(&empty_list);
213 EXPECT_EQ(1, test_sync_service()->add_type_debug_info_observer_count());
214
215 EXPECT_EQ(0, test_sync_service()->remove_observer_count());
216 EXPECT_EQ(0, test_sync_service()->remove_type_debug_info_observer_count());
217 handler()->DisallowJavascript();
218 EXPECT_EQ(1, test_sync_service()->remove_observer_count());
219 EXPECT_EQ(1, test_sync_service()->remove_type_debug_info_observer_count());
220
221 // Deregistration should not repeat, no counts should increase.
222 ResetHandler();
223 EXPECT_EQ(1, test_sync_service()->add_observer_count());
224 EXPECT_EQ(1, test_sync_service()->add_type_debug_info_observer_count());
225 EXPECT_EQ(1, test_sync_service()->remove_observer_count());
226 EXPECT_EQ(1, test_sync_service()->remove_type_debug_info_observer_count());
227 }
228
229 TEST_F(SyncInternalsMessageHandlerTest, AddRemoveObserversSyncDisabled) {
230 // Simulate completely disabling sync by flag or other mechanism.
231 ResetSyncService();
232
233 ListValue empty_list;
234 handler()->HandleRegisterForEvents(&empty_list);
235 handler()->HandleRegisterForPerTypeCounters(&empty_list);
236 handler()->DisallowJavascript();
237 // Cannot verify observer methods on sync services were not called, because
238 // there is no sync service. Rather, we're just making sure the handler hasn't
239 // performed any invalid operations when the sync service is missing.
240 }
241
242 TEST_F(SyncInternalsMessageHandlerTest,
243 RepeatedHandleRegisterForPerTypeCounters) {
244 ListValue empty_list;
245 handler()->HandleRegisterForPerTypeCounters(&empty_list);
246 EXPECT_EQ(1, test_sync_service()->add_type_debug_info_observer_count());
247 EXPECT_EQ(0, test_sync_service()->remove_type_debug_info_observer_count());
248
249 handler()->HandleRegisterForPerTypeCounters(&empty_list);
250 EXPECT_EQ(2, test_sync_service()->add_type_debug_info_observer_count());
251 EXPECT_EQ(1, test_sync_service()->remove_type_debug_info_observer_count());
252
253 handler()->HandleRegisterForPerTypeCounters(&empty_list);
254 EXPECT_EQ(3, test_sync_service()->add_type_debug_info_observer_count());
255 EXPECT_EQ(2, test_sync_service()->remove_type_debug_info_observer_count());
256
257 ResetHandler();
258 EXPECT_EQ(3, test_sync_service()->add_type_debug_info_observer_count());
259 EXPECT_EQ(3, test_sync_service()->remove_type_debug_info_observer_count());
260 }
261
262 TEST_F(SyncInternalsMessageHandlerTest, HandleGetAllNodes) {
263 // This callback should be ignored because javascript is disabled.
264 handler()->OnReceivedAllNodes(0, base::MakeUnique<ListValue>());
265 EXPECT_EQ(0, CallCountWithName(syncer::sync_ui_util::kGetAllNodesCallback));
266
267 handler()->AllowJavascriptForTesting();
268 handler()->OnReceivedAllNodes(0, base::MakeUnique<ListValue>());
269 EXPECT_EQ(1, CallCountWithName(syncer::sync_ui_util::kGetAllNodesCallback));
270 }
271
272 TEST_F(SyncInternalsMessageHandlerTest, SendAboutInfo) {
273 handler()->AllowJavascriptForTesting();
115 handler()->OnStateChanged(nullptr); 274 handler()->OnStateChanged(nullptr);
116 EXPECT_EQ(1, fake_extractor()->call_count()); 275 EXPECT_EQ(1, about_sync_data_delegate_call_count());
117 EXPECT_NE(nullptr, fake_extractor()->last_service()); 276 EXPECT_NE(nullptr, last_delegate_sync_service());
118 EXPECT_NE(nullptr, fake_extractor()->last_signin());
119 ValidateAboutInfoCall(); 277 ValidateAboutInfoCall();
120 } 278 }
121 279
122 TEST_F(SyncInternalsMessageHandlerTest, SendAboutInfoWithoutService) { 280 TEST_F(SyncInternalsMessageHandlerTest, SendAboutInfoSyncDisabled) {
123 base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kDisableSync); 281 // Simulate completely disabling sync by flag or other mechanism.
282 ResetSyncService();
283
284 handler()->AllowJavascriptForTesting();
124 handler()->OnStateChanged(nullptr); 285 handler()->OnStateChanged(nullptr);
125 EXPECT_EQ(1, fake_extractor()->call_count()); 286 EXPECT_EQ(1, about_sync_data_delegate_call_count());
126 EXPECT_EQ(nullptr, fake_extractor()->last_service()); 287 EXPECT_EQ(nullptr, last_delegate_sync_service());
127 EXPECT_EQ(nullptr, fake_extractor()->last_signin());
128 ValidateAboutInfoCall(); 288 ValidateAboutInfoCall();
129 } 289 }
290
291 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698