1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/glue/chrome_sync_notification_bridge.h"
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "base/sequenced_task_runner.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/test/base/profile_mock.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/test_browser_thread.h"
#include "sync/internal_api/public/base/model_type.h"
#include "sync/internal_api/public/base/model_type_payload_map.h"
#include "sync/notifier/sync_notifier_observer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace browser_sync {
namespace {
using ::testing::Mock;
using ::testing::NiceMock;
using ::testing::StrictMock;
using content::BrowserThread;
// Receives a ChromeSyncNotificationBridge to register to, and an expected
// ModelTypePayloadMap. ReceivedProperNotification() will return true only
// if the observer has received a notification with the proper source and
// payload.
// Note: Because this object lives on the sync thread, we use a fake
// (vs a mock) so we don't have to worry about possible thread safety
// issues within GTest/GMock.
class FakeSyncNotifierObserver : public syncer::SyncNotifierObserver {
public:
FakeSyncNotifierObserver(
const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner,
ChromeSyncNotificationBridge* bridge,
const syncer::ObjectIdPayloadMap& expected_payloads,
syncer::IncomingNotificationSource expected_source)
: sync_task_runner_(sync_task_runner),
bridge_(bridge),
received_improper_notification_(false),
notification_count_(0),
expected_payloads_(expected_payloads),
expected_source_(expected_source) {
DCHECK(sync_task_runner_->RunsTasksOnCurrentThread());
const syncer::ObjectIdSet& ids =
syncer::ObjectIdPayloadMapToSet(expected_payloads);
bridge_->UpdateRegisteredIds(this, ids);
}
virtual ~FakeSyncNotifierObserver() {
DCHECK(sync_task_runner_->RunsTasksOnCurrentThread());
bridge_->UpdateRegisteredIds(this, syncer::ObjectIdSet());
}
// SyncNotifierObserver implementation.
virtual void OnIncomingNotification(
const syncer::ObjectIdPayloadMap& id_payloads,
syncer::IncomingNotificationSource source) OVERRIDE {
DCHECK(sync_task_runner_->RunsTasksOnCurrentThread());
notification_count_++;
if (source != expected_source_) {
LOG(ERROR) << "Received notification with wrong source";
received_improper_notification_ = true;
}
if (expected_payloads_ != id_payloads) {
LOG(ERROR) << "Received wrong payload";
received_improper_notification_ = true;
}
}
virtual void OnNotificationsEnabled() OVERRIDE {
NOTREACHED();
}
virtual void OnNotificationsDisabled(
syncer::NotificationsDisabledReason reason) OVERRIDE {
NOTREACHED();
}
bool ReceivedProperNotification() const {
DCHECK(sync_task_runner_->RunsTasksOnCurrentThread());
return (notification_count_ == 1) && !received_improper_notification_;
}
private:
const scoped_refptr<base::SequencedTaskRunner> sync_task_runner_;
ChromeSyncNotificationBridge* const bridge_;
bool received_improper_notification_;
size_t notification_count_;
const syncer::ObjectIdPayloadMap expected_payloads_;
const syncer::IncomingNotificationSource expected_source_;
};
class ChromeSyncNotificationBridgeTest : public testing::Test {
public:
ChromeSyncNotificationBridgeTest()
: ui_thread_(BrowserThread::UI),
sync_thread_("Sync thread"),
sync_observer_(NULL),
sync_observer_notification_failure_(false),
done_(true, false) {}
virtual ~ChromeSyncNotificationBridgeTest() {}
protected:
virtual void SetUp() OVERRIDE {
ASSERT_TRUE(sync_thread_.Start());
bridge_.reset(
new ChromeSyncNotificationBridge(
&mock_profile_, sync_thread_.message_loop_proxy()));
}
virtual void TearDown() OVERRIDE {
sync_thread_.Stop();
// Must be reset only after the sync thread is stopped.
bridge_.reset();
EXPECT_EQ(NULL, sync_observer_);
if (sync_observer_notification_failure_)
ADD_FAILURE() << "Sync Observer did not receive proper notification.";
}
void VerifyAndDestroyObserver() {
ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask(
FROM_HERE,
base::Bind(&ChromeSyncNotificationBridgeTest::
VerifyAndDestroyObserverOnSyncThread,
base::Unretained(this))));
BlockForSyncThread();
}
void CreateObserverWithExpectations(
const syncer::ModelTypePayloadMap& expected_payloads,
syncer::IncomingNotificationSource expected_source) {
const syncer::ObjectIdPayloadMap& expected_id_payloads =
syncer::ModelTypePayloadMapToObjectIdPayloadMap(expected_payloads);
ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask(
FROM_HERE,
base::Bind(
&ChromeSyncNotificationBridgeTest::CreateObserverOnSyncThread,
base::Unretained(this),
expected_id_payloads,
expected_source)));
BlockForSyncThread();
}
void UpdateBridgeEnabledTypes(syncer::ModelTypeSet enabled_types) {
ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask(
FROM_HERE,
base::Bind(
&ChromeSyncNotificationBridgeTest::
UpdateBridgeEnabledTypesOnSyncThread,
base::Unretained(this),
enabled_types)));
BlockForSyncThread();
}
void TriggerRefreshNotification(
int type,
const syncer::ModelTypePayloadMap& payload_map) {
content::NotificationService::current()->Notify(
type,
content::Source<Profile>(&mock_profile_),
content::Details<const syncer::ModelTypePayloadMap>(&payload_map));
}
private:
void VerifyAndDestroyObserverOnSyncThread() {
DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread());
if (!sync_observer_) {
sync_observer_notification_failure_ = true;
} else {
sync_observer_notification_failure_ =
!sync_observer_->ReceivedProperNotification();
delete sync_observer_;
sync_observer_ = NULL;
}
}
void CreateObserverOnSyncThread(
const syncer::ObjectIdPayloadMap& expected_payloads,
syncer::IncomingNotificationSource expected_source) {
DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread());
sync_observer_ = new FakeSyncNotifierObserver(
sync_thread_.message_loop_proxy(),
bridge_.get(),
expected_payloads,
expected_source);
}
void UpdateBridgeEnabledTypesOnSyncThread(
syncer::ModelTypeSet enabled_types) {
DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread());
bridge_->UpdateEnabledTypes(enabled_types);
}
void SignalOnSyncThread() {
DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread());
done_.Signal();
}
void BlockForSyncThread() {
done_.Reset();
ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask(
FROM_HERE,
base::Bind(&ChromeSyncNotificationBridgeTest::SignalOnSyncThread,
base::Unretained(this))));
done_.TimedWait(TestTimeouts::action_timeout());
if (!done_.IsSignaled())
ADD_FAILURE() << "Timed out waiting for sync thread.";
}
content::TestBrowserThread ui_thread_;
base::Thread sync_thread_;
NiceMock<ProfileMock> mock_profile_;
// Created/used/destroyed on sync thread.
FakeSyncNotifierObserver* sync_observer_;
bool sync_observer_notification_failure_;
scoped_ptr<ChromeSyncNotificationBridge> bridge_;
base::WaitableEvent done_;
};
// Adds an observer on the sync thread, triggers a local refresh
// notification, and ensures the bridge posts a LOCAL_NOTIFICATION
// with the proper payload to it.
TEST_F(ChromeSyncNotificationBridgeTest, LocalNotification) {
syncer::ModelTypePayloadMap payload_map;
payload_map[syncer::SESSIONS] = "";
CreateObserverWithExpectations(payload_map, syncer::LOCAL_NOTIFICATION);
TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL,
payload_map);
VerifyAndDestroyObserver();
}
// Adds an observer on the sync thread, triggers a remote refresh
// notification, and ensures the bridge posts a REMOTE_NOTIFICATION
// with the proper payload to it.
TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotification) {
syncer::ModelTypePayloadMap payload_map;
payload_map[syncer::SESSIONS] = "";
CreateObserverWithExpectations(payload_map, syncer::REMOTE_NOTIFICATION);
TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE,
payload_map);
VerifyAndDestroyObserver();
}
// Adds an observer on the sync thread, triggers a local refresh
// notification with empty payload map and ensures the bridge posts a
// LOCAL_NOTIFICATION with the proper payload to it.
TEST_F(ChromeSyncNotificationBridgeTest, LocalNotificationEmptyPayloadMap) {
const syncer::ModelTypeSet enabled_types(
syncer::BOOKMARKS, syncer::PASSWORDS);
const syncer::ModelTypePayloadMap enabled_types_payload_map =
syncer::ModelTypePayloadMapFromEnumSet(enabled_types, std::string());
CreateObserverWithExpectations(
enabled_types_payload_map, syncer::LOCAL_NOTIFICATION);
UpdateBridgeEnabledTypes(enabled_types);
TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL,
syncer::ModelTypePayloadMap());
VerifyAndDestroyObserver();
}
// Adds an observer on the sync thread, triggers a remote refresh
// notification with empty payload map and ensures the bridge posts a
// REMOTE_NOTIFICATION with the proper payload to it.
TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotificationEmptyPayloadMap) {
const syncer::ModelTypeSet enabled_types(
syncer::BOOKMARKS, syncer::TYPED_URLS);
const syncer::ModelTypePayloadMap enabled_types_payload_map =
syncer::ModelTypePayloadMapFromEnumSet(enabled_types, std::string());
CreateObserverWithExpectations(
enabled_types_payload_map, syncer::REMOTE_NOTIFICATION);
UpdateBridgeEnabledTypes(enabled_types);
TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE,
syncer::ModelTypePayloadMap());
VerifyAndDestroyObserver();
}
} // namespace
} // namespace browser_sync
|