1 // Copyright 2013 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.
5 #include "sync/engine/download.h"
6 #include "sync/internal_api/public/base/model_type_test_util.h"
7 #include "sync/protocol/sync.pb.h"
8 #include "sync/sessions/nudge_tracker.h"
9 #include "sync/test/engine/fake_model_worker.h"
10 #include "sync/test/engine/syncer_command_test.h"
14 // A test fixture for tests exercising download updates functions.
15 class DownloadUpdatesTest
: public SyncerCommandTest
{
17 DownloadUpdatesTest() {
20 virtual void SetUp() {
22 mutable_routing_info()->clear();
24 make_scoped_refptr(new FakeModelWorker(GROUP_DB
)));
26 make_scoped_refptr(new FakeModelWorker(GROUP_UI
)));
27 (*mutable_routing_info())[AUTOFILL
] = GROUP_DB
;
28 (*mutable_routing_info())[BOOKMARKS
] = GROUP_UI
;
29 (*mutable_routing_info())[PREFERENCES
] = GROUP_UI
;
30 SyncerCommandTest::SetUp();
34 DISALLOW_COPY_AND_ASSIGN(DownloadUpdatesTest
);
37 TEST_F(DownloadUpdatesTest
, ExecuteNoStates
) {
38 sessions::NudgeTracker nudge_tracker
;
39 nudge_tracker
.RecordLocalChange(ModelTypeSet(BOOKMARKS
));
41 scoped_ptr
<sessions::SyncSession
> session(
42 sessions::SyncSession::Build(context(), delegate()));
43 sync_pb::ClientToServerMessage msg
;
44 BuildNormalDownloadUpdates(session
.get(),
46 GetRoutingInfoTypes(routing_info()),
50 const sync_pb::GetUpdatesMessage
& gu_msg
= msg
.get_updates();
51 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::LOCAL
,
52 gu_msg
.caller_info().source());
53 EXPECT_EQ(sync_pb::SyncEnums::GU_TRIGGER
, gu_msg
.get_updates_origin());
54 for (int i
= 0; i
< gu_msg
.from_progress_marker_size(); ++i
) {
55 syncer::ModelType type
= GetModelTypeFromSpecificsFieldNumber(
56 gu_msg
.from_progress_marker(i
).data_type_id());
57 EXPECT_TRUE(GetRoutingInfoTypes(routing_info()).Has(type
));
59 const sync_pb::DataTypeProgressMarker
& progress_marker
=
60 gu_msg
.from_progress_marker(i
);
61 const sync_pb::GetUpdateTriggers
& gu_trigger
=
62 progress_marker
.get_update_triggers();
64 // We perform some basic tests of GU trigger and source fields here. The
65 // more complicated scenarios are tested by the NudgeTracker tests.
66 if (type
== BOOKMARKS
) {
67 EXPECT_TRUE(progress_marker
.has_notification_hint());
68 EXPECT_EQ("", progress_marker
.notification_hint());
69 EXPECT_EQ(1, gu_trigger
.local_modification_nudges());
70 EXPECT_EQ(0, gu_trigger
.datatype_refresh_nudges());
72 EXPECT_FALSE(progress_marker
.has_notification_hint());
73 EXPECT_EQ(0, gu_trigger
.local_modification_nudges());
74 EXPECT_EQ(0, gu_trigger
.datatype_refresh_nudges());
79 TEST_F(DownloadUpdatesTest
, ExecuteWithStates
) {
80 sessions::NudgeTracker nudge_tracker
;
81 nudge_tracker
.RecordRemoteInvalidation(
82 BuildInvalidationMap(AUTOFILL
, 1, "autofill_payload"));
83 nudge_tracker
.RecordRemoteInvalidation(
84 BuildInvalidationMap(BOOKMARKS
, 1, "bookmark_payload"));
85 nudge_tracker
.RecordRemoteInvalidation(
86 BuildInvalidationMap(PREFERENCES
, 1, "preferences_payload"));
87 ModelTypeSet notified_types
;
88 notified_types
.Put(AUTOFILL
);
89 notified_types
.Put(BOOKMARKS
);
90 notified_types
.Put(PREFERENCES
);
92 scoped_ptr
<sessions::SyncSession
> session(
93 sessions::SyncSession::Build(context(), delegate()));
94 sync_pb::ClientToServerMessage msg
;
95 BuildNormalDownloadUpdates(session
.get(),
97 GetRoutingInfoTypes(routing_info()),
101 const sync_pb::GetUpdatesMessage
& gu_msg
= msg
.get_updates();
102 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::NOTIFICATION
,
103 gu_msg
.caller_info().source());
104 EXPECT_EQ(sync_pb::SyncEnums::GU_TRIGGER
, gu_msg
.get_updates_origin());
105 for (int i
= 0; i
< gu_msg
.from_progress_marker_size(); ++i
) {
106 syncer::ModelType type
= GetModelTypeFromSpecificsFieldNumber(
107 gu_msg
.from_progress_marker(i
).data_type_id());
108 EXPECT_TRUE(GetRoutingInfoTypes(routing_info()).Has(type
));
110 const sync_pb::DataTypeProgressMarker
& progress_marker
=
111 gu_msg
.from_progress_marker(i
);
112 const sync_pb::GetUpdateTriggers
& gu_trigger
=
113 progress_marker
.get_update_triggers();
115 // We perform some basic tests of GU trigger and source fields here. The
116 // more complicated scenarios are tested by the NudgeTracker tests.
117 if (notified_types
.Has(type
)) {
118 EXPECT_TRUE(progress_marker
.has_notification_hint());
119 EXPECT_FALSE(progress_marker
.notification_hint().empty());
120 EXPECT_EQ(1, gu_trigger
.notification_hint_size());
122 EXPECT_FALSE(progress_marker
.has_notification_hint());
123 EXPECT_EQ(0, gu_trigger
.notification_hint_size());
128 // Test that debug info is sent uploaded only once per sync session.
129 TEST_F(DownloadUpdatesTest
, VerifyAppendDebugInfo
) {
130 // Start by expecting that no events are uploaded.
131 sessions::NudgeTracker nudge_tracker
;
132 nudge_tracker
.RecordLocalChange(ModelTypeSet(BOOKMARKS
));
134 sync_pb::ClientToServerMessage msg1
;
135 scoped_ptr
<sessions::SyncSession
> session1(
136 sessions::SyncSession::Build(context(), delegate()));
137 BuildNormalDownloadUpdates(session1
.get(),
139 GetRoutingInfoTypes(routing_info()),
142 EXPECT_EQ(0, msg1
.debug_info().events_size());
144 // Create a new session, record an event, and try again.
145 scoped_ptr
<sessions::SyncSession
> session2(
146 sessions::SyncSession::Build(context(), delegate()));
147 DataTypeConfigurationStats stats
;
148 stats
.model_type
= BOOKMARKS
;
149 debug_info_event_listener()->OnDataTypeConfigureComplete(
150 std::vector
<DataTypeConfigurationStats
>(1, stats
));
151 sync_pb::ClientToServerMessage msg2
;
152 BuildNormalDownloadUpdates(session2
.get(),
154 GetRoutingInfoTypes(routing_info()),
157 EXPECT_EQ(1, msg2
.debug_info().events_size());
159 // Events should never be sent up more than once per session.
160 sync_pb::ClientToServerMessage msg3
;
161 BuildNormalDownloadUpdates(session2
.get(),
163 GetRoutingInfoTypes(routing_info()),
166 EXPECT_EQ(0, msg3
.debug_info().events_size());
169 } // namespace syncer