summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/profile_sync_factory_impl_unittest.cc
blob: 56643ac7b5fb822d429a07939b8295d58b251ac5 (plain)
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
// Copyright (c) 2011 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 <vector>

#include "testing/gtest/include/gtest/gtest.h"
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "chrome/browser/sync/engine/syncapi.h"
#include "chrome/browser/sync/glue/data_type_controller.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_factory_impl.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/testing_profile.h"
#include "content/browser/browser_thread.h"

using browser_sync::DataTypeController;

class ProfileSyncFactoryImplTest : public testing::Test {
 protected:
  ProfileSyncFactoryImplTest()
      : ui_thread_(BrowserThread::UI, &message_loop_) {}

  virtual void SetUp() {
    profile_.reset(new TestingProfile());
    FilePath program_path(FILE_PATH_LITERAL("chrome.exe"));
    command_line_.reset(new CommandLine(program_path));
    profile_sync_service_factory_.reset(
        new ProfileSyncFactoryImpl(profile_.get(), command_line_.get()));
  }

  // Returns the collection of default datatypes.
  static std::vector<syncable::ModelType> DefaultDatatypes() {
    std::vector<syncable::ModelType> datatypes;
    datatypes.push_back(syncable::BOOKMARKS);
    datatypes.push_back(syncable::PREFERENCES);
    datatypes.push_back(syncable::AUTOFILL);
    datatypes.push_back(syncable::THEMES);
    datatypes.push_back(syncable::EXTENSIONS);
    datatypes.push_back(syncable::APPS);
    datatypes.push_back(syncable::AUTOFILL_PROFILE);
    datatypes.push_back(syncable::PASSWORDS);
    return datatypes;
  }

  // Returns the number of default datatypes.
  static size_t DefaultDatatypesCount() {
    return DefaultDatatypes().size();
  }

  // Asserts that all the default datatypes are in |map|, except
  // for |exception_type|, which unless it is UNDEFINED, is asserted to
  // not be in |map|.
  static void CheckDefaultDatatypesInMapExcept(
      DataTypeController::StateMap* map,
      syncable::ModelType exception_type) {
    std::vector<syncable::ModelType> defaults = DefaultDatatypes();
    std::vector<syncable::ModelType>::iterator iter;
    for (iter = defaults.begin(); iter != defaults.end(); ++iter) {
      if (exception_type != syncable::UNSPECIFIED && exception_type == *iter)
        EXPECT_EQ(0U, map->count(*iter))
            << *iter << " found in dataypes map, shouldn't be there.";
      else
        EXPECT_EQ(1U, map->count(*iter))
            << *iter << " not found in datatypes map";
    }
  }

  // Asserts that if you apply the command line switch |cmd_switch|,
  // all types are enabled except for |type|, which is disabled.
  void TestSwitchDisablesType(const char* cmd_switch,
                              syncable::ModelType type) {
    command_line_->AppendSwitch(cmd_switch);
    scoped_ptr<ProfileSyncService> pss(
        profile_sync_service_factory_->CreateProfileSyncService(""));
    profile_sync_service_factory_->RegisterDataTypes(pss.get());
    DataTypeController::StateMap controller_states;
    pss->GetDataTypeControllerStates(&controller_states);
    EXPECT_EQ(DefaultDatatypesCount() - 1, controller_states.size());
    CheckDefaultDatatypesInMapExcept(&controller_states, type);
  }

  MessageLoop message_loop_;
  BrowserThread ui_thread_;
  scoped_ptr<Profile> profile_;
  scoped_ptr<CommandLine> command_line_;
  scoped_ptr<ProfileSyncFactoryImpl> profile_sync_service_factory_;
};

TEST_F(ProfileSyncFactoryImplTest, CreatePSSDefault) {
  scoped_ptr<ProfileSyncService> pss(
      profile_sync_service_factory_->CreateProfileSyncService(""));
  profile_sync_service_factory_->RegisterDataTypes(pss.get());
  DataTypeController::StateMap controller_states;
  pss->GetDataTypeControllerStates(&controller_states);
  EXPECT_EQ(DefaultDatatypesCount(), controller_states.size());
  CheckDefaultDatatypesInMapExcept(&controller_states, syncable::UNSPECIFIED);
}

TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisableAutofill) {
  TestSwitchDisablesType(switches::kDisableSyncAutofill,
                         syncable::AUTOFILL);
}

TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisableBookmarks) {
  TestSwitchDisablesType(switches::kDisableSyncBookmarks,
                         syncable::BOOKMARKS);
}

TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisablePreferences) {
  TestSwitchDisablesType(switches::kDisableSyncPreferences,
                         syncable::PREFERENCES);
}

TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisableThemes) {
  TestSwitchDisablesType(switches::kDisableSyncThemes,
                         syncable::THEMES);
}

TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisableExtensions) {
  TestSwitchDisablesType(switches::kDisableSyncExtensions,
                         syncable::EXTENSIONS);
}

TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisableApps) {
  TestSwitchDisablesType(switches::kDisableSyncApps,
                         syncable::APPS);
}

TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisableAutofillProfile) {
  TestSwitchDisablesType(switches::kDisableSyncAutofillProfile,
                         syncable::AUTOFILL_PROFILE);
}

TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisablePasswords) {
  TestSwitchDisablesType(switches::kDisableSyncPasswords,
                         syncable::PASSWORDS);
}