Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / preferences_private / preferences_private_apitest.cc
bloba119636df03a2c0c23aad8a39c2c16b440e1e5a2
1 // Copyright 2014 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.
5 #include "base/basictypes.h"
6 #include "base/bind.h"
7 #include "base/bind_helpers.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/location.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/path_service.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "base/values.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/extensions/api/preferences_private/preferences_private_api.h"
19 #include "chrome/browser/extensions/extension_apitest.h"
20 #include "chrome/browser/extensions/extension_function_test_utils.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/profiles/profile_manager.h"
23 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
24 #include "chrome/browser/sync/profile_sync_components_factory_mock.h"
25 #include "chrome/browser/sync/profile_sync_service.h"
26 #include "chrome/browser/sync/profile_sync_service_factory.h"
27 #include "chrome/browser/ui/browser.h"
28 #include "chrome/common/chrome_constants.h"
29 #include "chrome/common/chrome_paths.h"
30 #include "chrome/test/base/testing_profile.h"
31 #include "components/bookmarks/common/bookmark_constants.h"
32 #include "components/sync_driver/signin_manager_wrapper.h"
33 #include "components/sync_driver/sync_prefs.h"
34 #include "content/public/browser/browser_context.h"
35 #include "extensions/test/extension_test_message_listener.h"
37 #if defined(OS_CHROMEOS)
38 #include "chromeos/chromeos_switches.h"
39 #endif
41 using extensions::PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction;
43 namespace {
45 class FakeProfileSyncService : public ProfileSyncService {
46 public:
47 explicit FakeProfileSyncService(Profile* profile)
48 : ProfileSyncService(
49 scoped_ptr<sync_driver::SyncApiComponentFactory>(
50 new ProfileSyncComponentsFactoryMock()),
51 profile,
52 make_scoped_ptr<SigninManagerWrapper>(NULL),
53 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
54 browser_sync::MANUAL_START),
55 sync_initialized_(true),
56 initialized_state_violation_(false) {}
58 ~FakeProfileSyncService() override {}
60 static scoped_ptr<KeyedService> BuildFakeProfileSyncService(
61 content::BrowserContext* context) {
62 return make_scoped_ptr(
63 new FakeProfileSyncService(static_cast<Profile*>(context)));
66 void set_sync_initialized(bool sync_initialized) {
67 sync_initialized_ = sync_initialized;
70 bool initialized_state_violation() { return initialized_state_violation_; }
72 // ProfileSyncService:
73 bool IsSyncActive() const override { return sync_initialized_; }
75 void AddObserver(sync_driver::SyncServiceObserver* observer) override {
76 if (sync_initialized_)
77 initialized_state_violation_ = true;
78 // Set sync initialized state to true so the function will run after
79 // OnStateChanged is called.
80 sync_initialized_ = true;
81 base::ThreadTaskRunnerHandle::Get()->PostTask(
82 FROM_HERE, base::Bind(&sync_driver::SyncServiceObserver::OnStateChanged,
83 base::Unretained(observer)));
86 syncer::ModelTypeSet GetEncryptedDataTypes() const override {
87 if (!sync_initialized_)
88 initialized_state_violation_ = true;
89 syncer::ModelTypeSet type_set;
90 type_set.Put(syncer::AUTOFILL);
91 return type_set;
94 syncer::ModelTypeSet GetPreferredDataTypes() const override {
95 if (!sync_initialized_)
96 initialized_state_violation_ = true;
97 syncer::ModelTypeSet preferred_types =
98 syncer::UserSelectableTypes();
99 preferred_types.Remove(syncer::TYPED_URLS);
100 return preferred_types;
103 private:
104 bool sync_initialized_;
105 // Set to true if a function is called when sync_initialized is in an
106 // unexpected state.
107 mutable bool initialized_state_violation_;
109 DISALLOW_COPY_AND_ASSIGN(FakeProfileSyncService);
112 class PreferencesPrivateApiTest : public ExtensionApiTest {
113 public:
114 PreferencesPrivateApiTest() : browser_(NULL), service_(NULL) {}
115 ~PreferencesPrivateApiTest() override {}
117 void SetUpCommandLine(base::CommandLine* command_line) override {
118 #if defined(OS_CHROMEOS)
119 command_line->AppendSwitch(
120 chromeos::switches::kIgnoreUserProfileMappingForTests);
121 #endif
124 void SetUpOnMainThread() override {
125 ExtensionApiTest::SetUpOnMainThread();
127 base::FilePath path;
128 PathService::Get(chrome::DIR_USER_DATA, &path);
129 path = path.AppendASCII("test_profile");
130 if (!base::PathExists(path))
131 CHECK(base::CreateDirectory(path));
133 Profile* profile =
134 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
135 sync_driver::SyncPrefs sync_prefs(profile->GetPrefs());
136 sync_prefs.SetKeepEverythingSynced(false);
138 ProfileManager* profile_manager = g_browser_process->profile_manager();
139 profile_manager->RegisterTestingProfile(profile, true, false);
141 service_ = static_cast<FakeProfileSyncService*>(
142 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
143 profile, &FakeProfileSyncService::BuildFakeProfileSyncService));
145 browser_ = new Browser(Browser::CreateParams(
146 profile, chrome::HOST_DESKTOP_TYPE_NATIVE));
149 // Calls GetSyncCategoriesWithoutPassphraseFunction and verifies that the
150 // results returned are the expected ones.
151 void TestGetSyncCategoriesWithoutPassphraseFunction();
153 protected:
154 Browser* browser_;
155 FakeProfileSyncService* service_;
157 private:
158 DISALLOW_COPY_AND_ASSIGN(PreferencesPrivateApiTest);
161 void
162 PreferencesPrivateApiTest::TestGetSyncCategoriesWithoutPassphraseFunction() {
163 scoped_refptr<PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction>
164 function(
165 new PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction);
166 ASSERT_TRUE(extension_function_test_utils::RunFunction(
167 function.get(), "[]", browser_, extension_function_test_utils::NONE));
168 EXPECT_FALSE(service_->initialized_state_violation());
170 const base::ListValue* result = function->GetResultList();
171 EXPECT_EQ(1u, result->GetSize());
173 const base::ListValue* categories = NULL;
174 ASSERT_TRUE(result->GetList(0, &categories));
175 EXPECT_NE(categories->end(),
176 categories->Find(base::StringValue(bookmarks::kBookmarksFileName)));
177 EXPECT_NE(categories->end(),
178 categories->Find(base::StringValue(chrome::kPreferencesFilename)));
179 EXPECT_EQ(categories->end(),
180 categories->Find(base::StringValue("Autofill"))) <<
181 "Encrypted categories should not be present";
182 EXPECT_EQ(categories->end(),
183 categories->Find(base::StringValue("Typed URLs"))) <<
184 "Unsynced categories should not be present";
187 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,
188 GetSyncCategoriesWithoutPassphrase) {
189 TestGetSyncCategoriesWithoutPassphraseFunction();
192 // Verifies that we wait for the sync service to be ready before checking
193 // encryption status.
194 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,
195 GetSyncCategoriesWithoutPassphraseAsynchronous) {
196 service_->set_sync_initialized(false);
197 TestGetSyncCategoriesWithoutPassphraseFunction();
200 } // namespace