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; | |
maniscalco
2015/03/18 15:33:55
Is the using statement necessary? I ask because i
pval...(no longer on Chromium)
2015/03/23 23:35:39
It's not necessary, but I think the code below rea
| |
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(base::DictionaryValue* entities) { | |
maniscalco
2015/03/18 15:33:56
Does entities need to be a non-const pointer? Cou
pval...(no longer on Chromium)
2015/03/23 23:35:39
It does not. Thanks for pointing this out; I've ma
maniscalco
2015/03/24 00:11:19
Great.
| |
40 string entities_str; | |
41 if (!JSONWriter::WriteWithOptions(entities, | |
42 JSONWriter::OPTIONS_PRETTY_PRINT, | |
43 &entities_str)) { | |
44 entities_str = "Could not convert FakeServer contents to string."; | |
45 } | |
46 return "FakeServer contents:\n" + entities_str; | |
47 } | |
48 | |
36 } // namespace | 49 } // namespace |
37 | 50 |
38 namespace fake_server { | 51 namespace fake_server { |
39 | 52 |
40 FakeServerVerifier::FakeServerVerifier(FakeServer* fake_server) | 53 FakeServerVerifier::FakeServerVerifier(FakeServer* fake_server) |
41 : fake_server_(fake_server) { } | 54 : fake_server_(fake_server) { } |
42 | 55 |
43 FakeServerVerifier::~FakeServerVerifier() {} | 56 FakeServerVerifier::~FakeServerVerifier() {} |
44 | 57 |
45 AssertionResult FakeServerVerifier::VerifyEntityCountByType( | 58 AssertionResult FakeServerVerifier::VerifyEntityCountByType( |
46 size_t expected_count, | 59 size_t expected_count, |
47 syncer::ModelType model_type) const { | 60 syncer::ModelType model_type) const { |
48 scoped_ptr<base::DictionaryValue> entities = | 61 scoped_ptr<base::DictionaryValue> entities = |
49 fake_server_->GetEntitiesAsDictionaryValue(); | 62 fake_server_->GetEntitiesAsDictionaryValue(); |
50 if (!entities.get()) { | 63 if (!entities.get()) { |
51 return DictionaryCreationAssertionFailure(); | 64 return DictionaryCreationAssertionFailure(); |
52 } | 65 } |
53 | 66 |
54 string model_type_string = ModelTypeToString(model_type); | 67 string model_type_string = ModelTypeToString(model_type); |
55 base::ListValue* entity_list = NULL; | 68 base::ListValue* entity_list = NULL; |
56 if (!entities->GetList(model_type_string, &entity_list)) { | 69 if (!entities->GetList(model_type_string, &entity_list)) { |
57 return UnknownTypeAssertionFailure(model_type_string); | 70 return UnknownTypeAssertionFailure(model_type_string); |
58 } else if (expected_count != entity_list->GetSize()) { | 71 } else if (expected_count != entity_list->GetSize()) { |
59 return VerificationCountAssertionFailure(entity_list->GetSize(), | 72 return VerificationCountAssertionFailure(entity_list->GetSize(), |
60 expected_count); | 73 expected_count) |
74 << "\n\n" | |
75 << ConvertFakeServerContentsToString(entities.get()); | |
61 } | 76 } |
62 | 77 |
63 return AssertionSuccess(); | 78 return AssertionSuccess(); |
64 } | 79 } |
65 | 80 |
66 AssertionResult FakeServerVerifier::VerifyEntityCountByTypeAndName( | 81 AssertionResult FakeServerVerifier::VerifyEntityCountByTypeAndName( |
67 size_t expected_count, | 82 size_t expected_count, |
68 syncer::ModelType model_type, | 83 syncer::ModelType model_type, |
69 const string& name) const { | 84 const string& name) const { |
70 scoped_ptr<base::DictionaryValue> entities = | 85 scoped_ptr<base::DictionaryValue> entities = |
(...skipping 12 matching lines...) Expand all Loading... | |
83 if (name_value->Equals(*it)) { | 98 if (name_value->Equals(*it)) { |
84 actual_count++; | 99 actual_count++; |
85 } | 100 } |
86 } | 101 } |
87 } | 102 } |
88 | 103 |
89 if (!entity_list) { | 104 if (!entity_list) { |
90 return UnknownTypeAssertionFailure(model_type_string); | 105 return UnknownTypeAssertionFailure(model_type_string); |
91 } else if (actual_count != expected_count) { | 106 } else if (actual_count != expected_count) { |
92 return VerificationCountAssertionFailure(actual_count, expected_count) | 107 return VerificationCountAssertionFailure(actual_count, expected_count) |
93 << "; Name: " << name; | 108 << "; Name: " |
109 << name | |
110 << "\n\n" | |
111 << ConvertFakeServerContentsToString(entities.get()); | |
94 } | 112 } |
95 | 113 |
96 return AssertionSuccess(); | 114 return AssertionSuccess(); |
97 } | 115 } |
98 | 116 |
99 } // namespace fake_server | 117 } // namespace fake_server |
OLD | NEW |