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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/browser_sync/sync_internals_message_handler.h"
#include <functional>
#include <memory>
#include <string>
#include <utility>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/gmock_move_support.h"
#include "base/test/task_environment.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/sync/model/type_entities_count.h"
#include "components/sync/service/sync_internals_util.h"
#include "components/sync/service/sync_service.h"
#include "components/sync/test/mock_sync_invalidations_service.h"
#include "components/sync/test/mock_sync_service.h"
#include "components/sync_user_events/fake_user_event_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using syncer::sync_ui_util::kGetAllNodes;
using syncer::sync_ui_util::kOnAboutInfoUpdated;
using syncer::sync_ui_util::kOnEntityCountsUpdated;
using syncer::sync_ui_util::kRequestDataAndRegisterForUpdates;
using syncer::sync_ui_util::kRequestStart;
using syncer::sync_ui_util::kWriteUserEvent;
using testing::_;
using testing::ElementsAre;
using testing::Return;
namespace browser_sync {
namespace {
const char kChannel[] = "canary";
// Matches a given base::ValueView against `dict`.
MATCHER_P(ValueViewMatchesDict, dict, "") {
base::Value value = arg.ToValue();
return value.is_dict() && value.GetDict() == dict;
}
class MockDelegate : public SyncInternalsMessageHandler::Delegate {
public:
MockDelegate() = default;
~MockDelegate() override = default;
MOCK_METHOD(void,
SendEventToPage,
(std::string_view, base::span<const base::ValueView>),
(override));
MOCK_METHOD(void,
ResolvePageCallback,
(const base::ValueView, const base::ValueView),
(override));
};
class SyncInternalsMessageHandlerTest : public testing::Test {
public:
SyncInternalsMessageHandlerTest() {
ON_CALL(mock_sync_service_, GetEntityCountsForDebugging)
.WillByDefault(base::test::RunCallback<0>(
syncer::TypeEntitiesCount(syncer::PASSWORDS)));
}
SyncInternalsMessageHandlerTest(const SyncInternalsMessageHandlerTest&) =
delete;
SyncInternalsMessageHandlerTest& operator=(
const SyncInternalsMessageHandlerTest&) = delete;
~SyncInternalsMessageHandlerTest() override = default;
MockDelegate* mock_delegate() { return &mock_delegate_; }
signin::IdentityTestEnvironment* identity_test_environment() {
return &identity_test_environment_;
}
syncer::MockSyncService* mock_sync_service() { return &mock_sync_service_; }
syncer::MockSyncInvalidationsService* mock_sync_invalidations_service() {
return &mock_sync_invalidations_service_;
}
syncer::FakeUserEventService* fake_user_event_service() {
return &fake_user_event_service_;
}
SyncInternalsMessageHandler* handler() { return handler_.get(); }
int get_about_sync_data_call_count() const {
return get_about_sync_data_dall_count_;
}
void ResetHandler() { handler_.reset(); }
// Fake return value for sync_ui_util::ConstructAboutInformation().
const base::Value::Dict kAboutInformation =
base::Value::Dict().Set("some_sync_state", "some_value");
private:
// Returns copies of the same constant dictionary, |kAboutInformation|.
base::Value::Dict ConstructFakeAboutInformation(syncer::SyncService* service,
const std::string& channel) {
++get_about_sync_data_dall_count_;
return kAboutInformation.Clone();
}
// SingleThreadTaskEnvironment is needed for IdentityTestEnvironment.
base::test::SingleThreadTaskEnvironment task_environment_;
MockDelegate mock_delegate_;
signin::IdentityTestEnvironment identity_test_environment_;
syncer::MockSyncService mock_sync_service_;
syncer::MockSyncInvalidationsService mock_sync_invalidations_service_;
syncer::FakeUserEventService fake_user_event_service_;
std::unique_ptr<SyncInternalsMessageHandler> handler_ =
std::make_unique<SyncInternalsMessageHandler>(
&mock_delegate_,
base::BindRepeating(
&SyncInternalsMessageHandlerTest::ConstructFakeAboutInformation,
base::Unretained(this)),
identity_test_environment_.identity_manager(),
&mock_sync_service_,
&mock_sync_invalidations_service_,
&fake_user_event_service_,
kChannel);
int get_about_sync_data_dall_count_ = 0;
};
TEST_F(SyncInternalsMessageHandlerTest, AddRemoveObservers) {
EXPECT_CALL(*mock_sync_service(), AddObserver);
EXPECT_CALL(*mock_sync_service(), RemoveObserver).Times(0);
handler()
->GetMessageHandlerMap()
.at(kRequestDataAndRegisterForUpdates)
.Run(base::Value::List());
testing::Mock::VerifyAndClearExpectations(mock_sync_service());
EXPECT_CALL(*mock_sync_service(), AddObserver).Times(0);
EXPECT_CALL(*mock_sync_service(), RemoveObserver);
ResetHandler();
}
TEST_F(SyncInternalsMessageHandlerTest, AddRemoveObserversDisableMessages) {
EXPECT_CALL(*mock_sync_service(), AddObserver);
EXPECT_CALL(*mock_sync_service(), RemoveObserver).Times(0);
handler()
->GetMessageHandlerMap()
.at(kRequestDataAndRegisterForUpdates)
.Run(base::Value::List());
testing::Mock::VerifyAndClearExpectations(mock_sync_service());
EXPECT_CALL(*mock_sync_service(), AddObserver).Times(0);
EXPECT_CALL(*mock_sync_service(), RemoveObserver);
handler()->DisableMessagesToPage();
testing::Mock::VerifyAndClearExpectations(mock_sync_service());
// Deregistration should not repeat, no counts should increase.
EXPECT_CALL(*mock_sync_service(), AddObserver).Times(0);
EXPECT_CALL(*mock_sync_service(), RemoveObserver).Times(0);
ResetHandler();
}
TEST_F(SyncInternalsMessageHandlerTest, AddRemoveObserversSyncDisabled) {
// Simulate completely disabling sync by flag or other mechanism.
auto handler = std::make_unique<SyncInternalsMessageHandler>(
mock_delegate(),
base::BindLambdaForTesting([&](syncer::SyncService*, const std::string&) {
return kAboutInformation.Clone();
}),
identity_test_environment()->identity_manager(),
/*sync_service=*/nullptr, mock_sync_invalidations_service(),
fake_user_event_service(), kChannel);
handler->GetMessageHandlerMap()
.at(kRequestDataAndRegisterForUpdates)
.Run(base::Value::List());
handler->DisableMessagesToPage();
// Cannot verify observer methods on sync services were not called, because
// there is no sync service. Rather, we're just making sure the handler hasn't
// performed any invalid operations when the sync service is missing.
}
TEST_F(SyncInternalsMessageHandlerTest, HandleGetAllNodes) {
base::OnceCallback<void(base::Value::List)> get_all_nodes_callback;
ON_CALL(*mock_sync_service(), GetAllNodesForDebugging)
.WillByDefault(MoveArg<0>(&get_all_nodes_callback));
handler()
->GetMessageHandlerMap()
.at(kGetAllNodes)
.Run(base::Value::List().Append("getAllNodes_0"));
EXPECT_CALL(*mock_delegate(), ResolvePageCallback);
std::move(get_all_nodes_callback).Run(base::Value::List());
testing::Mock::VerifyAndClearExpectations(mock_delegate());
handler()
->GetMessageHandlerMap()
.at(kGetAllNodes)
.Run(base::Value::List().Append("getAllNodes_1"));
// This breaks the weak ref the callback is hanging onto. Which results in
// the call count not incrementing.
handler()->DisableMessagesToPage();
EXPECT_CALL(*mock_delegate(), ResolvePageCallback).Times(0);
std::move(get_all_nodes_callback).Run(base::Value::List());
testing::Mock::VerifyAndClearExpectations(mock_delegate());
handler()
->GetMessageHandlerMap()
.at(kGetAllNodes)
.Run(base::Value::List().Append("getAllNodes_2"));
EXPECT_CALL(*mock_delegate(), ResolvePageCallback);
std::move(get_all_nodes_callback).Run(base::Value::List());
}
TEST_F(SyncInternalsMessageHandlerTest, SendAboutInfo) {
EXPECT_CALL(
*mock_delegate(),
SendEventToPage(kOnAboutInfoUpdated, ElementsAre(ValueViewMatchesDict(
std::cref(kAboutInformation)))));
EXPECT_CALL(*mock_delegate(), SendEventToPage(kOnEntityCountsUpdated, _));
static_cast<syncer::SyncServiceObserver*>(handler())->OnStateChanged(
mock_sync_service());
EXPECT_EQ(1, get_about_sync_data_call_count());
}
TEST_F(SyncInternalsMessageHandlerTest, WriteUserEvent) {
handler()
->GetMessageHandlerMap()
.at(kWriteUserEvent)
.Run(base::Value::List().Append("1000000000000000000").Append("-1"));
ASSERT_EQ(1u, fake_user_event_service()->GetRecordedUserEvents().size());
const sync_pb::UserEventSpecifics& event =
*fake_user_event_service()->GetRecordedUserEvents().begin();
EXPECT_EQ(sync_pb::UserEventSpecifics::kTestEvent, event.event_case());
EXPECT_EQ(1000000000000000000, event.event_time_usec());
EXPECT_EQ(-1, event.navigation_id());
}
TEST_F(SyncInternalsMessageHandlerTest, WriteUserEventBadParse) {
handler()
->GetMessageHandlerMap()
.at(kWriteUserEvent)
.Run(base::Value::List().Append("123abc").Append("abcde"));
ASSERT_EQ(1u, fake_user_event_service()->GetRecordedUserEvents().size());
const sync_pb::UserEventSpecifics& event =
*fake_user_event_service()->GetRecordedUserEvents().begin();
EXPECT_EQ(sync_pb::UserEventSpecifics::kTestEvent, event.event_case());
EXPECT_EQ(0, event.event_time_usec());
EXPECT_EQ(0, event.navigation_id());
}
TEST_F(SyncInternalsMessageHandlerTest, WriteUserEventBlank) {
handler()
->GetMessageHandlerMap()
.at(kWriteUserEvent)
.Run(base::Value::List().Append("").Append(""));
ASSERT_EQ(1u, fake_user_event_service()->GetRecordedUserEvents().size());
const sync_pb::UserEventSpecifics& event =
*fake_user_event_service()->GetRecordedUserEvents().begin();
EXPECT_EQ(sync_pb::UserEventSpecifics::kTestEvent, event.event_case());
EXPECT_TRUE(event.has_event_time_usec());
EXPECT_EQ(0, event.event_time_usec());
// Should not have a navigation_id because that means something different to
// the UserEvents logic.
EXPECT_FALSE(event.has_navigation_id());
}
TEST_F(SyncInternalsMessageHandlerTest, WriteUserEventZero) {
handler()
->GetMessageHandlerMap()
.at(kWriteUserEvent)
.Run(base::Value::List().Append("0").Append("0"));
ASSERT_EQ(1u, fake_user_event_service()->GetRecordedUserEvents().size());
const sync_pb::UserEventSpecifics& event =
*fake_user_event_service()->GetRecordedUserEvents().begin();
EXPECT_EQ(sync_pb::UserEventSpecifics::kTestEvent, event.event_case());
EXPECT_TRUE(event.has_event_time_usec());
EXPECT_EQ(0, event.event_time_usec());
// Should have a navigation_id, even though the value is 0.
EXPECT_TRUE(event.has_navigation_id());
EXPECT_EQ(0, event.navigation_id());
}
#if !BUILDFLAG(IS_CHROMEOS)
TEST_F(SyncInternalsMessageHandlerTest, RequestStart) {
identity_test_environment()->MakePrimaryAccountAvailable(
"foo@gmail.com", signin::ConsentLevel::kSignin);
EXPECT_CALL(*mock_sync_service()->GetMockUserSettings(),
SetInitialSyncFeatureSetupComplete);
handler()->GetMessageHandlerMap().at(kRequestStart).Run(base::Value::List());
CoreAccountInfo account_info =
identity_test_environment()->identity_manager()->GetPrimaryAccountInfo(
signin::ConsentLevel::kSync);
EXPECT_FALSE(account_info.IsEmpty());
EXPECT_EQ(account_info.email, "foo@gmail.com");
}
#endif // !BUILDFLAG(IS_CHROMEOS)
} // namespace
} // namespace browser_sync
|