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
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_SYNC_TEST_TEST_MATCHERS_H_
#define COMPONENTS_SYNC_TEST_TEST_MATCHERS_H_
#include <map>
#include <memory>
#include <string>
#include <utility>
#include "base/hash/hash.h"
#include "components/sync/model/metadata_batch.h"
#include "components/sync/protocol/data_type_state.pb.h"
#include "components/sync/protocol/deletion_origin.pb.h"
#include "components/sync/protocol/entity_metadata.pb.h"
#include "components/sync/service/local_data_description.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace syncer {
using ::testing::_;
using ::testing::Eq;
using ::testing::ExplainMatchResult;
using ::testing::Field;
using ::testing::IsEmpty;
// Matcher for std::optional<ModelError>: verifies that it contains no error.
MATCHER(NoModelError, "") {
if (arg.has_value()) {
*result_listener << "which represents error: " << arg->ToString();
return false;
}
return true;
}
// Matcher for MetadataBatch: verifies that it is empty (i.e. contains neither
// entity metadata nor global model state.
MATCHER(IsEmptyMetadataBatch, "") {
return arg != nullptr &&
sync_pb::DataTypeState().SerializeAsString() ==
arg->GetDataTypeState().SerializeAsString() &&
arg->TakeAllMetadata().empty();
}
// Matcher for MetadataBatch: general purpose verification given two matchers,
// of type Matcher<DataTypeState> and Matcher<EntityMetadataMap> respectively.
MATCHER_P2(MetadataBatchContains, state, entities, "") {
if (arg == nullptr) {
*result_listener << "which is null";
return false;
}
if (!ExplainMatchResult(testing::Matcher<sync_pb::DataTypeState>(state),
arg->GetDataTypeState(), result_listener)) {
return false;
}
// We need to convert the map values to non-pointers in order to make them
// copyable and use gmock.
std::map<std::string, std::unique_ptr<sync_pb::EntityMetadata>>
metadata_by_storage_key = arg->TakeAllMetadata();
std::map<std::string, sync_pb::EntityMetadata> copyable_metadata;
for (auto& [storage_key, metadata] : metadata_by_storage_key) {
copyable_metadata[storage_key] = std::move(*metadata);
}
return ExplainMatchResult(
testing::Matcher<std::map<std::string, sync_pb::EntityMetadata>>(
entities),
copyable_metadata, result_listener);
}
// Matcher for sync_pb::DataTypeState: verifies that field
// `encryption_key_name` is equal to the provided string `expected_key_name`.
MATCHER_P(HasEncryptionKeyName, expected_key_name, "") {
return arg.encryption_key_name() == expected_key_name;
}
// Matcher for sync_pb::DataTypeState: verifies that initial sync is done.
MATCHER(HasInitialSyncDone, "") {
return arg.initial_sync_state() ==
sync_pb::DataTypeState_InitialSyncState_INITIAL_SYNC_DONE;
}
// Matcher for sync_pb::DataTypeState: verifies that initial sync is not done.
MATCHER(HasNotInitialSyncDone, "") {
return arg.initial_sync_state() ==
sync_pb::DataTypeState_InitialSyncState_INITIAL_SYNC_STATE_UNSPECIFIED;
}
MATCHER_P2(MatchesDeletionOrigin, expected_version, expected_location, "") {
const sync_pb::DeletionOrigin& actual_origin = arg;
if (actual_origin.chromium_version() != expected_version) {
*result_listener << "Expected version " << expected_version << " but got "
<< actual_origin.chromium_version();
return false;
}
if (actual_origin.file_name_hash() !=
base::PersistentHash(expected_location.file_name())) {
*result_listener << "Unexpected file name hash: "
<< actual_origin.file_name_hash();
return false;
}
if (actual_origin.file_line_number() != expected_location.line_number()) {
*result_listener << "Unexpected line number: "
<< actual_origin.file_line_number();
return false;
}
return true;
}
// Checks whether the item matches a syncer::LocalDataItemModel.
MATCHER_P4(MatchesLocalDataItemModel, id, icon, title, subtitle, "") {
return ExplainMatchResult(Field(&syncer::LocalDataItemModel::id, id), arg,
result_listener) &&
ExplainMatchResult(Field(&syncer::LocalDataItemModel::icon, icon), arg,
result_listener) &&
ExplainMatchResult(Field(&syncer::LocalDataItemModel::title, title),
arg, result_listener) &&
ExplainMatchResult(
Field(&syncer::LocalDataItemModel::subtitle, subtitle), arg,
result_listener);
}
// Checks whether the description matches a syncer::LocalDataDescription.
MATCHER_P5(MatchesLocalDataDescription,
type,
local_data_models,
item_count,
domains,
domain_count,
"") {
return ExplainMatchResult(Field(&syncer::LocalDataDescription::type, type),
arg, result_listener) &&
ExplainMatchResult(
Field(&syncer::LocalDataDescription::local_data_models,
local_data_models),
arg, result_listener) &&
ExplainMatchResult(
Field(&syncer::LocalDataDescription::item_count, item_count), arg,
result_listener) &&
ExplainMatchResult(
Field(&syncer::LocalDataDescription::domains, domains), arg,
result_listener) &&
ExplainMatchResult(
Field(&syncer::LocalDataDescription::domain_count, domain_count),
arg, result_listener);
}
MATCHER(IsEmptyLocalDataDescription, "") {
return ExplainMatchResult(
MatchesLocalDataDescription(_, IsEmpty(), Eq(0u), IsEmpty(), Eq(0u)), arg,
result_listener);
}
} // namespace syncer
#endif // COMPONENTS_SYNC_TEST_TEST_MATCHERS_H_
|