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/file_util.h"
9 #include "base/files/file_path.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/extensions/extension_test_message_listener.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/profiles/profile_manager.h"
21 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
22 #include "chrome/browser/sync/profile_sync_service.h"
23 #include "chrome/browser/sync/profile_sync_service_factory.h"
24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/common/chrome_constants.h"
26 #include "chrome/common/chrome_paths.h"
27 #include "chrome/test/base/testing_profile.h"
28 #include "content/public/browser/browser_context.h"
30 using extensions::PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction;
34 class FakeProfileSyncService : public ProfileSyncService {
36 explicit FakeProfileSyncService(Profile* profile)
41 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
42 ProfileSyncService::MANUAL_START),
43 sync_initialized_(true),
44 initialized_state_violation_(false) {}
46 virtual ~FakeProfileSyncService() {}
48 static BrowserContextKeyedService* BuildFakeProfileSyncService(
49 content::BrowserContext* context) {
50 return new FakeProfileSyncService(static_cast<Profile*>(context));
53 void set_sync_initialized(bool sync_initialized) {
54 sync_initialized_ = sync_initialized;
57 bool initialized_state_violation() { return initialized_state_violation_; }
59 // ProfileSyncService:
60 virtual bool sync_initialized() const OVERRIDE {
61 return sync_initialized_;
64 virtual void AddObserver(
65 ProfileSyncServiceBase::Observer* observer) OVERRIDE {
66 if (sync_initialized_)
67 initialized_state_violation_ = true;
68 // Set sync initialized state to true so the function will run after
69 // OnStateChanged is called.
70 sync_initialized_ = true;
71 base::MessageLoop::current()->PostTask(
73 base::Bind(&ProfileSyncServiceBase::Observer::OnStateChanged,
74 base::Unretained(observer)));
77 virtual syncer::ModelTypeSet GetEncryptedDataTypes() const OVERRIDE {
78 if (!sync_initialized_)
79 initialized_state_violation_ = true;
80 syncer::ModelTypeSet type_set;
81 type_set.Put(syncer::AUTOFILL);
85 virtual syncer::ModelTypeSet GetPreferredDataTypes() const OVERRIDE {
86 if (!sync_initialized_)
87 initialized_state_violation_ = true;
88 syncer::ModelTypeSet preferred_types =
89 syncer::UserSelectableTypes();
90 preferred_types.Remove(syncer::TYPED_URLS);
91 return preferred_types;
95 bool sync_initialized_;
96 // Set to true if a function is called when sync_initialized is in an
98 mutable bool initialized_state_violation_;
100 DISALLOW_COPY_AND_ASSIGN(FakeProfileSyncService);
103 class PreferencesPrivateApiTest : public ExtensionApiTest {
105 PreferencesPrivateApiTest() : browser_(NULL), service_(NULL) {}
106 virtual ~PreferencesPrivateApiTest() {}
108 virtual void SetUpOnMainThread() OVERRIDE {
109 ExtensionApiTest::SetUpOnMainThread();
112 PathService::Get(chrome::DIR_USER_DATA, &path);
113 path = path.AppendASCII("test_profile");
114 if (!base::PathExists(path))
115 CHECK(base::CreateDirectory(path));
118 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
119 browser_sync::SyncPrefs sync_prefs(profile->GetPrefs());
120 sync_prefs.SetKeepEverythingSynced(false);
122 ProfileManager* profile_manager = g_browser_process->profile_manager();
123 profile_manager->RegisterTestingProfile(profile, true, false);
124 browser_ = new Browser(Browser::CreateParams(
125 profile, chrome::HOST_DESKTOP_TYPE_NATIVE));
127 service_ = static_cast<FakeProfileSyncService*>(
128 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
129 profile, &FakeProfileSyncService::BuildFakeProfileSyncService));
132 // Calls GetSyncCategoriesWithoutPassphraseFunction and verifies that the
133 // results returned are the expected ones.
134 void TestGetSyncCategoriesWithoutPassphraseFunction();
138 FakeProfileSyncService* service_;
141 DISALLOW_COPY_AND_ASSIGN(PreferencesPrivateApiTest);
145 PreferencesPrivateApiTest::TestGetSyncCategoriesWithoutPassphraseFunction() {
146 scoped_refptr<PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction>
148 new PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction);
149 ASSERT_TRUE(extension_function_test_utils::RunFunction(
153 extension_function_test_utils::NONE));
154 EXPECT_FALSE(service_->initialized_state_violation());
156 const base::ListValue* result = function->GetResultList();
157 EXPECT_EQ(1u, result->GetSize());
159 const base::ListValue* categories = NULL;
160 ASSERT_TRUE(result->GetList(0, &categories));
161 EXPECT_NE(categories->end(),
162 categories->Find(base::StringValue(chrome::kBookmarksFileName)));
163 EXPECT_NE(categories->end(),
164 categories->Find(base::StringValue(chrome::kPreferencesFilename)));
165 EXPECT_EQ(categories->end(),
166 categories->Find(base::StringValue("Autofill"))) <<
167 "Encrypted categories should not be present";
168 EXPECT_EQ(categories->end(),
169 categories->Find(base::StringValue("Typed URLs"))) <<
170 "Unsynced categories should not be present";;
173 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,
174 GetSyncCategoriesWithoutPassphrase) {
175 TestGetSyncCategoriesWithoutPassphraseFunction();
178 // Verifies that we wait for the sync service to be ready before checking
179 // encryption status.
180 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,
181 GetSyncCategoriesWithoutPassphraseAsynchronous) {
182 service_->set_sync_initialized(false);
183 TestGetSyncCategoriesWithoutPassphraseFunction();