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

Side by Side Diff: chrome/browser/sync/sessions/sessions_sync_manager_unittest.cc

Issue 2343463003: [Sync] Fix namespaces for the sync_sessions component. (Closed)
Patch Set: Fix gn. Created 4 years, 3 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/sync_sessions/sessions_sync_manager.h" 5 #include "components/sync_sessions/sessions_sync_manager.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "build/build_config.h" 14 #include "build/build_config.h"
15 #include "chrome/browser/sessions/session_tab_helper.h" 15 #include "chrome/browser/sessions/session_tab_helper.h"
16 #include "chrome/browser/sync/chrome_sync_client.h" 16 #include "chrome/browser/sync/chrome_sync_client.h"
17 #include "chrome/browser/sync/glue/session_sync_test_helper.h"
18 #include "chrome/browser/sync/sessions/notification_service_sessions_router.h" 17 #include "chrome/browser/sync/sessions/notification_service_sessions_router.h"
19 #include "chrome/browser/ui/sync/browser_synced_window_delegates_getter.h" 18 #include "chrome/browser/ui/sync/browser_synced_window_delegates_getter.h"
20 #include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h" 19 #include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h" 20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/test/base/browser_with_test_window_test.h" 21 #include "chrome/test/base/browser_with_test_window_test.h"
23 #include "components/sessions/content/content_serialized_navigation_builder.h" 22 #include "components/sessions/content/content_serialized_navigation_builder.h"
24 #include "components/sessions/core/serialized_navigation_entry_test_helper.h" 23 #include "components/sessions/core/serialized_navigation_entry_test_helper.h"
25 #include "components/sessions/core/session_id.h" 24 #include "components/sessions/core/session_id.h"
26 #include "components/sessions/core/session_types.h" 25 #include "components/sessions/core/session_types.h"
27 #include "components/sync/api/attachments/attachment_id.h" 26 #include "components/sync/api/attachments/attachment_id.h"
28 #include "components/sync/api/sync_error_factory_mock.h" 27 #include "components/sync/api/sync_error_factory_mock.h"
29 #include "components/sync/core/attachments/attachment_service_proxy_for_test.h" 28 #include "components/sync/core/attachments/attachment_service_proxy_for_test.h"
30 #include "components/sync/device_info/device_info.h" 29 #include "components/sync/device_info/device_info.h"
31 #include "components/sync/device_info/local_device_info_provider_mock.h" 30 #include "components/sync/device_info/local_device_info_provider_mock.h"
32 #include "components/sync/driver/sync_api_component_factory.h" 31 #include "components/sync/driver/sync_api_component_factory.h"
32 #include "components/sync_sessions/session_sync_test_helper.h"
33 #include "components/sync_sessions/sync_sessions_client.h" 33 #include "components/sync_sessions/sync_sessions_client.h"
34 #include "components/sync_sessions/synced_tab_delegate.h" 34 #include "components/sync_sessions/synced_tab_delegate.h"
35 #include "components/sync_sessions/synced_window_delegate.h" 35 #include "components/sync_sessions/synced_window_delegate.h"
36 #include "components/sync_sessions/synced_window_delegates_getter.h" 36 #include "components/sync_sessions/synced_window_delegates_getter.h"
37 #include "content/public/browser/navigation_entry.h" 37 #include "content/public/browser/navigation_entry.h"
38 #include "content/public/browser/web_contents.h" 38 #include "content/public/browser/web_contents.h"
39 #include "testing/gmock/include/gmock/gmock.h" 39 #include "testing/gmock/include/gmock/gmock.h"
40 #include "testing/gtest/include/gtest/gtest.h" 40 #include "testing/gtest/include/gtest/gtest.h"
41 41
42 using content::WebContents; 42 using content::WebContents;
43 using sessions::SerializedNavigationEntry; 43 using sessions::SerializedNavigationEntry;
44 using sessions::SerializedNavigationEntryTestHelper; 44 using sessions::SerializedNavigationEntryTestHelper;
45 using sync_driver::DeviceInfo; 45 using sync_driver::DeviceInfo;
46 using sync_driver::LocalDeviceInfoProvider; 46 using sync_driver::LocalDeviceInfoProvider;
47 using sync_driver::LocalDeviceInfoProviderMock; 47 using sync_driver::LocalDeviceInfoProviderMock;
48 using sync_driver::SyncedSession;
49 using syncer::SyncChange; 48 using syncer::SyncChange;
50 using syncer::SyncData; 49 using syncer::SyncData;
51 50
52 namespace browser_sync { 51 namespace sync_sessions {
53 52
54 namespace { 53 namespace {
55 54
56 class SessionNotificationObserver { 55 class SessionNotificationObserver {
57 public: 56 public:
58 SessionNotificationObserver() 57 SessionNotificationObserver()
59 : notified_of_update_(false), notified_of_refresh_(false) {} 58 : notified_of_update_(false), notified_of_refresh_(false) {}
60 void NotifyOfUpdate() { notified_of_update_ = true; } 59 void NotifyOfUpdate() { notified_of_update_ = true; }
61 void NotifyOfRefresh() { notified_of_refresh_ = true; } 60 void NotifyOfRefresh() { notified_of_refresh_ = true; }
62 61
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 } 202 }
204 203
205 int CountIfTagMatches(const syncer::SyncChangeList& changes, 204 int CountIfTagMatches(const syncer::SyncChangeList& changes,
206 const std::string& tag) { 205 const std::string& tag) {
207 return std::count_if( 206 return std::count_if(
208 changes.begin(), changes.end(), [&tag](const syncer::SyncChange& change) { 207 changes.begin(), changes.end(), [&tag](const syncer::SyncChange& change) {
209 return change.sync_data().GetSpecifics().session().session_tag() == tag; 208 return change.sync_data().GetSpecifics().session().session_tag() == tag;
210 }); 209 });
211 } 210 }
212 211
213 int CountIfTagMatches( 212 int CountIfTagMatches(const std::vector<const SyncedSession*>& sessions,
214 const std::vector<const sync_driver::SyncedSession*>& sessions, 213 const std::string& tag) {
215 const std::string& tag) {
216 return std::count_if(sessions.begin(), sessions.end(), 214 return std::count_if(sessions.begin(), sessions.end(),
217 [&tag](const sync_driver::SyncedSession* session) { 215 [&tag](const SyncedSession* session) {
218 return session->session_tag == tag; 216 return session->session_tag == tag;
219 }); 217 });
220 } 218 }
221 219
222 // Creates a field trial with the specified |trial_name| and |group_name| and 220 // Creates a field trial with the specified |trial_name| and |group_name| and
223 // registers an associated |variation_id| for it for the given |service|. 221 // registers an associated |variation_id| for it for the given |service|.
224 void CreateAndActivateFieldTrial(const std::string& trial_name, 222 void CreateAndActivateFieldTrial(const std::string& trial_name,
225 const std::string& group_name, 223 const std::string& group_name,
226 variations::VariationID variation_id, 224 variations::VariationID variation_id,
227 variations::IDCollectionKey service) { 225 variations::IDCollectionKey service) {
(...skipping 10 matching lines...) Expand all
238 void StartRoutingTo(LocalSessionEventHandler* handler) override {} 236 void StartRoutingTo(LocalSessionEventHandler* handler) override {}
239 void Stop() override {} 237 void Stop() override {}
240 }; 238 };
241 239
242 std::unique_ptr<LocalSessionEventRouter> NewDummyRouter() { 240 std::unique_ptr<LocalSessionEventRouter> NewDummyRouter() {
243 return std::unique_ptr<LocalSessionEventRouter>(new DummyRouter()); 241 return std::unique_ptr<LocalSessionEventRouter>(new DummyRouter());
244 } 242 }
245 243
246 // Provides ability to override SyncedWindowDelegatesGetter. 244 // Provides ability to override SyncedWindowDelegatesGetter.
247 // All other calls are passed through to the original SyncSessionsClient. 245 // All other calls are passed through to the original SyncSessionsClient.
248 class SyncSessionsClientShim : public sync_sessions::SyncSessionsClient { 246 class SyncSessionsClientShim : public SyncSessionsClient {
249 public: 247 public:
250 SyncSessionsClientShim( 248 explicit SyncSessionsClientShim(SyncSessionsClient* sync_sessions_client)
251 sync_sessions::SyncSessionsClient* sync_sessions_client)
252 : sync_sessions_client_(sync_sessions_client), 249 : sync_sessions_client_(sync_sessions_client),
253 synced_window_getter_(nullptr) {} 250 synced_window_getter_(nullptr) {}
254 ~SyncSessionsClientShim() override {} 251 ~SyncSessionsClientShim() override {}
255 252
256 bookmarks::BookmarkModel* GetBookmarkModel() override { 253 bookmarks::BookmarkModel* GetBookmarkModel() override {
257 return sync_sessions_client_->GetBookmarkModel(); 254 return sync_sessions_client_->GetBookmarkModel();
258 } 255 }
259 256
260 favicon::FaviconService* GetFaviconService() override { 257 favicon::FaviconService* GetFaviconService() override {
261 return sync_sessions_client_->GetFaviconService(); 258 return sync_sessions_client_->GetFaviconService();
262 } 259 }
263 260
264 history::HistoryService* GetHistoryService() override { 261 history::HistoryService* GetHistoryService() override {
265 return sync_sessions_client_->GetHistoryService(); 262 return sync_sessions_client_->GetHistoryService();
266 } 263 }
267 264
268 bool ShouldSyncURL(const GURL& url) const override { 265 bool ShouldSyncURL(const GURL& url) const override {
269 return sync_sessions_client_->ShouldSyncURL(url); 266 return sync_sessions_client_->ShouldSyncURL(url);
270 } 267 }
271 268
272 browser_sync::SyncedWindowDelegatesGetter* GetSyncedWindowDelegatesGetter() 269 SyncedWindowDelegatesGetter* GetSyncedWindowDelegatesGetter() override {
273 override {
274 // The idea here is to allow the test code override the default 270 // The idea here is to allow the test code override the default
275 // SyncedWindowDelegatesGetter provided by |sync_sessions_client_|. 271 // SyncedWindowDelegatesGetter provided by |sync_sessions_client_|.
276 // If |synced_window_getter_| is explicitly set, return it; otherwise return 272 // If |synced_window_getter_| is explicitly set, return it; otherwise return
277 // the default one provided by |sync_sessions_client_|. 273 // the default one provided by |sync_sessions_client_|.
278 return synced_window_getter_ 274 return synced_window_getter_
279 ? synced_window_getter_ 275 ? synced_window_getter_
280 : sync_sessions_client_->GetSyncedWindowDelegatesGetter(); 276 : sync_sessions_client_->GetSyncedWindowDelegatesGetter();
281 } 277 }
282 278
283 std::unique_ptr<browser_sync::LocalSessionEventRouter> 279 std::unique_ptr<LocalSessionEventRouter> GetLocalSessionEventRouter()
284 GetLocalSessionEventRouter() override { 280 override {
285 return sync_sessions_client_->GetLocalSessionEventRouter(); 281 return sync_sessions_client_->GetLocalSessionEventRouter();
286 } 282 }
287 283
288 void set_synced_window_getter( 284 void set_synced_window_getter(
289 browser_sync::SyncedWindowDelegatesGetter* synced_window_getter) { 285 SyncedWindowDelegatesGetter* synced_window_getter) {
290 synced_window_getter_ = synced_window_getter; 286 synced_window_getter_ = synced_window_getter;
291 } 287 }
292 288
293 private: 289 private:
294 sync_sessions::SyncSessionsClient* const sync_sessions_client_; 290 SyncSessionsClient* const sync_sessions_client_;
295 browser_sync::SyncedWindowDelegatesGetter* synced_window_getter_; 291 SyncedWindowDelegatesGetter* synced_window_getter_;
296 }; 292 };
297 293
298 } // namespace 294 } // namespace
299 295
300 class SessionsSyncManagerTest 296 class SessionsSyncManagerTest
301 : public BrowserWithTestWindowTest { 297 : public BrowserWithTestWindowTest {
302 protected: 298 protected:
303 SessionsSyncManagerTest() 299 SessionsSyncManagerTest()
304 : test_processor_(NULL) { 300 : test_processor_(NULL) {
305 local_device_.reset(new LocalDeviceInfoProviderMock( 301 local_device_.reset(new LocalDeviceInfoProviderMock(
306 "cache_guid", 302 "cache_guid",
307 "Wayne Gretzky's Hacking Box", 303 "Wayne Gretzky's Hacking Box",
308 "Chromium 10k", 304 "Chromium 10k",
309 "Chrome 10k", 305 "Chrome 10k",
310 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, 306 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
311 "device_id")); 307 "device_id"));
312 } 308 }
313 309
314 void SetUp() override { 310 void SetUp() override {
315 BrowserWithTestWindowTest::SetUp(); 311 BrowserWithTestWindowTest::SetUp();
316 sync_client_.reset(new browser_sync::ChromeSyncClient(profile())); 312 sync_client_.reset(new browser_sync::ChromeSyncClient(profile()));
317 sessions_client_shim_.reset( 313 sessions_client_shim_.reset(
318 new SyncSessionsClientShim(sync_client_->GetSyncSessionsClient())); 314 new SyncSessionsClientShim(sync_client_->GetSyncSessionsClient()));
319 browser_sync::NotificationServiceSessionsRouter* router( 315 NotificationServiceSessionsRouter* router(
320 new browser_sync::NotificationServiceSessionsRouter( 316 new NotificationServiceSessionsRouter(
321 profile(), GetSyncSessionsClient(), 317 profile(), GetSyncSessionsClient(),
322 syncer::SyncableService::StartSyncFlare())); 318 syncer::SyncableService::StartSyncFlare()));
323 sync_prefs_.reset(new sync_driver::SyncPrefs(profile()->GetPrefs())); 319 sync_prefs_.reset(new sync_driver::SyncPrefs(profile()->GetPrefs()));
324 manager_.reset(new SessionsSyncManager( 320 manager_.reset(new SessionsSyncManager(
325 GetSyncSessionsClient(), sync_prefs_.get(), local_device_.get(), 321 GetSyncSessionsClient(), sync_prefs_.get(), local_device_.get(),
326 std::unique_ptr<LocalSessionEventRouter>(router), 322 std::unique_ptr<LocalSessionEventRouter>(router),
327 base::Bind(&SessionNotificationObserver::NotifyOfUpdate, 323 base::Bind(&SessionNotificationObserver::NotifyOfUpdate,
328 base::Unretained(&observer_)), 324 base::Unretained(&observer_)),
329 base::Bind(&SessionNotificationObserver::NotifyOfRefresh, 325 base::Bind(&SessionNotificationObserver::NotifyOfRefresh,
330 base::Unretained(&observer_)))); 326 base::Unretained(&observer_))));
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 it = list->erase(it); 381 it = list->erase(it);
386 found = true; 382 found = true;
387 } else { 383 } else {
388 ++it; 384 ++it;
389 } 385 }
390 } 386 }
391 EXPECT_TRUE(found); 387 EXPECT_TRUE(found);
392 return list; 388 return list;
393 } 389 }
394 390
395 sync_sessions::SyncSessionsClient* GetSyncSessionsClient() { 391 SyncSessionsClient* GetSyncSessionsClient() {
396 return sessions_client_shim_.get(); 392 return sessions_client_shim_.get();
397 } 393 }
398 394
399 sync_driver::SyncPrefs* sync_prefs() { return sync_prefs_.get(); } 395 sync_driver::SyncPrefs* sync_prefs() { return sync_prefs_.get(); }
400 396
401 browser_sync::SyncedWindowDelegatesGetter* get_synced_window_getter() { 397 SyncedWindowDelegatesGetter* get_synced_window_getter() {
402 return manager()->synced_window_delegates_getter(); 398 return manager()->synced_window_delegates_getter();
403 } 399 }
404 400
405 void set_synced_window_getter( 401 void set_synced_window_getter(
406 browser_sync::SyncedWindowDelegatesGetter* synced_window_getter) { 402 SyncedWindowDelegatesGetter* synced_window_getter) {
407 sessions_client_shim_->set_synced_window_getter(synced_window_getter); 403 sessions_client_shim_->set_synced_window_getter(synced_window_getter);
408 } 404 }
409 405
410 syncer::SyncChange MakeRemoteChange( 406 syncer::SyncChange MakeRemoteChange(
411 const sync_pb::SessionSpecifics& specifics, 407 const sync_pb::SessionSpecifics& specifics,
412 SyncChange::SyncChangeType type) const { 408 SyncChange::SyncChangeType type) const {
413 return syncer::SyncChange(FROM_HERE, type, CreateRemoteData(specifics)); 409 return syncer::SyncChange(FROM_HERE, type, CreateRemoteData(specifics));
414 } 410 }
415 411
416 void AddTabsToChangeList(const std::vector<sync_pb::SessionSpecifics>& batch, 412 void AddTabsToChangeList(const std::vector<sync_pb::SessionSpecifics>& batch,
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 blocked_navigations_.size(), *entry); 567 blocked_navigations_.size(), *entry);
572 blocked_navigations_.push_back(serialized_entry.release()); 568 blocked_navigations_.push_back(serialized_entry.release());
573 } 569 }
574 } 570 }
575 bool IsPlaceholderTab() const override { return true; } 571 bool IsPlaceholderTab() const override { return true; }
576 572
577 // Session sync related methods. 573 // Session sync related methods.
578 int GetSyncId() const override { return sync_id_; } 574 int GetSyncId() const override { return sync_id_; }
579 void SetSyncId(int sync_id) override { sync_id_ = sync_id; } 575 void SetSyncId(int sync_id) override { sync_id_ = sync_id; }
580 576
581 bool ShouldSync(sync_sessions::SyncSessionsClient* sessions_client) override { 577 bool ShouldSync(SyncSessionsClient* sessions_client) override {
582 return false; 578 return false;
583 } 579 }
584 580
585 void reset() { 581 void reset() {
586 current_entry_index_ = 0; 582 current_entry_index_ = 0;
587 sync_id_ = -1; 583 sync_id_ = -1;
588 entries_.clear(); 584 entries_.clear();
589 } 585 }
590 586
591 private: 587 private:
(...skipping 1182 matching lines...) Expand 10 before | Expand all | Expand 10 after
1774 1, entity, base::Time(), syncer::AttachmentIdList(), 1770 1, entity, base::Time(), syncer::AttachmentIdList(),
1775 syncer::AttachmentServiceProxyForTest::Create(), "bad_tab_tag_hash")); 1771 syncer::AttachmentServiceProxyForTest::Create(), "bad_tab_tag_hash"));
1776 1772
1777 syncer::SyncChangeList output; 1773 syncer::SyncChangeList output;
1778 InitWithSyncDataTakeOutput(foreign_data, &output); 1774 InitWithSyncDataTakeOutput(foreign_data, &output);
1779 ASSERT_EQ(2U, FilterOutLocalHeaderChanges(&output)->size()); 1775 ASSERT_EQ(2U, FilterOutLocalHeaderChanges(&output)->size());
1780 ExpectAllOfChangesType(output, SyncChange::ACTION_DELETE); 1776 ExpectAllOfChangesType(output, SyncChange::ACTION_DELETE);
1781 EXPECT_EQ(1, CountIfTagMatches(output, bad_header_tag)); 1777 EXPECT_EQ(1, CountIfTagMatches(output, bad_header_tag));
1782 EXPECT_EQ(1, CountIfTagMatches(output, bad_tab_tag)); 1778 EXPECT_EQ(1, CountIfTagMatches(output, bad_tab_tag));
1783 1779
1784 std::vector<const sync_driver::SyncedSession*> sessions; 1780 std::vector<const SyncedSession*> sessions;
1785 manager()->session_tracker_.LookupAllForeignSessions( 1781 manager()->session_tracker_.LookupAllForeignSessions(
1786 &sessions, SyncedSessionTracker::RAW); 1782 &sessions, SyncedSessionTracker::RAW);
1787 ASSERT_EQ(2U, sessions.size()); 1783 ASSERT_EQ(2U, sessions.size());
1788 EXPECT_EQ(1, CountIfTagMatches(sessions, good_header_tag)); 1784 EXPECT_EQ(1, CountIfTagMatches(sessions, good_header_tag));
1789 EXPECT_EQ(1, CountIfTagMatches(sessions, good_tag_tab)); 1785 EXPECT_EQ(1, CountIfTagMatches(sessions, good_tag_tab));
1790 } 1786 }
1791 1787
1792 // Test that things work if a tab is initially ignored. 1788 // Test that things work if a tab is initially ignored.
1793 TEST_F(SessionsSyncManagerTest, AssociateWindowsDontReloadTabs) { 1789 TEST_F(SessionsSyncManagerTest, AssociateWindowsDontReloadTabs) {
1794 syncer::SyncChangeList out; 1790 syncer::SyncChangeList out;
(...skipping 804 matching lines...) Expand 10 before | Expand all | Expand 10 after
2599 ASSERT_EQ(tab_list1.size() + tab_list2.size(), tabs.size()); 2595 ASSERT_EQ(tab_list1.size() + tab_list2.size(), tabs.size());
2600 base::Time last_time; 2596 base::Time last_time;
2601 for (size_t i = 0; i < tabs.size(); ++i) { 2597 for (size_t i = 0; i < tabs.size(); ++i) {
2602 base::Time this_time = tabs[i]->timestamp; 2598 base::Time this_time = tabs[i]->timestamp;
2603 if (i > 0) 2599 if (i > 0)
2604 ASSERT_GE(last_time, this_time); 2600 ASSERT_GE(last_time, this_time);
2605 last_time = tabs[i]->timestamp; 2601 last_time = tabs[i]->timestamp;
2606 } 2602 }
2607 } 2603 }
2608 2604
2609 } // namespace browser_sync 2605 } // namespace sync_sessions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698