Commit e5d2356b authored by Mikel Astiz's avatar Mikel Astiz Committed by Commit Bot

Introduce common gmock matchers for USS

We adopt them already in some existing tests and plan to use them in
future patches as part of the migration of session sync to USS.

Bug: 681921
Change-Id: I91bcb5b2d0383be89bef1b905e1d3b16259f94bb
Reviewed-on: https://chromium-review.googlesource.com/983918Reviewed-by: default avatarJan Krcal <jkrcal@chromium.org>
Commit-Queue: Mikel Astiz <mastiz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#549525}
parent 17e0bfdb
......@@ -620,6 +620,7 @@ static_library("test_support_base") {
"test/mock_invalidation.h",
"test/mock_invalidation_tracker.cc",
"test/mock_invalidation_tracker.h",
"test/test_matchers.h",
"test/trackable_mock_invalidation.cc",
"test/trackable_mock_invalidation.h",
]
......
......@@ -5,4 +5,5 @@ include_rules = [
"+components/sync/engine",
"+components/sync/model",
"+components/sync/protocol",
"+components/sync/test",
]
......@@ -22,6 +22,7 @@
#include "components/sync/model/mock_model_type_change_processor.h"
#include "components/sync/model/model_type_store_test_util.h"
#include "components/sync/protocol/model_type_state.pb.h"
#include "components/sync/test/test_matchers.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
......@@ -57,11 +58,6 @@ const sync_pb::SyncEnums::DeviceType kDeviceType =
// initializing. Remote data should use other suffixes.
const int kDefaultLocalSuffix = 0;
MATCHER_P(MetadataHasEncryptionKeyName, expected_key_name, "") {
return arg != nullptr &&
arg->GetModelTypeState().encryption_key_name() == expected_key_name;
}
MATCHER_P(HasDeviceInfo, expected, "") {
return arg.device_info().SerializeAsString() == expected.SerializeAsString();
}
......@@ -444,8 +440,10 @@ TEST_F(DeviceInfoSyncBridgeTest, TestWithLocalDataAndMetadata) {
WriteToStore({specifics}, state);
EXPECT_CALL(*processor(),
DoModelReadyToSync(_, MetadataHasEncryptionKeyName(
state.encryption_key_name())));
DoModelReadyToSync(
_, MetadataBatchContains(
HasEncryptionKeyName(state.encryption_key_name()),
/*entities=*/IsEmpty())));
InitializeAndPump();
ASSERT_EQ(2u, bridge()->GetAllDeviceInfo().size());
......@@ -545,8 +543,10 @@ TEST_F(DeviceInfoSyncBridgeTest, ApplySyncChangesStore) {
EXPECT_EQ(2, change_count());
EXPECT_CALL(*processor(),
DoModelReadyToSync(_, MetadataHasEncryptionKeyName(
state.encryption_key_name())));
DoModelReadyToSync(
_, MetadataBatchContains(
HasEncryptionKeyName(state.encryption_key_name()),
/*entities=*/IsEmpty())));
RestartBridge();
std::unique_ptr<DeviceInfo> info =
......@@ -670,8 +670,10 @@ TEST_F(DeviceInfoSyncBridgeTest, MergeWithData) {
ModelEqualsSpecifics(conflict_remote));
EXPECT_CALL(*processor(),
DoModelReadyToSync(_, MetadataHasEncryptionKeyName(
state.encryption_key_name())));
DoModelReadyToSync(
_, MetadataBatchContains(
HasEncryptionKeyName(state.encryption_key_name()),
/*entities=*/IsEmpty())));
RestartBridge();
}
......
......@@ -15,6 +15,7 @@
#include "components/sync/model/model_error.h"
#include "components/sync/protocol/entity_metadata.pb.h"
#include "components/sync/protocol/model_type_state.pb.h"
#include "components/sync/test/test_matchers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -38,13 +39,6 @@ sync_pb::EntityMetadata CreateEntityMetadata(const std::string& value) {
return metadata;
}
MATCHER(IsEmptyMetadataBatch, "") {
return arg != nullptr &&
sync_pb::ModelTypeState().SerializeAsString() ==
arg->GetModelTypeState().SerializeAsString() &&
arg->TakeAllMetadata().empty();
}
} // namespace
class ModelTypeStoreImplTest : public testing::Test {
......
// Copyright 2018 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.
#ifndef COMPONENTS_SYNC_TEST_TEST_MATCHERS_H_
#define COMPONENTS_SYNC_TEST_TEST_MATCHERS_H_
#include <string>
#include <vector>
#include "components/sync/model/metadata_batch.h"
#include "components/sync/protocol/model_type_state.pb.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace syncer {
// Matcher for base::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::ModelTypeState().SerializeAsString() ==
arg->GetModelTypeState().SerializeAsString() &&
arg->TakeAllMetadata().empty();
}
// Matcher for MetadataBatch: general purpose verification given two matchers,
// of type Matcher<ModelTypeState> 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::ModelTypeState>(state),
arg->GetModelTypeState(), result_listener)) {
return false;
}
return ExplainMatchResult(testing::Matcher<EntityMetadataMap>(entities),
arg->TakeAllMetadata(), result_listener);
}
// Matcher for sync_pb::ModelTypeState: 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;
}
} // namespace syncer
#endif // COMPONENTS_SYNC_TEST_TEST_MATCHERS_H_
......@@ -63,6 +63,8 @@ static_library("test_support") {
"mock_sync_sessions_client.h",
"session_sync_test_helper.cc",
"session_sync_test_helper.h",
"test_matchers.cc",
"test_matchers.h",
"test_synced_window_delegates_getter.cc",
"test_synced_window_delegates_getter.h",
]
......
// Copyright 2018 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/sync_sessions/test_matchers.h"
#include "components/sessions/core/session_id.h"
#include "components/sync/model/entity_data.h"
#include "components/sync/protocol/session_specifics.pb.h"
#include "components/sync_sessions/synced_session.h"
namespace sync_sessions {
namespace {
using testing::ElementsAreArray;
using testing::Matcher;
using testing::MatcherInterface;
using testing::MatchResultListener;
class MatchesHeaderMatcher
: public MatcherInterface<const sync_pb::SessionSpecifics&> {
public:
MatchesHeaderMatcher(Matcher<std::string> session_tag,
Matcher<std::vector<int>> window_ids,
Matcher<std::vector<int>> tab_ids)
: session_tag_(session_tag), window_ids_(window_ids), tab_ids_(tab_ids) {}
bool MatchAndExplain(const sync_pb::SessionSpecifics& actual,
MatchResultListener* listener) const override {
if (!actual.has_header()) {
*listener << "which is not a header entity";
return false;
}
if (!session_tag_.MatchAndExplain(actual.session_tag(), listener)) {
*listener << "which contains an unexpected session tag: "
<< actual.session_tag();
return false;
}
std::vector<int> actual_window_ids;
std::vector<int> actual_tab_ids;
for (const auto& window : actual.header().window()) {
actual_window_ids.push_back(window.window_id());
actual_tab_ids.insert(actual_tab_ids.end(), window.tab().begin(),
window.tab().end());
}
if (!window_ids_.MatchAndExplain(actual_window_ids, listener)) {
*listener << "which contains an unexpected windows";
return false;
}
if (!tab_ids_.MatchAndExplain(actual_tab_ids, listener)) {
*listener << "which contains an unexpected tabs";
return false;
}
return true;
}
void DescribeTo(::std::ostream* os) const override {
*os << "matches expected header";
}
void DescribeNegationTo(::std::ostream* os) const override {
*os << "does not match expected header";
}
private:
Matcher<std::string> session_tag_;
Matcher<std::vector<int>> window_ids_;
Matcher<std::vector<int>> tab_ids_;
};
class MatchesTabMatcher
: public MatcherInterface<const sync_pb::SessionSpecifics&> {
public:
MatchesTabMatcher(Matcher<std::string> session_tag,
Matcher<int> window_id,
Matcher<int> tab_id,
Matcher<std::vector<std::string>> urls)
: session_tag_(session_tag),
window_id_(window_id),
tab_id_(tab_id),
urls_(urls) {}
bool MatchAndExplain(const sync_pb::SessionSpecifics& actual,
MatchResultListener* listener) const override {
if (!actual.has_tab()) {
*listener << "which is not a tab entity";
return false;
}
if (!session_tag_.MatchAndExplain(actual.session_tag(), listener)) {
*listener << "which contains an unexpected session tag: "
<< actual.session_tag();
return false;
}
if (!window_id_.MatchAndExplain(actual.tab().window_id(), listener)) {
*listener << "which contains an unexpected window ID: "
<< actual.tab().window_id();
return false;
}
if (!tab_id_.MatchAndExplain(actual.tab().tab_id(), listener)) {
*listener << "which contains an unexpected tab ID: "
<< actual.tab().tab_id();
return false;
}
std::vector<std::string> actual_urls;
for (const sync_pb::TabNavigation& navigation : actual.tab().navigation()) {
actual_urls.push_back(navigation.virtual_url());
}
if (!urls_.MatchAndExplain(actual_urls, listener)) {
*listener << "which contains unexpected navigation URLs";
return false;
}
return true;
}
void DescribeTo(::std::ostream* os) const override {
*os << "matches expected tab";
}
void DescribeNegationTo(::std::ostream* os) const override {
*os << "does not match expected tab";
}
private:
Matcher<std::string> session_tag_;
Matcher<int> window_id_;
Matcher<int> tab_id_;
Matcher<std::vector<std::string>> urls_;
};
class MatchesSyncedSessionMatcher
: public MatcherInterface<const SyncedSession*> {
public:
MatchesSyncedSessionMatcher(Matcher<std::string> session_tag,
Matcher<std::vector<int>> window_ids,
Matcher<std::vector<int>> tab_ids)
: session_tag_(session_tag), window_ids_(window_ids), tab_ids_(tab_ids) {}
bool MatchAndExplain(const SyncedSession* actual,
MatchResultListener* listener) const override {
if (!actual) {
*listener << "which is null";
return false;
}
if (!session_tag_.MatchAndExplain(actual->session_tag, listener)) {
*listener << "which contains an unexpected session tag: "
<< actual->session_tag;
return false;
}
std::vector<int> actual_window_ids;
std::vector<int> actual_tab_ids;
for (const auto& id_and_window : actual->windows) {
actual_window_ids.push_back(
id_and_window.second->wrapped_window.window_id.id());
for (const auto& id : id_and_window.second->wrapped_window.tabs) {
actual_tab_ids.push_back(id->tab_id.id());
}
}
if (!window_ids_.MatchAndExplain(actual_window_ids, listener)) {
*listener << "which contains an unexpected window set";
return false;
}
if (!tab_ids_.MatchAndExplain(actual_tab_ids, listener)) {
*listener << "which contains an unexpected tab set";
return false;
}
return true;
}
void DescribeTo(::std::ostream* os) const override {
*os << "matches expected synced session";
}
void DescribeNegationTo(::std::ostream* os) const override {
*os << "does not match expected synced session";
}
private:
Matcher<std::string> session_tag_;
Matcher<std::vector<int>> window_ids_;
Matcher<std::vector<int>> tab_ids_;
};
} // namespace
Matcher<const sync_pb::SessionSpecifics&> MatchesHeader(
Matcher<std::string> session_tag,
Matcher<std::vector<int>> window_ids,
Matcher<std::vector<int>> tab_ids) {
return testing::MakeMatcher(
new MatchesHeaderMatcher(session_tag, window_ids, tab_ids));
}
testing::Matcher<const sync_pb::SessionSpecifics&> MatchesHeader(
testing::Matcher<std::string> session_tag,
const std::vector<int>& window_ids,
const std::vector<int>& tab_ids) {
return MatchesHeader(session_tag, ElementsAreArray(window_ids),
ElementsAreArray(tab_ids));
}
Matcher<const sync_pb::SessionSpecifics&> MatchesTab(
Matcher<std::string> session_tag,
Matcher<int> window_id,
Matcher<int> tab_id,
Matcher<std::vector<std::string>> urls) {
return testing::MakeMatcher(
new MatchesTabMatcher(session_tag, window_id, tab_id, urls));
}
testing::Matcher<const sync_pb::SessionSpecifics&> MatchesTab(
testing::Matcher<std::string> session_tag,
testing::Matcher<int> window_id,
testing::Matcher<int> tab_id,
const std::vector<std::string>& urls) {
return MatchesTab(session_tag, window_id, tab_id, ElementsAreArray(urls));
}
Matcher<const SyncedSession*> MatchesSyncedSession(
Matcher<std::string> session_tag,
Matcher<std::vector<int>> window_ids,
Matcher<std::vector<int>> tab_ids) {
return testing::MakeMatcher(
new MatchesSyncedSessionMatcher(session_tag, window_ids, tab_ids));
}
testing::Matcher<const SyncedSession*> MatchesSyncedSession(
testing::Matcher<std::string> session_tag,
const std::vector<int>& window_ids,
const std::vector<int>& tab_ids) {
return MatchesSyncedSession(session_tag, ElementsAreArray(window_ids),
ElementsAreArray(tab_ids));
}
} // namespace sync_sessions
// Copyright 2018 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.
#ifndef COMPONENTS_SYNC_SESSIONS_TEST_MATCHERS_H_
#define COMPONENTS_SYNC_SESSIONS_TEST_MATCHERS_H_
#include <string>
#include <vector>
#include "components/sync/protocol/session_specifics.pb.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace sync_sessions {
struct SyncedSession;
testing::Matcher<const sync_pb::SessionSpecifics&> MatchesHeader(
testing::Matcher<std::string> session_tag,
testing::Matcher<std::vector<int>> window_ids,
testing::Matcher<std::vector<int>> tab_ids);
// Convenience overload.
testing::Matcher<const sync_pb::SessionSpecifics&> MatchesHeader(
testing::Matcher<std::string> session_tag,
const std::vector<int>& window_ids,
const std::vector<int>& tab_ids);
testing::Matcher<const sync_pb::SessionSpecifics&> MatchesTab(
testing::Matcher<std::string> session_tag,
testing::Matcher<int> window_id,
testing::Matcher<int> tab_id,
testing::Matcher<std::vector<std::string>> urls);
// Convenience overload.
testing::Matcher<const sync_pb::SessionSpecifics&> MatchesTab(
testing::Matcher<std::string> session_tag,
testing::Matcher<int> window_id,
testing::Matcher<int> tab_id,
const std::vector<std::string>& urls);
testing::Matcher<const SyncedSession*> MatchesSyncedSession(
testing::Matcher<std::string> session_tag,
testing::Matcher<std::vector<int>> window_ids,
testing::Matcher<std::vector<int>> tab_ids);
// Convenience overload.
testing::Matcher<const SyncedSession*> MatchesSyncedSession(
testing::Matcher<std::string> session_tag,
const std::vector<int>& window_ids,
const std::vector<int>& tab_ids);
} // namespace sync_sessions
#endif // COMPONENTS_SYNC_SESSIONS_TEST_MATCHERS_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment