1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/sync/abstract_profile_sync_service_test.h"
6
7#include "base/bind.h"
8#include "base/bind_helpers.h"
9#include "base/location.h"
10#include "base/run_loop.h"
11#include "chrome/browser/sync/test_profile_sync_service.h"
12#include "content/public/test/test_utils.h"
13#include "sync/internal_api/public/test/test_user_share.h"
14#include "sync/internal_api/public/write_transaction.h"
15#include "sync/protocol/sync.pb.h"
16#include "sync/util/cryptographer.h"
17
18using syncer::ModelType;
19using syncer::UserShare;
20
21/* static */
22syncer::ImmutableChangeRecordList
23    ProfileSyncServiceTestHelper::MakeSingletonChangeRecordList(
24        int64 node_id, syncer::ChangeRecord::Action action) {
25  syncer::ChangeRecord record;
26  record.action = action;
27  record.id = node_id;
28  syncer::ChangeRecordList records(1, record);
29  return syncer::ImmutableChangeRecordList(&records);
30}
31
32/* static */
33syncer::ImmutableChangeRecordList
34    ProfileSyncServiceTestHelper::MakeSingletonDeletionChangeRecordList(
35        int64 node_id, const sync_pb::EntitySpecifics& specifics) {
36  syncer::ChangeRecord record;
37  record.action = syncer::ChangeRecord::ACTION_DELETE;
38  record.id = node_id;
39  record.specifics = specifics;
40  syncer::ChangeRecordList records(1, record);
41  return syncer::ImmutableChangeRecordList(&records);
42}
43
44AbstractProfileSyncServiceTest::AbstractProfileSyncServiceTest()
45    : thread_bundle_(content::TestBrowserThreadBundle::REAL_DB_THREAD |
46                     content::TestBrowserThreadBundle::REAL_FILE_THREAD |
47                     content::TestBrowserThreadBundle::REAL_IO_THREAD),
48      token_service_(NULL),
49      sync_service_(NULL) {
50}
51
52AbstractProfileSyncServiceTest::~AbstractProfileSyncServiceTest() {}
53
54void AbstractProfileSyncServiceTest::SetUp() {
55}
56
57void AbstractProfileSyncServiceTest::TearDown() {
58  // Pump messages posted by the sync core thread (which may end up
59  // posting on the IO thread).
60  base::RunLoop().RunUntilIdle();
61  content::RunAllPendingInMessageLoop(content::BrowserThread::IO);
62  base::RunLoop().RunUntilIdle();
63}
64
65bool AbstractProfileSyncServiceTest::CreateRoot(ModelType model_type) {
66  return syncer::TestUserShare::CreateRoot(model_type,
67                                           sync_service_->GetUserShare());
68}
69
70// static
71BrowserContextKeyedService* AbstractProfileSyncServiceTest::BuildTokenService(
72    content::BrowserContext* profile) {
73  return new TokenService;
74}
75
76CreateRootHelper::CreateRootHelper(AbstractProfileSyncServiceTest* test,
77                                   ModelType model_type)
78    : callback_(base::Bind(&CreateRootHelper::CreateRootCallback,
79                           base::Unretained(this))),
80      test_(test),
81      model_type_(model_type),
82      success_(false) {
83}
84
85CreateRootHelper::~CreateRootHelper() {
86}
87
88const base::Closure& CreateRootHelper::callback() const {
89  return callback_;
90}
91
92bool CreateRootHelper::success() {
93  return success_;
94}
95
96void CreateRootHelper::CreateRootCallback() {
97  success_ = test_->CreateRoot(model_type_);
98}
99