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 "components/bookmarks/core/common/bookmark_constants.h"
29 #include "components/sync_driver/sync_prefs.h"
30 #include "content/public/browser/browser_context.h"
32 using extensions::PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction;
36 class FakeProfileSyncService : public ProfileSyncService {
38 explicit FakeProfileSyncService(Profile* profile)
43 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
44 browser_sync::MANUAL_START),
45 sync_initialized_(true),
46 initialized_state_violation_(false) {}
48 virtual ~FakeProfileSyncService() {}
50 static KeyedService* BuildFakeProfileSyncService(
51 content::BrowserContext* context) {
52 return new FakeProfileSyncService(static_cast<Profile*>(context));
55 void set_sync_initialized(bool sync_initialized) {
56 sync_initialized_ = sync_initialized;
59 bool initialized_state_violation() { return initialized_state_violation_; }
61 // ProfileSyncService:
62 virtual bool sync_initialized() const OVERRIDE {
63 return sync_initialized_;
66 virtual void AddObserver(
67 ProfileSyncServiceBase::Observer* observer) OVERRIDE {
68 if (sync_initialized_)
69 initialized_state_violation_ = true;
70 // Set sync initialized state to true so the function will run after
71 // OnStateChanged is called.
72 sync_initialized_ = true;
73 base::MessageLoop::current()->PostTask(
75 base::Bind(&ProfileSyncServiceBase::Observer::OnStateChanged,
76 base::Unretained(observer)));
79 virtual syncer::ModelTypeSet GetEncryptedDataTypes() const OVERRIDE {
80 if (!sync_initialized_)
81 initialized_state_violation_ = true;
82 syncer::ModelTypeSet type_set;
83 type_set.Put(syncer::AUTOFILL);
87 virtual syncer::ModelTypeSet GetPreferredDataTypes() const OVERRIDE {
88 if (!sync_initialized_)
89 initialized_state_violation_ = true;
90 syncer::ModelTypeSet preferred_types =
91 syncer::UserSelectableTypes();
92 preferred_types.Remove(syncer::TYPED_URLS);
93 return preferred_types;
97 bool sync_initialized_;
98 // Set to true if a function is called when sync_initialized is in an
100 mutable bool initialized_state_violation_;
102 DISALLOW_COPY_AND_ASSIGN(FakeProfileSyncService);
105 class PreferencesPrivateApiTest : public ExtensionApiTest {
107 PreferencesPrivateApiTest() : browser_(NULL), service_(NULL) {}
108 virtual ~PreferencesPrivateApiTest() {}
110 virtual void SetUpOnMainThread() OVERRIDE {
111 ExtensionApiTest::SetUpOnMainThread();
114 PathService::Get(chrome::DIR_USER_DATA, &path);
115 path = path.AppendASCII("test_profile");
116 if (!base::PathExists(path))
117 CHECK(base::CreateDirectory(path));
120 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
121 sync_driver::SyncPrefs sync_prefs(profile->GetPrefs());
122 sync_prefs.SetKeepEverythingSynced(false);
124 ProfileManager* profile_manager = g_browser_process->profile_manager();
125 profile_manager->RegisterTestingProfile(profile, true, false);
127 service_ = static_cast<FakeProfileSyncService*>(
128 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
129 profile, &FakeProfileSyncService::BuildFakeProfileSyncService));
131 browser_ = new Browser(Browser::CreateParams(
132 profile, chrome::HOST_DESKTOP_TYPE_NATIVE));
135 // Calls GetSyncCategoriesWithoutPassphraseFunction and verifies that the
136 // results returned are the expected ones.
137 void TestGetSyncCategoriesWithoutPassphraseFunction();
141 FakeProfileSyncService* service_;
144 DISALLOW_COPY_AND_ASSIGN(PreferencesPrivateApiTest);
148 PreferencesPrivateApiTest::TestGetSyncCategoriesWithoutPassphraseFunction() {
149 scoped_refptr<PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction>
151 new PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction);
152 ASSERT_TRUE(extension_function_test_utils::RunFunction(
156 extension_function_test_utils::NONE));
157 EXPECT_FALSE(service_->initialized_state_violation());
159 const base::ListValue* result = function->GetResultList();
160 EXPECT_EQ(1u, result->GetSize());
162 const base::ListValue* categories = NULL;
163 ASSERT_TRUE(result->GetList(0, &categories));
164 EXPECT_NE(categories->end(),
165 categories->Find(base::StringValue(bookmarks::kBookmarksFileName)));
166 EXPECT_NE(categories->end(),
167 categories->Find(base::StringValue(chrome::kPreferencesFilename)));
168 EXPECT_EQ(categories->end(),
169 categories->Find(base::StringValue("Autofill"))) <<
170 "Encrypted categories should not be present";
171 EXPECT_EQ(categories->end(),
172 categories->Find(base::StringValue("Typed URLs"))) <<
173 "Unsynced categories should not be present";
176 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,
177 GetSyncCategoriesWithoutPassphrase) {
178 TestGetSyncCategoriesWithoutPassphraseFunction();
181 // Verifies that we wait for the sync service to be ready before checking
182 // encryption status.
183 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,
184 GetSyncCategoriesWithoutPassphraseAsynchronous) {
185 service_->set_sync_initialized(false);
186 TestGetSyncCategoriesWithoutPassphraseFunction();