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
|
// Copyright (c) 2012 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/browser_sync/abstract_profile_sync_service_test.h"
#include <utility>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/files/file_path.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/browser_sync/test_http_bridge_factory.h"
#include "components/browser_sync/test_profile_sync_service.h"
#include "components/sync/driver/glue/sync_backend_host_core.h"
#include "components/sync/driver/sync_api_component_factory_mock.h"
#include "components/sync/engine/sync_manager_factory_for_profile_sync_test.h"
#include "components/sync/engine/test_engine_components_factory.h"
#include "components/sync/protocol/sync.pb.h"
#include "components/sync/syncable/test_user_share.h"
#include "google_apis/gaia/gaia_constants.h"
using syncer::SyncBackendHostImpl;
using syncer::ModelType;
using testing::_;
using testing::Return;
namespace browser_sync {
namespace {
std::unique_ptr<syncer::HttpPostProviderFactory> GetHttpPostProviderFactory(
syncer::CancelationSignal* signal) {
return base::MakeUnique<TestHttpBridgeFactory>();
}
class SyncEngineForProfileSyncTest : public SyncBackendHostImpl {
public:
SyncEngineForProfileSyncTest(
const base::FilePath& temp_dir,
syncer::SyncClient* sync_client,
invalidation::InvalidationService* invalidator,
const base::WeakPtr<syncer::SyncPrefs>& sync_prefs,
const base::Closure& callback);
~SyncEngineForProfileSyncTest() override;
void Initialize(InitParams params) override;
void ConfigureDataTypes(ConfigureParams params) override;
private:
// Invoked at the start of HandleSyncManagerInitializationOnFrontendLoop.
// Allows extra initialization work to be performed before the engine comes
// up.
base::Closure callback_;
DISALLOW_COPY_AND_ASSIGN(SyncEngineForProfileSyncTest);
};
SyncEngineForProfileSyncTest::SyncEngineForProfileSyncTest(
const base::FilePath& temp_dir,
syncer::SyncClient* sync_client,
invalidation::InvalidationService* invalidator,
const base::WeakPtr<syncer::SyncPrefs>& sync_prefs,
const base::Closure& callback)
: SyncBackendHostImpl(
"dummy_debug_name",
sync_client,
invalidator,
sync_prefs,
temp_dir.Append(base::FilePath(FILE_PATH_LITERAL("test")))),
callback_(callback) {}
SyncEngineForProfileSyncTest::~SyncEngineForProfileSyncTest() {}
void SyncEngineForProfileSyncTest::Initialize(InitParams params) {
params.http_factory_getter = base::Bind(&GetHttpPostProviderFactory);
params.sync_manager_factory =
base::MakeUnique<syncer::SyncManagerFactoryForProfileSyncTest>(callback_);
params.credentials.email = "testuser@gmail.com";
params.credentials.sync_token = "token";
params.credentials.scope_set.insert(GaiaConstants::kChromeSyncOAuth2Scope);
params.restored_key_for_bootstrapping.clear();
// It'd be nice if we avoided creating the EngineComponentsFactory in the
// first place, but SyncEngine will have created one by now so we must free
// it. Grab the switches to pass on first.
syncer::EngineComponentsFactory::Switches factory_switches =
params.engine_components_factory->GetSwitches();
params.engine_components_factory =
base::MakeUnique<syncer::TestEngineComponentsFactory>(
factory_switches, syncer::EngineComponentsFactory::STORAGE_IN_MEMORY,
nullptr);
SyncBackendHostImpl::Initialize(std::move(params));
}
void SyncEngineForProfileSyncTest::ConfigureDataTypes(ConfigureParams params) {
// The first parameter there should be the set of enabled types. That's not
// something we have access to from this strange test harness. We'll just
// send back the list of newly configured types instead and hope it doesn't
// break anything.
// Posted to avoid re-entrancy issues.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(
&SyncEngineForProfileSyncTest::FinishConfigureDataTypesOnFrontendLoop,
base::Unretained(this), params.to_download, params.to_download,
syncer::ModelTypeSet(), params.ready_task));
}
// Helper function for return-type-upcasting of the callback.
syncer::SyncService* GetSyncService(
base::Callback<TestProfileSyncService*(void)> get_sync_service_callback) {
return get_sync_service_callback.Run();
}
} // namespace
/* static */
syncer::ImmutableChangeRecordList
ProfileSyncServiceTestHelper::MakeSingletonChangeRecordList(
int64_t node_id,
syncer::ChangeRecord::Action action) {
syncer::ChangeRecord record;
record.action = action;
record.id = node_id;
syncer::ChangeRecordList records(1, record);
return syncer::ImmutableChangeRecordList(&records);
}
/* static */
syncer::ImmutableChangeRecordList
ProfileSyncServiceTestHelper::MakeSingletonDeletionChangeRecordList(
int64_t node_id,
const sync_pb::EntitySpecifics& specifics) {
syncer::ChangeRecord record;
record.action = syncer::ChangeRecord::ACTION_DELETE;
record.id = node_id;
record.specifics = specifics;
syncer::ChangeRecordList records(1, record);
return syncer::ImmutableChangeRecordList(&records);
}
AbstractProfileSyncServiceTest::AbstractProfileSyncServiceTest()
: data_type_thread_("Extra thread") {
CHECK(temp_dir_.CreateUniqueTempDir());
}
AbstractProfileSyncServiceTest::~AbstractProfileSyncServiceTest() {
sync_service_->Shutdown();
}
bool AbstractProfileSyncServiceTest::CreateRoot(ModelType model_type) {
return syncer::TestUserShare::CreateRoot(model_type,
sync_service_->GetUserShare());
}
void AbstractProfileSyncServiceTest::CreateSyncService(
std::unique_ptr<syncer::SyncClient> sync_client,
const base::Closure& initialization_success_callback) {
DCHECK(sync_client);
ProfileSyncService::InitParams init_params =
profile_sync_service_bundle_.CreateBasicInitParams(
ProfileSyncService::AUTO_START, std::move(sync_client));
sync_service_ =
base::MakeUnique<TestProfileSyncService>(std::move(init_params));
syncer::SyncApiComponentFactoryMock* components =
profile_sync_service_bundle_.component_factory();
EXPECT_CALL(*components, CreateSyncEngine(_, _, _, _))
.WillOnce(Return(new SyncEngineForProfileSyncTest(
temp_dir_.GetPath(), sync_service_->GetSyncClient(),
profile_sync_service_bundle_.fake_invalidation_service(),
sync_service_->sync_prefs()->AsWeakPtr(),
initialization_success_callback)));
sync_service_->SetFirstSetupComplete();
}
base::Callback<syncer::SyncService*(void)>
AbstractProfileSyncServiceTest::GetSyncServiceCallback() {
return base::Bind(GetSyncService,
base::Bind(&AbstractProfileSyncServiceTest::sync_service,
base::Unretained(this)));
}
CreateRootHelper::CreateRootHelper(AbstractProfileSyncServiceTest* test,
ModelType model_type)
: callback_(base::Bind(&CreateRootHelper::CreateRootCallback,
base::Unretained(this))),
test_(test),
model_type_(model_type),
success_(false) {}
CreateRootHelper::~CreateRootHelper() {}
const base::Closure& CreateRootHelper::callback() const {
return callback_;
}
bool CreateRootHelper::success() {
return success_;
}
void CreateRootHelper::CreateRootCallback() {
success_ = test_->CreateRoot(model_type_);
}
} // namespace browser_sync
|