1// Copyright (c) 2011 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 "chrome/browser/sync/engine/syncapi.h"
8#include "chrome/browser/sync/glue/autofill_model_associator.h"
9#include "chrome/browser/sync/glue/autofill_profile_model_associator.h"
10#include "chrome/browser/sync/glue/password_model_associator.h"
11#include "chrome/browser/sync/glue/preference_model_associator.h"
12#include "chrome/browser/sync/glue/session_model_associator.h"
13#include "chrome/browser/sync/glue/typed_url_model_associator.h"
14#include "chrome/browser/sync/protocol/sync.pb.h"
15#include "chrome/browser/sync/syncable/directory_manager.h"
16#include "chrome/browser/sync/syncable/syncable.h"
17#include "chrome/browser/sync/test_profile_sync_service.h"
18#include "chrome/browser/sync/util/cryptographer.h"
19#include "chrome/test/profile_mock.h"
20#include "chrome/test/sync/engine/test_id_factory.h"
21
22using browser_sync::TestIdFactory;
23using sync_api::UserShare;
24using syncable::BASE_VERSION;
25using syncable::CREATE;
26using syncable::DirectoryManager;
27using syncable::IS_DEL;
28using syncable::IS_DIR;
29using syncable::IS_UNAPPLIED_UPDATE;
30using syncable::IS_UNSYNCED;
31using syncable::ModelType;
32using syncable::MutableEntry;
33using syncable::SERVER_IS_DIR;
34using syncable::SERVER_VERSION;
35using syncable::SPECIFICS;
36using syncable::ScopedDirLookup;
37using syncable::UNIQUE_SERVER_TAG;
38using syncable::UNITTEST;
39using syncable::WriteTransaction;
40
41const std::string ProfileSyncServiceTestHelper::GetTagForType(
42    ModelType model_type) {
43  switch (model_type) {
44    case syncable::AUTOFILL:
45      return browser_sync::kAutofillTag;
46    case syncable::AUTOFILL_PROFILE:
47      return browser_sync::kAutofillProfileTag;
48    case syncable::PREFERENCES:
49      return browser_sync::kPreferencesTag;
50    case syncable::PASSWORDS:
51      return browser_sync::kPasswordTag;
52    case syncable::NIGORI:
53      return browser_sync::kNigoriTag;
54    case syncable::TYPED_URLS:
55      return browser_sync::kTypedUrlTag;
56    case syncable::SESSIONS:
57      return browser_sync::kSessionsTag;
58    case syncable::BOOKMARKS:
59      return "google_chrome_bookmarks";
60    default:
61      NOTREACHED();
62  }
63  return std::string();
64}
65
66bool ProfileSyncServiceTestHelper::CreateRoot(
67    ModelType model_type, UserShare* user_share,
68    TestIdFactory* ids) {
69  DirectoryManager* dir_manager = user_share->dir_manager.get();
70
71  ScopedDirLookup dir(dir_manager, user_share->name);
72  if (!dir.good())
73    return false;
74
75  std::string tag_name = GetTagForType(model_type);
76
77  WriteTransaction wtrans(dir, UNITTEST, __FILE__, __LINE__);
78  MutableEntry node(&wtrans,
79                    CREATE,
80                    wtrans.root_id(),
81                    tag_name);
82  node.Put(UNIQUE_SERVER_TAG, tag_name);
83  node.Put(IS_DIR, true);
84  node.Put(SERVER_IS_DIR, false);
85  node.Put(IS_UNSYNCED, false);
86  node.Put(IS_UNAPPLIED_UPDATE, false);
87  node.Put(SERVER_VERSION, 20);
88  node.Put(BASE_VERSION, 20);
89  node.Put(IS_DEL, false);
90  node.Put(syncable::ID, ids->MakeServer(tag_name));
91  sync_pb::EntitySpecifics specifics;
92  syncable::AddDefaultExtensionValue(model_type, &specifics);
93  node.Put(SPECIFICS, specifics);
94
95  return true;
96}
97
98AbstractProfileSyncServiceTest::AbstractProfileSyncServiceTest()
99    : ui_thread_(BrowserThread::UI, &message_loop_) {}
100
101AbstractProfileSyncServiceTest::~AbstractProfileSyncServiceTest() {}
102
103bool AbstractProfileSyncServiceTest::CreateRoot(ModelType model_type) {
104  return ProfileSyncServiceTestHelper::CreateRoot(
105      model_type,
106      service_->GetUserShare(),
107      service_->id_factory());
108}
109
110CreateRootTask::CreateRootTask(
111    AbstractProfileSyncServiceTest* test, ModelType model_type)
112    : test_(test), model_type_(model_type), success_(false) {
113}
114
115CreateRootTask::~CreateRootTask() {}
116void CreateRootTask::Run() {
117  success_ = test_->CreateRoot(model_type_);
118}
119
120bool CreateRootTask::success() {
121  return success_;
122}
123