Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / sync / test / fake_server / fake_server_verifier.cc
blobb968d0f919a06c9775d66e8fbf9a9fdf6bf1b7d5
1 // Copyright 2014 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/test/fake_server/fake_server_verifier.h"
7 #include "base/json/json_writer.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "sync/internal_api/public/base/model_type.h"
11 #include "sync/test/fake_server/fake_server.h"
12 #include "sync/test/fake_server/sessions_hierarchy.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using base::JSONWriter;
16 using std::string;
17 using testing::AssertionFailure;
18 using testing::AssertionResult;
19 using testing::AssertionSuccess;
21 namespace fake_server {
23 namespace {
25 AssertionResult DictionaryCreationAssertionFailure() {
26 return AssertionFailure() << "FakeServer failed to create an entities "
27 << "dictionary.";
30 AssertionResult VerificationCountAssertionFailure(size_t actual_count,
31 size_t expected_count) {
32 return AssertionFailure() << "Actual count: " << actual_count << "; "
33 << "Expected count: " << expected_count;
36 AssertionResult UnknownTypeAssertionFailure(const string& model_type) {
37 return AssertionFailure() << "Verification not attempted. Unknown ModelType: "
38 << model_type;
41 AssertionResult VerifySessionsHierarchyEquality(
42 const SessionsHierarchy& expected,
43 const SessionsHierarchy& actual) {
44 if (expected.Equals(actual))
45 return AssertionSuccess() << "Sessions hierarchies are equal.";
47 return AssertionFailure() << "Sessions hierarchies are not equal. "
48 << "FakeServer contents: " << actual.ToString()
49 << "; Expected contents: " << expected.ToString();
52 // Caller maintains ownership of |entities|.
53 string ConvertFakeServerContentsToString(
54 const base::DictionaryValue& entities) {
55 string entities_str;
56 if (!JSONWriter::WriteWithOptions(entities, JSONWriter::OPTIONS_PRETTY_PRINT,
57 &entities_str)) {
58 entities_str = "Could not convert FakeServer contents to string.";
60 return "FakeServer contents:\n" + entities_str;
63 } // namespace
65 FakeServerVerifier::FakeServerVerifier(FakeServer* fake_server)
66 : fake_server_(fake_server) { }
68 FakeServerVerifier::~FakeServerVerifier() {}
70 AssertionResult FakeServerVerifier::VerifyEntityCountByType(
71 size_t expected_count,
72 syncer::ModelType model_type) const {
73 scoped_ptr<base::DictionaryValue> entities =
74 fake_server_->GetEntitiesAsDictionaryValue();
75 if (!entities.get()) {
76 return DictionaryCreationAssertionFailure();
79 string model_type_string = ModelTypeToString(model_type);
80 base::ListValue* entity_list = NULL;
81 if (!entities->GetList(model_type_string, &entity_list)) {
82 return UnknownTypeAssertionFailure(model_type_string);
83 } else if (expected_count != entity_list->GetSize()) {
84 return VerificationCountAssertionFailure(entity_list->GetSize(),
85 expected_count)
86 << "\n\n"
87 << ConvertFakeServerContentsToString(*entities);
90 return AssertionSuccess();
93 AssertionResult FakeServerVerifier::VerifyEntityCountByTypeAndName(
94 size_t expected_count,
95 syncer::ModelType model_type,
96 const string& name) const {
97 scoped_ptr<base::DictionaryValue> entities =
98 fake_server_->GetEntitiesAsDictionaryValue();
99 if (!entities.get()) {
100 return DictionaryCreationAssertionFailure();
103 string model_type_string = ModelTypeToString(model_type);
104 base::ListValue* entity_list = NULL;
105 size_t actual_count = 0;
106 if (entities->GetList(model_type_string, &entity_list)) {
107 scoped_ptr<base::Value> name_value(new base::StringValue(name));
108 for (base::ListValue::const_iterator it = entity_list->begin();
109 it != entity_list->end(); ++it) {
110 if (name_value->Equals(*it)) {
111 actual_count++;
116 if (!entity_list) {
117 return UnknownTypeAssertionFailure(model_type_string);
118 } else if (actual_count != expected_count) {
119 return VerificationCountAssertionFailure(actual_count, expected_count)
120 << "; Name: "
121 << name
122 << "\n\n"
123 << ConvertFakeServerContentsToString(*entities);
126 return AssertionSuccess();
129 AssertionResult FakeServerVerifier::VerifySessions(
130 const SessionsHierarchy& expected_sessions) {
131 std::vector<sync_pb::SyncEntity> sessions =
132 fake_server_->GetSyncEntitiesByModelType(syncer::SESSIONS);
133 // Look for the sessions entity containing a SessionHeader and cache all tab
134 // IDs/URLs. These will be used later to construct a SessionsHierarchy.
135 sync_pb::SessionHeader session_header;
136 std::map<int, int> tab_ids_to_window_ids;
137 std::map<int, std::string> tab_ids_to_urls;
138 std::string session_tag;
139 for (std::vector<sync_pb::SyncEntity>::const_iterator it = sessions.begin();
140 it != sessions.end(); ++it) {
141 sync_pb::SyncEntity entity = *it;
142 sync_pb::SessionSpecifics session_specifics = entity.specifics().session();
144 // Ensure that all session tags match the first entity. Only one session is
145 // supported for verification at this time.
146 if (it == sessions.begin())
147 session_tag = session_specifics.session_tag();
148 else if (session_specifics.session_tag() != session_tag)
149 return AssertionFailure() << "Multiple session tags found.";
151 if (session_specifics.has_header()) {
152 session_header = session_specifics.header();
153 } else if (session_specifics.has_tab()) {
154 sync_pb::SessionTab tab = session_specifics.tab();
155 tab_ids_to_window_ids[tab.tab_id()] = tab.window_id();
156 tab_ids_to_urls[tab.tab_id()] =
157 tab.navigation(tab.current_navigation_index()).virtual_url();
161 // Create a SessionsHierarchy from the cached SyncEntity data. This loop over
162 // the SessionHeader also ensures its data corresponds to the data stored in
163 // each SessionTab.
164 SessionsHierarchy actual_sessions;
165 ::google::protobuf::RepeatedPtrField<sync_pb::SessionWindow>::const_iterator
166 window_it;
167 for (window_it = session_header.window().begin();
168 window_it != session_header.window().end(); ++window_it) {
169 sync_pb::SessionWindow window = *window_it;
170 std::multiset<std::string> tab_urls;
171 ::google::protobuf::RepeatedField<int>::const_iterator tab_it;
172 for (tab_it = window.tab().begin(); tab_it != window.tab().end();
173 ++tab_it) {
174 int tab_id = *tab_it;
175 if (tab_ids_to_window_ids.find(tab_id) == tab_ids_to_window_ids.end()) {
176 return AssertionFailure() << "Malformed data: Tab entity not found.";
178 tab_urls.insert(tab_ids_to_urls[tab_id]);
180 actual_sessions.AddWindow(tab_urls);
182 return VerifySessionsHierarchyEquality(expected_sessions, actual_sessions);
185 } // namespace fake_server