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

Side by Side Diff: chrome/browser/sync/glue/android_invalidator_bridge_unittest.cc

Issue 13197004: Draft: InvalidationService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Passes tests Created 7 years, 8 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/sync/glue/android_invalidator_bridge.h"
6
7 #include <cstddef>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/compiler_specific.h"
12 #include "base/location.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop.h"
16 #include "base/message_loop_proxy.h"
17 #include "base/run_loop.h"
18 #include "base/sequenced_task_runner.h"
19 #include "base/threading/thread.h"
20 #include "chrome/common/chrome_notification_types.h"
21 #include "chrome/test/base/profile_mock.h"
22 #include "content/public/browser/notification_details.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/test/test_browser_thread.h"
25 #include "sync/internal_api/public/base/model_type.h"
26 #include "sync/internal_api/public/base/model_type_invalidation_map.h"
27 #include "sync/notifier/fake_invalidation_handler.h"
28 #include "sync/notifier/object_id_invalidation_map_test_util.h"
29 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31
32 namespace browser_sync {
33 namespace {
34
35 using ::testing::NiceMock;
36
37 // Since all the interesting stuff happens on the sync thread, we have
38 // to be careful to use GTest/GMock only on the main thread since they
39 // are not thread-safe.
40
41 class AndroidInvalidatorBridgeTest : public testing::Test {
42 public:
43 AndroidInvalidatorBridgeTest()
44 : ui_thread_(content::BrowserThread::UI, &ui_loop_),
45 sync_thread_("Sync thread"),
46 sync_handler_notification_success_(false) {}
47
48 virtual ~AndroidInvalidatorBridgeTest() {}
49
50 protected:
51 virtual void SetUp() OVERRIDE {
52 ASSERT_TRUE(sync_thread_.Start());
53 bridge_.reset(
54 new AndroidInvalidatorBridge(
55 &mock_profile_, sync_thread_.message_loop_proxy()));
56 }
57
58 virtual void TearDown() OVERRIDE {
59 bridge_->StopForShutdown();
60 sync_thread_.Stop();
61 // Must be reset only after the sync thread is stopped.
62 bridge_.reset();
63 EXPECT_EQ(NULL, sync_handler_.get());
64 if (!sync_handler_notification_success_)
65 ADD_FAILURE() << "Sync handler did not receive proper notification.";
66 }
67
68 void VerifyAndDestroyObserver(
69 const syncer::ModelTypeInvalidationMap& expected_invalidations) {
70 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask(
71 FROM_HERE,
72 base::Bind(&AndroidInvalidatorBridgeTest::
73 VerifyAndDestroyObserverOnSyncThread,
74 base::Unretained(this),
75 expected_invalidations)));
76 BlockForSyncThread();
77 }
78
79 void CreateObserver() {
80 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask(
81 FROM_HERE,
82 base::Bind(
83 &AndroidInvalidatorBridgeTest::CreateObserverOnSyncThread,
84 base::Unretained(this))));
85 BlockForSyncThread();
86 }
87
88 void UpdateEnabledTypes(syncer::ModelTypeSet enabled_types) {
89 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask(
90 FROM_HERE,
91 base::Bind(
92 &AndroidInvalidatorBridgeTest::
93 UpdateEnabledTypesOnSyncThread,
94 base::Unretained(this),
95 enabled_types)));
96 BlockForSyncThread();
97 }
98
99 void TriggerRefreshNotification(
100 int type,
101 const syncer::ModelTypeInvalidationMap& invalidation_map) {
102 content::NotificationService::current()->Notify(
103 type,
104 content::Source<Profile>(&mock_profile_),
105 content::Details<const syncer::ModelTypeInvalidationMap>(
106 &invalidation_map));
107 BlockForSyncThread();
108 }
109
110 private:
111 void VerifyAndDestroyObserverOnSyncThread(
112 const syncer::ModelTypeInvalidationMap& expected_invalidations) {
113 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread());
114 if (sync_handler_.get()) {
115 sync_handler_notification_success_ =
116 (sync_handler_->GetInvalidationCount() == 1) &&
117 ObjectIdInvalidationMapEquals(
118 sync_handler_->GetLastInvalidationMap(),
119 syncer::ModelTypeInvalidationMapToObjectIdInvalidationMap(
120 expected_invalidations));
121 bridge_->UnregisterHandler(sync_handler_.get());
122 } else {
123 sync_handler_notification_success_ = false;
124 }
125 sync_handler_.reset();
126 }
127
128 void CreateObserverOnSyncThread() {
129 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread());
130 sync_handler_.reset(new syncer::FakeInvalidationHandler());
131 bridge_->RegisterHandler(sync_handler_.get());
132 }
133
134 void UpdateEnabledTypesOnSyncThread(
135 syncer::ModelTypeSet enabled_types) {
136 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread());
137 bridge_->UpdateRegisteredIds(
138 sync_handler_.get(), ModelTypeSetToObjectIdSet(enabled_types));
139 }
140
141 void BlockForSyncThread() {
142 // Post a task to the sync thread's message loop and block until
143 // it runs.
144 base::RunLoop run_loop;
145 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTaskAndReply(
146 FROM_HERE,
147 base::Bind(&base::DoNothing),
148 run_loop.QuitClosure()));
149 run_loop.Run();
150 }
151
152 MessageLoop ui_loop_;
153 content::TestBrowserThread ui_thread_;
154 base::Thread sync_thread_;
155 NiceMock<ProfileMock> mock_profile_;
156 // Created/used/destroyed on sync thread.
157 scoped_ptr<syncer::FakeInvalidationHandler> sync_handler_;
158 bool sync_handler_notification_success_;
159 scoped_ptr<AndroidInvalidatorBridge> bridge_;
160 };
161
162 // Adds an observer on the sync thread, triggers a remote refresh
163 // invalidation, and ensures the bridge posts a REMOTE_INVALIDATION
164 // with the proper state to it.
165 TEST_F(AndroidInvalidatorBridgeTest, RemoteNotification) {
166 const syncer::ModelTypeSet types(syncer::SESSIONS);
167 const syncer::ModelTypeInvalidationMap& invalidation_map =
168 ModelTypeSetToInvalidationMap(types, std::string());
169 CreateObserver();
170 UpdateEnabledTypes(syncer::ModelTypeSet(syncer::SESSIONS));
171 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE,
172 invalidation_map);
173 VerifyAndDestroyObserver(invalidation_map);
174 }
175
176 // Adds an observer on the sync thread, triggers a remote refresh
177 // notification with empty state map and ensures the bridge posts a
178 // REMOTE_INVALIDATION with the proper state to it.
179 TEST_F(AndroidInvalidatorBridgeTest, RemoteNotificationEmptyPayloadMap) {
180 const syncer::ModelTypeSet enabled_types(
181 syncer::BOOKMARKS, syncer::TYPED_URLS);
182 const syncer::ModelTypeInvalidationMap enabled_types_invalidation_map =
183 syncer::ModelTypeSetToInvalidationMap(enabled_types, std::string());
184 CreateObserver();
185 UpdateEnabledTypes(enabled_types);
186 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE,
187 syncer::ModelTypeInvalidationMap());
188 VerifyAndDestroyObserver(enabled_types_invalidation_map);
189 }
190
191 } // namespace
192 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698