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"
7 #include "base/bind_helpers.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/path_service.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/extensions/api/preferences_private/preferences_private_api.h"
16 #include "chrome/browser/extensions/extension_apitest.h"
17 #include "chrome/browser/extensions/extension_function_test_utils.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
21 #include "chrome/browser/sync/profile_sync_components_factory_mock.h"
22 #include "chrome/browser/sync/profile_sync_service.h"
23 #include "chrome/browser/sync/profile_sync_service_factory.h"
24 #include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h"
25 #include "chrome/browser/ui/browser.h"
26 #include "chrome/common/chrome_constants.h"
27 #include "chrome/common/chrome_paths.h"
28 #include "chrome/test/base/testing_profile.h"
29 #include "components/bookmarks/common/bookmark_constants.h"
30 #include "components/sync_driver/sync_prefs.h"
31 #include "content/public/browser/browser_context.h"
32 #include "extensions/test/extension_test_message_listener.h"
34 #if defined(OS_CHROMEOS)
35 #include "chromeos/chromeos_switches.h"
38 using extensions::PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction;
42 class FakeProfileSyncService : public ProfileSyncService {
44 explicit FakeProfileSyncService(Profile* profile)
46 scoped_ptr<ProfileSyncComponentsFactory>(
47 new ProfileSyncComponentsFactoryMock()),
49 make_scoped_ptr<SupervisedUserSigninManagerWrapper>(NULL),
50 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
51 browser_sync::MANUAL_START),
52 sync_initialized_(true),
53 initialized_state_violation_(false) {}
55 ~FakeProfileSyncService() override {}
57 static KeyedService* BuildFakeProfileSyncService(
58 content::BrowserContext* context) {
59 return new FakeProfileSyncService(static_cast<Profile*>(context));
62 void set_sync_initialized(bool sync_initialized) {
63 sync_initialized_ = sync_initialized;
66 bool initialized_state_violation() { return initialized_state_violation_; }
68 // ProfileSyncService:
69 bool SyncActive() const override { return sync_initialized_; }
71 void AddObserver(ProfileSyncServiceBase::Observer* observer) override {
72 if (sync_initialized_)
73 initialized_state_violation_ = true;
74 // Set sync initialized state to true so the function will run after
75 // OnStateChanged is called.
76 sync_initialized_ = true;
77 base::MessageLoop::current()->PostTask(
79 base::Bind(&ProfileSyncServiceBase::Observer::OnStateChanged,
80 base::Unretained(observer)));
83 syncer::ModelTypeSet GetEncryptedDataTypes() const override {
84 if (!sync_initialized_)
85 initialized_state_violation_ = true;
86 syncer::ModelTypeSet type_set;
87 type_set.Put(syncer::AUTOFILL);
91 syncer::ModelTypeSet GetPreferredDataTypes() const override {
92 if (!sync_initialized_)
93 initialized_state_violation_ = true;
94 syncer::ModelTypeSet preferred_types =
95 syncer::UserSelectableTypes();
96 preferred_types.Remove(syncer::TYPED_URLS);
97 return preferred_types;
101 bool sync_initialized_;
102 // Set to true if a function is called when sync_initialized is in an
104 mutable bool initialized_state_violation_;
106 DISALLOW_COPY_AND_ASSIGN(FakeProfileSyncService);
109 class PreferencesPrivateApiTest : public ExtensionApiTest {
111 PreferencesPrivateApiTest() : browser_(NULL), service_(NULL) {}
112 ~PreferencesPrivateApiTest() override {}
114 void SetUpCommandLine(CommandLine* command_line) override {
115 #if defined(OS_CHROMEOS)
116 command_line->AppendSwitch(
117 chromeos::switches::kIgnoreUserProfileMappingForTests);
121 void SetUpOnMainThread() override {
122 ExtensionApiTest::SetUpOnMainThread();
125 PathService::Get(chrome::DIR_USER_DATA, &path);
126 path = path.AppendASCII("test_profile");
127 if (!base::PathExists(path))
128 CHECK(base::CreateDirectory(path));
131 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
132 sync_driver::SyncPrefs sync_prefs(profile->GetPrefs());
133 sync_prefs.SetKeepEverythingSynced(false);
135 ProfileManager* profile_manager = g_browser_process->profile_manager();
136 profile_manager->RegisterTestingProfile(profile, true, false);
138 service_ = static_cast<FakeProfileSyncService*>(
139 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
140 profile, &FakeProfileSyncService::BuildFakeProfileSyncService));
142 browser_ = new Browser(Browser::CreateParams(
143 profile, chrome::HOST_DESKTOP_TYPE_NATIVE));
146 // Calls GetSyncCategoriesWithoutPassphraseFunction and verifies that the
147 // results returned are the expected ones.
148 void TestGetSyncCategoriesWithoutPassphraseFunction();
152 FakeProfileSyncService* service_;
155 DISALLOW_COPY_AND_ASSIGN(PreferencesPrivateApiTest);
159 PreferencesPrivateApiTest::TestGetSyncCategoriesWithoutPassphraseFunction() {
160 scoped_refptr<PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction>
162 new PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction);
163 ASSERT_TRUE(extension_function_test_utils::RunFunction(
164 function.get(), "[]", browser_, extension_function_test_utils::NONE));
165 EXPECT_FALSE(service_->initialized_state_violation());
167 const base::ListValue* result = function->GetResultList();
168 EXPECT_EQ(1u, result->GetSize());
170 const base::ListValue* categories = NULL;
171 ASSERT_TRUE(result->GetList(0, &categories));
172 EXPECT_NE(categories->end(),
173 categories->Find(base::StringValue(bookmarks::kBookmarksFileName)));
174 EXPECT_NE(categories->end(),
175 categories->Find(base::StringValue(chrome::kPreferencesFilename)));
176 EXPECT_EQ(categories->end(),
177 categories->Find(base::StringValue("Autofill"))) <<
178 "Encrypted categories should not be present";
179 EXPECT_EQ(categories->end(),
180 categories->Find(base::StringValue("Typed URLs"))) <<
181 "Unsynced categories should not be present";
184 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,
185 GetSyncCategoriesWithoutPassphrase) {
186 TestGetSyncCategoriesWithoutPassphraseFunction();
189 // Verifies that we wait for the sync service to be ready before checking
190 // encryption status.
191 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,
192 GetSyncCategoriesWithoutPassphraseAsynchronous) {
193 service_->set_sync_initialized(false);
194 TestGetSyncCategoriesWithoutPassphraseFunction();