Skip to content

Commit

Permalink
[WIP] Migrate brave_sync_profile_sync_service_unittest.cc away from m…
Browse files Browse the repository at this point in the history
…ocks

SyncApiComponentFactoryMock is no longer available upstream, and
FakeSyncApiComponentFactory should be used instead, along with
some other smaller changes derived from other recent related
changes upstream.

This patch adapts the unit tests to maintain the same behaviour
while using the new fake-based APIs, instead of the old ones.

[TODO] BraveProfileSyncServiceTest.NoIdentityManagerCalls is not
completely migrated yet, we still need to migrate mock-based check
EXPECT_CALL(*engine(), OnCookieJarChanged(_, _)).Times(0)

Chromium changes:
https://chromium.googlesource.com/chromium/src.git/+/dcdd123f162664500d3976cdcb8e575ad8711bcf
https://chromium.googlesource.com/chromium/src.git/+/0ff6cfd2ae2f17dd8f99ae09f04b0fb24fd2a09f
https://chromium.googlesource.com/chromium/src.git/+/e60c93afaa485a6641ccd26222581626f74a7cac

commit dcdd123f162664500d3976cdcb8e575ad8711bcf
Author: Mikel Astiz <[email protected]>
Date:   Fri Jan 29 18:43:53 2021 +0000

    [sync] Move SyncTransportDataPrefs closer to SyncEngineImpl

    These preferences represent the internal bookkeeping data that the
    implementation of the sync engine owns (SyncEngineImpl, backend, etc.),
    to be persisted in prefs.

    This patch moves the resposibility for dealing with prefs (reads and
    writes) from ProfileSyncService to SyncEngineImpl, simplifying some of
    of the APIs in between and adhering to more strict layering principles.

    Instead of moving all functionality in a single patch, the most relevant
    parts are moved, but TODOs are added to move the remaining parts and
    therefore completely avoid the dependency from ProfileSyncService to
    SyncTransportDataPrefs. Meanwhile, *both* ProfileSyncService and
    SyncEngineImpl interact with these preferences, which is obviously not
    great.

    Bug: 938894

commit 0ff6cfd2ae2f17dd8f99ae09f04b0fb24fd2a09f
Author: Mikel Astiz <[email protected]>
Date:   Fri Jan 29 12:52:21 2021 +0000

    [sync] Avoid abuse of mocks in ProfileSyncService tests

    In particular the tests in ProfileSyncServiceStartupTest made heavy use
    of statefull mocks, well beyond a reasonable use of mocks.

    Instead, this patch adopts fakes that mimic the production behavior
    without tests having to carefully do so. For DataTypeManager, there is
    currently no fake, so instead the actual DataTypeManagerImpl is used,
    which is perfectly testable in combination with FakeDataTypeController.

    Bug: 938894

commit e60c93afaa485a6641ccd26222581626f74a7cac
Author: Marc Treib <[email protected]>
Date:   Tue Jan 26 17:10:16 2021 +0000

    Cleanup: Remove initial_types param from DataTypeManagerImpl's ctor

    The param was used to initialize the downloaded_types_ member, but that
    anyway got recomputed in ActivateDataTypes(), so the initial value had
    no effect. This CL removes the param, and all the plumbing that was in
    place for it.
    Note that there was one test which used the initial_types param, with
    the intention of marking these types as "already downloaded". The test
    passes before as after this CL. Fixing the test so that it actually
    tests what it claims to test is left as a followup.

    Bug: 1170318
  • Loading branch information
mariospr committed Feb 8, 2021
1 parent b83897c commit d114720
Showing 1 changed file with 19 additions and 30 deletions.
49 changes: 19 additions & 30 deletions components/sync/driver/brave_sync_profile_sync_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@
#include "brave/components/sync/driver/brave_sync_profile_sync_service.h"
#include "brave/components/sync/driver/profile_sync_service_delegate.h"
#include "components/os_crypt/os_crypt_mocker.h"
#include "components/sync/driver/data_type_manager_mock.h"
#include "components/sync/driver/data_type_manager_impl.h"
#include "components/sync/driver/fake_data_type_controller.h"
#include "components/sync/driver/fake_sync_api_component_factory.h"
#include "components/sync/driver/profile_sync_service_bundle.h"
#include "components/sync/driver/sync_api_component_factory_mock.h"
#include "components/sync/test/engine/fake_sync_engine.h"
#include "components/sync/test/engine/mock_sync_engine.h"
#include "components/sync/test/engine/fake_sync_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::ByMove;
using testing::NiceMock;
using testing::Return;

namespace syncer {
Expand Down Expand Up @@ -75,32 +74,20 @@ class BraveProfileSyncServiceTest : public testing::Test {
std::make_unique<ProfileSyncServiceDelegateMock>());
}

MockSyncEngine* SetUpMockSyncEngine() {
auto sync_engine = std::make_unique<NiceMock<MockSyncEngine>>();
MockSyncEngine* sync_engine_raw = sync_engine.get();
ON_CALL(*component_factory(), CreateSyncEngine(_, _, _, _))
.WillByDefault(Return(ByMove(std::move(sync_engine))));
return sync_engine_raw;
}

FakeSyncEngine* SetUpFakeSyncEngine() {
auto sync_engine = std::make_unique<FakeSyncEngine>();
FakeSyncEngine* sync_engine_raw = sync_engine.get();
ON_CALL(*component_factory(), CreateSyncEngine(_, _, _, _))
.WillByDefault(Return(ByMove(std::move(sync_engine))));
return sync_engine_raw;
}

brave_sync::Prefs* brave_sync_prefs() { return &brave_sync_prefs_; }

SyncPrefs* sync_prefs() { return &sync_prefs_; }

BraveProfileSyncService* brave_sync_service() { return sync_service_.get(); }

SyncApiComponentFactoryMock* component_factory() {
FakeSyncApiComponentFactory* component_factory() {
return profile_sync_service_bundle_.component_factory();
}

FakeSyncEngine* engine() {
return component_factory()->last_created_engine();
}

private:
content::BrowserTaskEnvironment task_environment_;
ProfileSyncServiceBundle profile_sync_service_bundle_;
Expand All @@ -113,9 +100,9 @@ TEST_F(BraveProfileSyncServiceTest, ValidPassphrase) {
OSCryptMocker::SetUp();

CreateSyncService(ProfileSyncService::MANUAL_START);
SetUpFakeSyncEngine();

brave_sync_service()->Initialize();
EXPECT_FALSE(engine());

bool set_code_result = brave_sync_service()->SetSyncCode(kValidSyncCode);
EXPECT_TRUE(set_code_result);
Expand All @@ -129,9 +116,9 @@ TEST_F(BraveProfileSyncServiceTest, InvalidPassphrase) {
OSCryptMocker::SetUp();

CreateSyncService(ProfileSyncService::MANUAL_START);
SetUpFakeSyncEngine();

brave_sync_service()->Initialize();
EXPECT_FALSE(engine());

bool set_code_result =
brave_sync_service()->SetSyncCode("word one and then two");
Expand All @@ -146,9 +133,9 @@ TEST_F(BraveProfileSyncServiceTest, ValidPassphraseLeadingTrailingWhitespace) {
OSCryptMocker::SetUp();

CreateSyncService(ProfileSyncService::MANUAL_START);
SetUpFakeSyncEngine();

brave_sync_service()->Initialize();
EXPECT_FALSE(engine());

std::string sync_code_extra_whitespace =
std::string(" \t\n") + kValidSyncCode + std::string(" \t\n");
Expand All @@ -165,16 +152,16 @@ TEST_F(BraveProfileSyncServiceTest, NoIdentityManagerCalls) {
OSCryptMocker::SetUp();

CreateSyncService(ProfileSyncService::MANUAL_START);
MockSyncEngine* sync_engine = SetUpMockSyncEngine();

brave_sync_service()->Initialize();
component_factory()->AllowFakeEngineInitCompletion(false);

bool set_code_result = brave_sync_service()->SetSyncCode(kValidSyncCode);
EXPECT_TRUE(set_code_result);

EXPECT_EQ(brave_sync_prefs()->GetSeed(), kValidSyncCode);

ON_CALL(*sync_engine, IsInitialized()).WillByDefault(Return(true));
EXPECT_TRUE(engine());
engine()->TriggerInitializationCompletion(/*success=*/true);

// We need to test that during `ProfileSyncService::OnEngineInitialized`
// the stubbed call identity_manager_->GetAccountsInCookieJar() is invoked.
Expand All @@ -188,10 +175,12 @@ TEST_F(BraveProfileSyncServiceTest, NoIdentityManagerCalls) {
// So the indirect way to ensure is to see there is no call of
// `SyncEngine::OnCookieJarChanged`

EXPECT_CALL(*sync_engine, OnCookieJarChanged(_, _)).Times(0);
// TODO: Migrate this EXPECT_CALL
//EXPECT_CALL(*engine(), OnCookieJarChanged(_, _)).Times(0);

brave_sync_service()->OnEngineInitialized(
ModelTypeSet(BOOKMARKS), WeakHandle<JsBackend>(),
WeakHandle<DataTypeDebugInfoListener>(), "", "", true);
WeakHandle<JsBackend>(), WeakHandle<DataTypeDebugInfoListener>(),
/*success=*/true, /*is_first_time_sync_configure=*/false);

OSCryptMocker::TearDown();
}
Expand Down

0 comments on commit d114720

Please sign in to comment.