1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/invalidation/impl/unacked_invalidation_set_test_util.h"
#include <memory>
#include "base/json/json_string_value_serializer.h"
#include "base/macros.h"
#include "components/invalidation/public/object_id_invalidation_map.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
namespace syncer {
using ::testing::MakeMatcher;
using ::testing::MatchResultListener;
using ::testing::Matcher;
using ::testing::MatcherInterface;
using ::testing::PrintToString;
namespace test_util {
// This class needs to be declared outside the null namespace so the
// UnackedInvalidationSet can declare it as a friend. This class needs access
// to the UnackedInvalidationSet internals to implement its comparispon
// function.
class UnackedInvalidationSetEqMatcher
: public testing::MatcherInterface<const UnackedInvalidationSet&> {
public:
explicit UnackedInvalidationSetEqMatcher(
const UnackedInvalidationSet& expected);
bool MatchAndExplain(
const UnackedInvalidationSet& actual,
MatchResultListener* listener) const override;
void DescribeTo(::std::ostream* os) const override;
void DescribeNegationTo(::std::ostream* os) const override;
private:
const UnackedInvalidationSet expected_;
DISALLOW_COPY_AND_ASSIGN(UnackedInvalidationSetEqMatcher);
};
namespace {
struct InvalidationEq {
bool operator()(const syncer::Invalidation& a,
const syncer::Invalidation& b) const {
return a.Equals(b);
}
};
} // namespace
UnackedInvalidationSetEqMatcher::UnackedInvalidationSetEqMatcher(
const UnackedInvalidationSet& expected)
: expected_(expected) {}
bool UnackedInvalidationSetEqMatcher::MatchAndExplain(
const UnackedInvalidationSet& actual,
MatchResultListener* listener) const {
// Use our friendship with this class to compare the internals of two
// instances.
//
// Note that the registration status is intentionally not considered
// when performing this comparison.
return expected_.object_id_ == actual.object_id_
&& std::equal(expected_.invalidations_.begin(),
expected_.invalidations_.end(),
actual.invalidations_.begin(),
InvalidationEq());
}
void UnackedInvalidationSetEqMatcher::DescribeTo(::std::ostream* os) const {
*os << " is equal to " << PrintToString(expected_);
}
void UnackedInvalidationSetEqMatcher::DescribeNegationTo(
::std::ostream* os) const {
*os << " isn't equal to " << PrintToString(expected_);
}
// We're done declaring UnackedInvalidationSetEqMatcher. Everything else can
// go into the null namespace.
namespace {
ObjectIdInvalidationMap UnackedInvalidationsMapToObjectIdInvalidationMap(
const UnackedInvalidationsMap& state_map) {
ObjectIdInvalidationMap object_id_invalidation_map;
for (UnackedInvalidationsMap::const_iterator it = state_map.begin();
it != state_map.end(); ++it) {
it->second.ExportInvalidations(
base::WeakPtr<AckHandler>(),
scoped_refptr<base::SingleThreadTaskRunner>(),
&object_id_invalidation_map);
}
return object_id_invalidation_map;
}
class UnackedInvalidationsMapEqMatcher
: public testing::MatcherInterface<const UnackedInvalidationsMap&> {
public:
explicit UnackedInvalidationsMapEqMatcher(
const UnackedInvalidationsMap& expected);
virtual bool MatchAndExplain(const UnackedInvalidationsMap& actual,
MatchResultListener* listener) const;
virtual void DescribeTo(::std::ostream* os) const;
virtual void DescribeNegationTo(::std::ostream* os) const;
private:
const UnackedInvalidationsMap expected_;
DISALLOW_COPY_AND_ASSIGN(UnackedInvalidationsMapEqMatcher);
};
UnackedInvalidationsMapEqMatcher::UnackedInvalidationsMapEqMatcher(
const UnackedInvalidationsMap& expected)
: expected_(expected) {
}
bool UnackedInvalidationsMapEqMatcher::MatchAndExplain(
const UnackedInvalidationsMap& actual,
MatchResultListener* listener) const {
ObjectIdInvalidationMap expected_inv =
UnackedInvalidationsMapToObjectIdInvalidationMap(expected_);
ObjectIdInvalidationMap actual_inv =
UnackedInvalidationsMapToObjectIdInvalidationMap(actual);
return expected_inv == actual_inv;
}
void UnackedInvalidationsMapEqMatcher::DescribeTo(
::std::ostream* os) const {
*os << " is equal to " << PrintToString(expected_);
}
void UnackedInvalidationsMapEqMatcher::DescribeNegationTo(
::std::ostream* os) const {
*os << " isn't equal to " << PrintToString(expected_);
}
} // namespace
void PrintTo(const UnackedInvalidationSet& invalidations,
::std::ostream* os) {
std::unique_ptr<base::DictionaryValue> value = invalidations.ToValue();
std::string output;
JSONStringValueSerializer serializer(&output);
serializer.set_pretty_print(true);
serializer.Serialize(*value.get());
(*os) << output;
}
void PrintTo(const UnackedInvalidationsMap& map, ::std::ostream* os) {
std::unique_ptr<base::ListValue> list(new base::ListValue);
for (UnackedInvalidationsMap::const_iterator it = map.begin();
it != map.end(); ++it) {
list->Append(it->second.ToValue());
}
std::string output;
JSONStringValueSerializer serializer(&output);
serializer.set_pretty_print(true);
serializer.Serialize(*list.get());
(*os) << output;
}
Matcher<const UnackedInvalidationSet&> Eq(
const UnackedInvalidationSet& expected) {
return MakeMatcher(new UnackedInvalidationSetEqMatcher(expected));
}
Matcher<const UnackedInvalidationsMap&> Eq(
const UnackedInvalidationsMap& expected) {
return MakeMatcher(new UnackedInvalidationsMapEqMatcher(expected));
}
} // namespace test_util
};
|