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.
4
5 #include "base/basictypes.h"
6 #include "base/bind.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/managed_user_signin_manager_wrapper.h"
23 #include "chrome/browser/sync/profile_sync_service.h"
24 #include "chrome/browser/sync/profile_sync_service_factory.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
33 #if defined(OS_CHROMEOS)
34 #include "chromeos/chromeos_switches.h"
35 #endif
36
37 using extensions::PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction;
38
39 namespace {
40
41 class FakeProfileSyncService : public ProfileSyncService {
42 public:
FakeProfileSyncService(Profile * profile)43 explicit FakeProfileSyncService(Profile* profile)
44 : ProfileSyncService(
45 NULL,
46 profile,
47 make_scoped_ptr<ManagedUserSigninManagerWrapper>(NULL),
48 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
49 browser_sync::MANUAL_START),
50 sync_initialized_(true),
51 initialized_state_violation_(false) {}
52
~FakeProfileSyncService()53 virtual ~FakeProfileSyncService() {}
54
BuildFakeProfileSyncService(content::BrowserContext * context)55 static KeyedService* BuildFakeProfileSyncService(
56 content::BrowserContext* context) {
57 return new FakeProfileSyncService(static_cast<Profile*>(context));
58 }
59
set_sync_initialized(bool sync_initialized)60 void set_sync_initialized(bool sync_initialized) {
61 sync_initialized_ = sync_initialized;
62 }
63
initialized_state_violation()64 bool initialized_state_violation() { return initialized_state_violation_; }
65
66 // ProfileSyncService:
sync_initialized() const67 virtual bool sync_initialized() const OVERRIDE {
68 return sync_initialized_;
69 }
70
AddObserver(ProfileSyncServiceBase::Observer * observer)71 virtual void AddObserver(
72 ProfileSyncServiceBase::Observer* observer) OVERRIDE {
73 if (sync_initialized_)
74 initialized_state_violation_ = true;
75 // Set sync initialized state to true so the function will run after
76 // OnStateChanged is called.
77 sync_initialized_ = true;
78 base::MessageLoop::current()->PostTask(
79 FROM_HERE,
80 base::Bind(&ProfileSyncServiceBase::Observer::OnStateChanged,
81 base::Unretained(observer)));
82 }
83
GetEncryptedDataTypes() const84 virtual syncer::ModelTypeSet GetEncryptedDataTypes() const OVERRIDE {
85 if (!sync_initialized_)
86 initialized_state_violation_ = true;
87 syncer::ModelTypeSet type_set;
88 type_set.Put(syncer::AUTOFILL);
89 return type_set;
90 }
91
GetPreferredDataTypes() const92 virtual syncer::ModelTypeSet GetPreferredDataTypes() const OVERRIDE {
93 if (!sync_initialized_)
94 initialized_state_violation_ = true;
95 syncer::ModelTypeSet preferred_types =
96 syncer::UserSelectableTypes();
97 preferred_types.Remove(syncer::TYPED_URLS);
98 return preferred_types;
99 }
100
101 private:
102 bool sync_initialized_;
103 // Set to true if a function is called when sync_initialized is in an
104 // unexpected state.
105 mutable bool initialized_state_violation_;
106
107 DISALLOW_COPY_AND_ASSIGN(FakeProfileSyncService);
108 };
109
110 class PreferencesPrivateApiTest : public ExtensionApiTest {
111 public:
PreferencesPrivateApiTest()112 PreferencesPrivateApiTest() : browser_(NULL), service_(NULL) {}
~PreferencesPrivateApiTest()113 virtual ~PreferencesPrivateApiTest() {}
114
SetUpCommandLine(CommandLine * command_line)115 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
116 #if defined(OS_CHROMEOS)
117 command_line->AppendSwitch(
118 chromeos::switches::kIgnoreUserProfileMappingForTests);
119 #endif
120 }
121
SetUpOnMainThread()122 virtual void SetUpOnMainThread() OVERRIDE {
123 ExtensionApiTest::SetUpOnMainThread();
124
125 base::FilePath path;
126 PathService::Get(chrome::DIR_USER_DATA, &path);
127 path = path.AppendASCII("test_profile");
128 if (!base::PathExists(path))
129 CHECK(base::CreateDirectory(path));
130
131 Profile* profile =
132 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
133 sync_driver::SyncPrefs sync_prefs(profile->GetPrefs());
134 sync_prefs.SetKeepEverythingSynced(false);
135
136 ProfileManager* profile_manager = g_browser_process->profile_manager();
137 profile_manager->RegisterTestingProfile(profile, true, false);
138
139 service_ = static_cast<FakeProfileSyncService*>(
140 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
141 profile, &FakeProfileSyncService::BuildFakeProfileSyncService));
142
143 browser_ = new Browser(Browser::CreateParams(
144 profile, chrome::HOST_DESKTOP_TYPE_NATIVE));
145 }
146
147 // Calls GetSyncCategoriesWithoutPassphraseFunction and verifies that the
148 // results returned are the expected ones.
149 void TestGetSyncCategoriesWithoutPassphraseFunction();
150
151 protected:
152 Browser* browser_;
153 FakeProfileSyncService* service_;
154
155 private:
156 DISALLOW_COPY_AND_ASSIGN(PreferencesPrivateApiTest);
157 };
158
159 void
TestGetSyncCategoriesWithoutPassphraseFunction()160 PreferencesPrivateApiTest::TestGetSyncCategoriesWithoutPassphraseFunction() {
161 scoped_refptr<PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction>
162 function(
163 new PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction);
164 ASSERT_TRUE(extension_function_test_utils::RunFunction(
165 function,
166 "[]",
167 browser_,
168 extension_function_test_utils::NONE));
169 EXPECT_FALSE(service_->initialized_state_violation());
170
171 const base::ListValue* result = function->GetResultList();
172 EXPECT_EQ(1u, result->GetSize());
173
174 const base::ListValue* categories = NULL;
175 ASSERT_TRUE(result->GetList(0, &categories));
176 EXPECT_NE(categories->end(),
177 categories->Find(base::StringValue(bookmarks::kBookmarksFileName)));
178 EXPECT_NE(categories->end(),
179 categories->Find(base::StringValue(chrome::kPreferencesFilename)));
180 EXPECT_EQ(categories->end(),
181 categories->Find(base::StringValue("Autofill"))) <<
182 "Encrypted categories should not be present";
183 EXPECT_EQ(categories->end(),
184 categories->Find(base::StringValue("Typed URLs"))) <<
185 "Unsynced categories should not be present";
186 }
187
IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,GetSyncCategoriesWithoutPassphrase)188 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,
189 GetSyncCategoriesWithoutPassphrase) {
190 TestGetSyncCategoriesWithoutPassphraseFunction();
191 }
192
193 // Verifies that we wait for the sync service to be ready before checking
194 // encryption status.
IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,GetSyncCategoriesWithoutPassphraseAsynchronous)195 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,
196 GetSyncCategoriesWithoutPassphraseAsynchronous) {
197 service_->set_sync_initialized(false);
198 TestGetSyncCategoriesWithoutPassphraseFunction();
199 }
200
201 } // namespace
202