- add sources.
[platform/framework/web/crosswalk.git] / src / sync / engine / download_unittest.cc
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.
4
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"
11
12 namespace syncer {
13
14 // A test fixture for tests exercising download updates functions.
15 class DownloadUpdatesTest : public SyncerCommandTest {
16  protected:
17   DownloadUpdatesTest() {
18   }
19
20   virtual void SetUp() {
21     workers()->clear();
22     mutable_routing_info()->clear();
23     workers()->push_back(
24         make_scoped_refptr(new FakeModelWorker(GROUP_DB)));
25     workers()->push_back(
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();
31   }
32
33  private:
34   DISALLOW_COPY_AND_ASSIGN(DownloadUpdatesTest);
35 };
36
37 TEST_F(DownloadUpdatesTest, ExecuteNoStates) {
38   sessions::NudgeTracker nudge_tracker;
39   nudge_tracker.RecordLocalChange(ModelTypeSet(BOOKMARKS));
40
41   scoped_ptr<sessions::SyncSession> session(
42       sessions::SyncSession::Build(context(), delegate()));
43   sync_pb::ClientToServerMessage msg;
44   BuildNormalDownloadUpdates(session.get(),
45                              false,
46                              GetRoutingInfoTypes(routing_info()),
47                              nudge_tracker,
48                              &msg);
49
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));
58
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();
63
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());
71     } else {
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());
75     }
76   }
77 }
78
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);
91
92   scoped_ptr<sessions::SyncSession> session(
93       sessions::SyncSession::Build(context(), delegate()));
94   sync_pb::ClientToServerMessage msg;
95   BuildNormalDownloadUpdates(session.get(),
96                              false,
97                              GetRoutingInfoTypes(routing_info()),
98                              nudge_tracker,
99                              &msg);
100
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));
109
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();
114
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());
121     } else {
122       EXPECT_FALSE(progress_marker.has_notification_hint());
123       EXPECT_EQ(0, gu_trigger.notification_hint_size());
124     }
125   }
126 }
127
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));
133
134   sync_pb::ClientToServerMessage msg1;
135   scoped_ptr<sessions::SyncSession> session1(
136       sessions::SyncSession::Build(context(), delegate()));
137   BuildNormalDownloadUpdates(session1.get(),
138                              false,
139                              GetRoutingInfoTypes(routing_info()),
140                              nudge_tracker,
141                              &msg1);
142   EXPECT_EQ(0, msg1.debug_info().events_size());
143
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(),
153                              false,
154                              GetRoutingInfoTypes(routing_info()),
155                              nudge_tracker,
156                              &msg2);
157   EXPECT_EQ(1, msg2.debug_info().events_size());
158
159   // Events should never be sent up more than once per session.
160   sync_pb::ClientToServerMessage msg3;
161   BuildNormalDownloadUpdates(session2.get(),
162                              false,
163                              GetRoutingInfoTypes(routing_info()),
164                              nudge_tracker,
165                              &msg3);
166   EXPECT_EQ(0, msg3.debug_info().events_size());
167 }
168
169 }  // namespace syncer