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/memory/scoped_ptr.h"
8 #include "base/values.h"
9 #include "sync/internal_api/public/base/model_type.h"
10 #include "sync/test/fake_server/fake_server.h"
11 #include "testing/gtest/include/gtest/gtest.h"
14 using testing::AssertionFailure
;
15 using testing::AssertionResult
;
16 using testing::AssertionSuccess
;
20 AssertionResult
DictionaryCreationAssertionFailure() {
21 return AssertionFailure() << "FakeServer failed to create an entities "
25 AssertionResult
VerificationCountAssertionFailure(size_t actual_count
,
26 size_t expected_count
) {
27 return AssertionFailure() << "Actual count: " << actual_count
<< "; "
28 << "Expected count: " << expected_count
;
31 AssertionResult
UnknownTypeAssertionFailure(const string
& model_type
) {
32 return AssertionFailure() << "Verification not attempted. Unknown ModelType: "
38 namespace fake_server
{
40 FakeServerVerifier::FakeServerVerifier(FakeServer
* fake_server
)
41 : fake_server_(fake_server
) { }
43 FakeServerVerifier::~FakeServerVerifier() {}
45 AssertionResult
FakeServerVerifier::VerifyEntityCountByType(
46 size_t expected_count
,
47 syncer::ModelType model_type
) const {
48 scoped_ptr
<base::DictionaryValue
> entities
=
49 fake_server_
->GetEntitiesAsDictionaryValue();
50 if (!entities
.get()) {
51 return DictionaryCreationAssertionFailure();
54 string model_type_string
= ModelTypeToString(model_type
);
55 base::ListValue
* entity_list
= NULL
;
56 if (!entities
->GetList(model_type_string
, &entity_list
)) {
57 return UnknownTypeAssertionFailure(model_type_string
);
58 } else if (expected_count
!= entity_list
->GetSize()) {
59 return VerificationCountAssertionFailure(entity_list
->GetSize(),
63 return AssertionSuccess();
66 AssertionResult
FakeServerVerifier::VerifyEntityCountByTypeAndName(
67 size_t expected_count
,
68 syncer::ModelType model_type
,
69 const string
& name
) const {
70 scoped_ptr
<base::DictionaryValue
> entities
=
71 fake_server_
->GetEntitiesAsDictionaryValue();
72 if (!entities
.get()) {
73 return DictionaryCreationAssertionFailure();
76 string model_type_string
= ModelTypeToString(model_type
);
77 base::ListValue
* entity_list
= NULL
;
78 size_t actual_count
= 0;
79 if (entities
->GetList(model_type_string
, &entity_list
)) {
80 scoped_ptr
<base::Value
> name_value(new base::StringValue(name
));
81 for (base::ListValue::const_iterator it
= entity_list
->begin();
82 it
!= entity_list
->end(); ++it
) {
83 if (name_value
->Equals(*it
)) {
90 return UnknownTypeAssertionFailure(model_type_string
);
91 } else if (actual_count
!= expected_count
) {
92 return VerificationCountAssertionFailure(actual_count
, expected_count
)
93 << "; Name: " << name
;
96 return AssertionSuccess();
99 } // namespace fake_server