OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/test/fake_server/fake_server_verifier.h" | 5 #include "sync/test/fake_server/fake_server_verifier.h" |
6 | 6 |
| 7 #include "base/json/json_writer.h" |
7 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
8 #include "base/values.h" | 9 #include "base/values.h" |
9 #include "sync/internal_api/public/base/model_type.h" | 10 #include "sync/internal_api/public/base/model_type.h" |
10 #include "sync/test/fake_server/fake_server.h" | 11 #include "sync/test/fake_server/fake_server.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
| 14 using base::JSONWriter; |
13 using std::string; | 15 using std::string; |
14 using testing::AssertionFailure; | 16 using testing::AssertionFailure; |
15 using testing::AssertionResult; | 17 using testing::AssertionResult; |
16 using testing::AssertionSuccess; | 18 using testing::AssertionSuccess; |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 AssertionResult DictionaryCreationAssertionFailure() { | 22 AssertionResult DictionaryCreationAssertionFailure() { |
21 return AssertionFailure() << "FakeServer failed to create an entities " | 23 return AssertionFailure() << "FakeServer failed to create an entities " |
22 << "dictionary."; | 24 << "dictionary."; |
23 } | 25 } |
24 | 26 |
25 AssertionResult VerificationCountAssertionFailure(size_t actual_count, | 27 AssertionResult VerificationCountAssertionFailure(size_t actual_count, |
26 size_t expected_count) { | 28 size_t expected_count) { |
27 return AssertionFailure() << "Actual count: " << actual_count << "; " | 29 return AssertionFailure() << "Actual count: " << actual_count << "; " |
28 << "Expected count: " << expected_count; | 30 << "Expected count: " << expected_count; |
29 } | 31 } |
30 | 32 |
31 AssertionResult UnknownTypeAssertionFailure(const string& model_type) { | 33 AssertionResult UnknownTypeAssertionFailure(const string& model_type) { |
32 return AssertionFailure() << "Verification not attempted. Unknown ModelType: " | 34 return AssertionFailure() << "Verification not attempted. Unknown ModelType: " |
33 << model_type; | 35 << model_type; |
34 } | 36 } |
35 | 37 |
| 38 // Caller maintains ownership of |entities|. |
| 39 string ConvertFakeServerContentsToString( |
| 40 const base::DictionaryValue& entities) { |
| 41 string entities_str; |
| 42 if (!JSONWriter::WriteWithOptions(&entities, |
| 43 JSONWriter::OPTIONS_PRETTY_PRINT, |
| 44 &entities_str)) { |
| 45 entities_str = "Could not convert FakeServer contents to string."; |
| 46 } |
| 47 return "FakeServer contents:\n" + entities_str; |
| 48 } |
| 49 |
36 } // namespace | 50 } // namespace |
37 | 51 |
38 namespace fake_server { | 52 namespace fake_server { |
39 | 53 |
40 FakeServerVerifier::FakeServerVerifier(FakeServer* fake_server) | 54 FakeServerVerifier::FakeServerVerifier(FakeServer* fake_server) |
41 : fake_server_(fake_server) { } | 55 : fake_server_(fake_server) { } |
42 | 56 |
43 FakeServerVerifier::~FakeServerVerifier() {} | 57 FakeServerVerifier::~FakeServerVerifier() {} |
44 | 58 |
45 AssertionResult FakeServerVerifier::VerifyEntityCountByType( | 59 AssertionResult FakeServerVerifier::VerifyEntityCountByType( |
46 size_t expected_count, | 60 size_t expected_count, |
47 syncer::ModelType model_type) const { | 61 syncer::ModelType model_type) const { |
48 scoped_ptr<base::DictionaryValue> entities = | 62 scoped_ptr<base::DictionaryValue> entities = |
49 fake_server_->GetEntitiesAsDictionaryValue(); | 63 fake_server_->GetEntitiesAsDictionaryValue(); |
50 if (!entities.get()) { | 64 if (!entities.get()) { |
51 return DictionaryCreationAssertionFailure(); | 65 return DictionaryCreationAssertionFailure(); |
52 } | 66 } |
53 | 67 |
54 string model_type_string = ModelTypeToString(model_type); | 68 string model_type_string = ModelTypeToString(model_type); |
55 base::ListValue* entity_list = NULL; | 69 base::ListValue* entity_list = NULL; |
56 if (!entities->GetList(model_type_string, &entity_list)) { | 70 if (!entities->GetList(model_type_string, &entity_list)) { |
57 return UnknownTypeAssertionFailure(model_type_string); | 71 return UnknownTypeAssertionFailure(model_type_string); |
58 } else if (expected_count != entity_list->GetSize()) { | 72 } else if (expected_count != entity_list->GetSize()) { |
59 return VerificationCountAssertionFailure(entity_list->GetSize(), | 73 return VerificationCountAssertionFailure(entity_list->GetSize(), |
60 expected_count); | 74 expected_count) |
| 75 << "\n\n" |
| 76 << ConvertFakeServerContentsToString(*entities); |
61 } | 77 } |
62 | 78 |
63 return AssertionSuccess(); | 79 return AssertionSuccess(); |
64 } | 80 } |
65 | 81 |
66 AssertionResult FakeServerVerifier::VerifyEntityCountByTypeAndName( | 82 AssertionResult FakeServerVerifier::VerifyEntityCountByTypeAndName( |
67 size_t expected_count, | 83 size_t expected_count, |
68 syncer::ModelType model_type, | 84 syncer::ModelType model_type, |
69 const string& name) const { | 85 const string& name) const { |
70 scoped_ptr<base::DictionaryValue> entities = | 86 scoped_ptr<base::DictionaryValue> entities = |
(...skipping 12 matching lines...) Expand all Loading... |
83 if (name_value->Equals(*it)) { | 99 if (name_value->Equals(*it)) { |
84 actual_count++; | 100 actual_count++; |
85 } | 101 } |
86 } | 102 } |
87 } | 103 } |
88 | 104 |
89 if (!entity_list) { | 105 if (!entity_list) { |
90 return UnknownTypeAssertionFailure(model_type_string); | 106 return UnknownTypeAssertionFailure(model_type_string); |
91 } else if (actual_count != expected_count) { | 107 } else if (actual_count != expected_count) { |
92 return VerificationCountAssertionFailure(actual_count, expected_count) | 108 return VerificationCountAssertionFailure(actual_count, expected_count) |
93 << "; Name: " << name; | 109 << "; Name: " |
| 110 << name |
| 111 << "\n\n" |
| 112 << ConvertFakeServerContentsToString(*entities); |
94 } | 113 } |
95 | 114 |
96 return AssertionSuccess(); | 115 return AssertionSuccess(); |
97 } | 116 } |
98 | 117 |
99 } // namespace fake_server | 118 } // namespace fake_server |
OLD | NEW |