1 // Copyright (c) 2011 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/sessions/session_state.h"
6
7 #include <string>
8
9 #include "base/base64.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/values.h"
12 #include "chrome/test/values_test_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace browser_sync {
16 namespace sessions {
17 namespace {
18
19 using test::ExpectBooleanValue;
20 using test::ExpectDictionaryValue;
21 using test::ExpectIntegerValue;
22 using test::ExpectListValue;
23 using test::ExpectStringValue;
24
25 class SessionStateTest : public testing::Test {};
26
TEST_F(SessionStateTest,SyncSourceInfoToValue)27 TEST_F(SessionStateTest, SyncSourceInfoToValue) {
28 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource updates_source =
29 sync_pb::GetUpdatesCallerInfo::PERIODIC;
30 syncable::ModelTypePayloadMap types;
31 types[syncable::PREFERENCES] = "preferencespayload";
32 types[syncable::EXTENSIONS] = "";
33 scoped_ptr<DictionaryValue> expected_types_value(
34 syncable::ModelTypePayloadMapToValue(types));
35
36 SyncSourceInfo source_info(updates_source, types);
37
38 scoped_ptr<DictionaryValue> value(source_info.ToValue());
39 EXPECT_EQ(2u, value->size());
40 ExpectStringValue("PERIODIC", *value, "updatesSource");
41 ExpectDictionaryValue(*expected_types_value, *value, "types");
42 }
43
TEST_F(SessionStateTest,SyncerStatusToValue)44 TEST_F(SessionStateTest, SyncerStatusToValue) {
45 SyncerStatus status;
46 status.invalid_store = true;
47 status.syncer_stuck = false;
48 status.syncing = true;
49 status.num_successful_commits = 5;
50 status.num_successful_bookmark_commits = 10;
51 status.num_updates_downloaded_total = 100;
52 status.num_tombstone_updates_downloaded_total = 200;
53
54 scoped_ptr<DictionaryValue> value(status.ToValue());
55 EXPECT_EQ(7u, value->size());
56 ExpectBooleanValue(status.invalid_store, *value, "invalidStore");
57 ExpectBooleanValue(status.syncer_stuck, *value, "syncerStuck");
58 ExpectBooleanValue(status.syncing, *value, "syncing");
59 ExpectIntegerValue(status.num_successful_commits,
60 *value, "numSuccessfulCommits");
61 ExpectIntegerValue(status.num_successful_bookmark_commits,
62 *value, "numSuccessfulBookmarkCommits");
63 ExpectIntegerValue(status.num_updates_downloaded_total,
64 *value, "numUpdatesDownloadedTotal");
65 ExpectIntegerValue(status.num_tombstone_updates_downloaded_total,
66 *value, "numTombstoneUpdatesDownloadedTotal");
67 }
68
TEST_F(SessionStateTest,ErrorCountersToValue)69 TEST_F(SessionStateTest, ErrorCountersToValue) {
70 ErrorCounters counters;
71 counters.num_conflicting_commits = 1;
72 counters.consecutive_transient_error_commits = 5;
73 counters.consecutive_errors = 3;
74
75 scoped_ptr<DictionaryValue> value(counters.ToValue());
76 EXPECT_EQ(3u, value->size());
77 ExpectIntegerValue(counters.num_conflicting_commits,
78 *value, "numConflictingCommits");
79 ExpectIntegerValue(counters.consecutive_transient_error_commits,
80 *value, "consecutiveTransientErrorCommits");
81 ExpectIntegerValue(counters.consecutive_errors,
82 *value, "consecutiveErrors");
83 }
84
TEST_F(SessionStateTest,DownloadProgressMarkersToValue)85 TEST_F(SessionStateTest, DownloadProgressMarkersToValue) {
86 std::string download_progress_markers[syncable::MODEL_TYPE_COUNT];
87 for (int i = syncable::FIRST_REAL_MODEL_TYPE;
88 i < syncable::MODEL_TYPE_COUNT; ++i) {
89 std::string marker(i, i);
90 download_progress_markers[i] = marker;
91 }
92
93 scoped_ptr<DictionaryValue> value(
94 DownloadProgressMarkersToValue(download_progress_markers));
95 EXPECT_EQ(syncable::MODEL_TYPE_COUNT - syncable::FIRST_REAL_MODEL_TYPE,
96 static_cast<int>(value->size()));
97 for (int i = syncable::FIRST_REAL_MODEL_TYPE;
98 i < syncable::MODEL_TYPE_COUNT; ++i) {
99 syncable::ModelType model_type = syncable::ModelTypeFromInt(i);
100 std::string marker(i, i);
101 std::string expected_value;
102 EXPECT_TRUE(base::Base64Encode(marker, &expected_value));
103 ExpectStringValue(expected_value,
104 *value, syncable::ModelTypeToString(model_type));
105 }
106 }
107
TEST_F(SessionStateTest,SyncSessionSnapshotToValue)108 TEST_F(SessionStateTest, SyncSessionSnapshotToValue) {
109 SyncerStatus syncer_status;
110 syncer_status.num_successful_commits = 500;
111 scoped_ptr<DictionaryValue> expected_syncer_status_value(
112 syncer_status.ToValue());
113
114 ErrorCounters errors;
115 errors.num_conflicting_commits = 250;
116 scoped_ptr<DictionaryValue> expected_errors_value(
117 errors.ToValue());
118
119 const int kNumServerChangesRemaining = 105;
120 const bool kIsShareUsable = true;
121
122 syncable::ModelTypeBitSet initial_sync_ended;
123 initial_sync_ended.set(syncable::BOOKMARKS);
124 initial_sync_ended.set(syncable::PREFERENCES);
125 scoped_ptr<ListValue> expected_initial_sync_ended_value(
126 syncable::ModelTypeBitSetToValue(initial_sync_ended));
127
128 std::string download_progress_markers[syncable::MODEL_TYPE_COUNT];
129 download_progress_markers[syncable::BOOKMARKS] = "test";
130 download_progress_markers[syncable::APPS] = "apps";
131 scoped_ptr<DictionaryValue> expected_download_progress_markers_value(
132 DownloadProgressMarkersToValue(download_progress_markers));
133
134 const bool kHasMoreToSync = false;
135 const bool kIsSilenced = true;
136 const int kUnsyncedCount = 1053;
137 const int kNumConflictingUpdates = 1055;
138 const bool kDidCommitItems = true;
139
140 SyncSourceInfo source;
141 scoped_ptr<DictionaryValue> expected_source_value(source.ToValue());
142
143 SyncSessionSnapshot snapshot(syncer_status,
144 errors,
145 kNumServerChangesRemaining,
146 kIsShareUsable,
147 initial_sync_ended,
148 download_progress_markers,
149 kHasMoreToSync,
150 kIsSilenced,
151 kUnsyncedCount,
152 kNumConflictingUpdates,
153 kDidCommitItems,
154 source);
155 scoped_ptr<DictionaryValue> value(snapshot.ToValue());
156 EXPECT_EQ(12u, value->size());
157 ExpectDictionaryValue(*expected_syncer_status_value, *value,
158 "syncerStatus");
159 ExpectDictionaryValue(*expected_errors_value, *value, "errors");
160 ExpectIntegerValue(kNumServerChangesRemaining, *value,
161 "numServerChangesRemaining");
162 ExpectBooleanValue(kIsShareUsable, *value, "isShareUsable");
163 ExpectListValue(*expected_initial_sync_ended_value, *value,
164 "initialSyncEnded");
165 ExpectDictionaryValue(*expected_download_progress_markers_value,
166 *value, "downloadProgressMarkers");
167 ExpectBooleanValue(kHasMoreToSync, *value, "hasMoreToSync");
168 ExpectBooleanValue(kIsSilenced, *value, "isSilenced");
169 ExpectIntegerValue(kUnsyncedCount, *value, "unsyncedCount");
170 ExpectIntegerValue(kNumConflictingUpdates, *value,
171 "numConflictingUpdates");
172 ExpectBooleanValue(kDidCommitItems, *value,
173 "didCommitItems");
174 ExpectDictionaryValue(*expected_source_value, *value, "source");
175 }
176
177 } // namespace
178 } // namespace sessions
179 } // namespace browser_sync
180