1 // Copyright 2013 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 "chrome/browser/ui/sync/profile_signin_confirmation_helper.h"
7 #include "base/basictypes.h"
9 #include "base/bind_helpers.h"
10 #include "base/callback.h"
11 #include "base/command_line.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/prefs/pref_notifier_impl.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/prefs/testing_pref_service.h"
18 #include "base/run_loop.h"
19 #include "base/strings/string16.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
23 #include "chrome/browser/history/history_service.h"
24 #include "chrome/browser/history/history_service_factory.h"
25 #include "chrome/browser/prefs/browser_prefs.h"
26 #include "chrome/test/base/testing_pref_service_syncable.h"
27 #include "chrome/test/base/testing_profile.h"
28 #include "components/bookmarks/browser/bookmark_model.h"
29 #include "components/bookmarks/test/bookmark_test_helpers.h"
30 #include "components/pref_registry/pref_registry_syncable.h"
31 #include "content/public/test/test_browser_thread_bundle.h"
32 #include "content/public/test/test_utils.h"
33 #include "testing/gmock/include/gmock/gmock.h"
34 #include "testing/gtest/include/gtest/gtest.h"
36 #if defined(OS_CHROMEOS)
37 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
38 #include "chrome/browser/chromeos/settings/cros_settings.h"
39 #include "chrome/browser/chromeos/settings/device_settings_service.h"
42 #if defined(ENABLE_EXTENSIONS)
43 #include "chrome/browser/extensions/extension_service.h"
44 #include "chrome/browser/extensions/test_extension_system.h"
45 #include "chrome/common/extensions/extension_constants.h"
46 #include "extensions/browser/extension_prefs.h"
47 #include "extensions/common/constants.h"
48 #include "extensions/common/extension.h"
49 #include "extensions/common/manifest_constants.h"
50 #include "extensions/common/permissions/permission_set.h"
56 void GetValueAndQuit(T* result, const base::Closure& quit, T actual) {
63 const base::Callback<void(const base::Callback<void(T)>&)>& callback) {
66 callback.Run(base::Bind(&GetValueAndQuit<T>, &result, loop.QuitClosure()));
71 // A pref store that can have its read_error property changed for testing.
72 class TestingPrefStoreWithCustomReadError : public TestingPrefStore {
74 TestingPrefStoreWithCustomReadError()
75 : read_error_(PersistentPrefStore::PREF_READ_ERROR_NO_FILE) {
76 // By default the profile is "new" (NO_FILE means that the profile
77 // wasn't found on disk, so it was created).
79 PrefReadError GetReadError() const override { return read_error_; }
80 bool IsInitializationComplete() const override { return true; }
81 void set_read_error(PrefReadError read_error) {
82 read_error_ = read_error;
85 ~TestingPrefStoreWithCustomReadError() override {}
86 PrefReadError read_error_;
89 #if defined(ENABLE_EXTENSIONS)
91 const base::FilePath::CharType kExtensionFilePath[] =
92 FILE_PATH_LITERAL("c:\\foo");
93 #elif defined(OS_POSIX)
94 const base::FilePath::CharType kExtensionFilePath[] =
95 FILE_PATH_LITERAL("/oo");
98 static scoped_refptr<extensions::Extension> CreateExtension(
99 const std::string& name,
100 const std::string& id) {
101 base::DictionaryValue manifest;
102 manifest.SetString(extensions::manifest_keys::kVersion, "1.0.0.0");
103 manifest.SetString(extensions::manifest_keys::kName, name);
105 scoped_refptr<extensions::Extension> extension =
106 extensions::Extension::Create(
107 base::FilePath(kExtensionFilePath).AppendASCII(name),
108 extensions::Manifest::INTERNAL,
110 extensions::Extension::NO_FLAGS,
119 class ProfileSigninConfirmationHelperTest : public testing::Test {
121 ProfileSigninConfirmationHelperTest()
126 void SetUp() override {
127 // Create the profile.
128 TestingProfile::Builder builder;
129 user_prefs_ = new TestingPrefStoreWithCustomReadError;
130 TestingPrefServiceSyncable* pref_service = new TestingPrefServiceSyncable(
131 new TestingPrefStore(),
133 new TestingPrefStore(),
134 new user_prefs::PrefRegistrySyncable(),
135 new PrefNotifierImpl());
136 chrome::RegisterUserProfilePrefs(pref_service->registry());
137 builder.SetPrefService(make_scoped_ptr<PrefServiceSyncable>(pref_service));
138 profile_ = builder.Build();
140 // Initialize the services we check.
141 profile_->CreateBookmarkModel(true);
142 model_ = BookmarkModelFactory::GetForProfile(profile_.get());
143 bookmarks::test::WaitForBookmarkModelToLoad(model_);
144 ASSERT_TRUE(profile_->CreateHistoryService(true, false));
145 #if defined(ENABLE_EXTENSIONS)
146 extensions::TestExtensionSystem* system =
147 static_cast<extensions::TestExtensionSystem*>(
148 extensions::ExtensionSystem::Get(profile_.get()));
149 CommandLine command_line(CommandLine::NO_PROGRAM);
150 system->CreateExtensionService(&command_line,
151 base::FilePath(kExtensionFilePath),
156 void TearDown() override {
157 // TestExtensionSystem uses DeleteSoon, so we need to delete the profile
158 // and then run the message queue to clean up.
160 base::RunLoop().RunUntilIdle();
164 content::TestBrowserThreadBundle thread_bundle_;
165 scoped_ptr<TestingProfile> profile_;
166 TestingPrefStoreWithCustomReadError* user_prefs_;
167 BookmarkModel* model_;
169 #if defined OS_CHROMEOS
170 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
171 chromeos::ScopedTestCrosSettings test_cros_settings_;
172 chromeos::ScopedTestUserManager test_user_manager_;
176 // http://crbug.com/393149
177 TEST_F(ProfileSigninConfirmationHelperTest, DISABLED_DoNotPromptForNewProfile) {
178 // Profile is new and there's no profile data.
182 &ui::CheckShouldPromptForNewProfile,
186 TEST_F(ProfileSigninConfirmationHelperTest, PromptForNewProfile_Bookmarks) {
189 // Profile is new but has bookmarks.
190 model_->AddURL(model_->bookmark_bar_node(), 0,
191 base::string16(base::ASCIIToUTF16("foo")),
192 GURL("http://foo.com"));
196 &ui::CheckShouldPromptForNewProfile,
200 #if defined(ENABLE_EXTENSIONS)
201 TEST_F(ProfileSigninConfirmationHelperTest, PromptForNewProfile_Extensions) {
202 ExtensionService* extensions =
203 extensions::ExtensionSystem::Get(profile_.get())->extension_service();
204 ASSERT_TRUE(extensions);
206 // Profile is new but has synced extensions.
208 // (The web store doesn't count.)
209 scoped_refptr<extensions::Extension> webstore =
210 CreateExtension("web store", extensions::kWebStoreAppId);
211 extensions::ExtensionPrefs::Get(profile_.get())->AddGrantedPermissions(
212 webstore->id(), make_scoped_refptr(new extensions::PermissionSet).get());
213 extensions->AddExtension(webstore.get());
214 EXPECT_FALSE(GetCallbackResult(
215 base::Bind(&ui::CheckShouldPromptForNewProfile, profile_.get())));
217 scoped_refptr<extensions::Extension> extension =
218 CreateExtension("foo", std::string());
219 extensions::ExtensionPrefs::Get(profile_.get())->AddGrantedPermissions(
220 extension->id(), make_scoped_refptr(new extensions::PermissionSet).get());
221 extensions->AddExtension(extension.get());
222 EXPECT_TRUE(GetCallbackResult(
223 base::Bind(&ui::CheckShouldPromptForNewProfile, profile_.get())));
227 // http://crbug.com/393149
228 TEST_F(ProfileSigninConfirmationHelperTest,
229 DISABLED_PromptForNewProfile_History) {
230 HistoryService* history = HistoryServiceFactory::GetForProfile(
232 Profile::EXPLICIT_ACCESS);
233 ASSERT_TRUE(history);
235 // Profile is new but has more than $(kHistoryEntriesBeforeNewProfilePrompt)
238 for (int i = 0; i < 10; i++) {
239 base::snprintf(buf, arraysize(buf), "http://foo.com/%d", i);
241 GURL(std::string(buf)), base::Time::Now(), NULL, 1,
242 GURL(), history::RedirectList(), ui::PAGE_TRANSITION_LINK,
243 history::SOURCE_BROWSED, false);
248 &ui::CheckShouldPromptForNewProfile,
252 // http://crbug.com/393149
253 TEST_F(ProfileSigninConfirmationHelperTest,
254 DISABLED_PromptForNewProfile_TypedURLs) {
255 HistoryService* history = HistoryServiceFactory::GetForProfile(
257 Profile::EXPLICIT_ACCESS);
258 ASSERT_TRUE(history);
260 // Profile is new but has a typed URL.
262 GURL("http://example.com"), base::Time::Now(), NULL, 1,
263 GURL(), history::RedirectList(), ui::PAGE_TRANSITION_TYPED,
264 history::SOURCE_BROWSED, false);
268 &ui::CheckShouldPromptForNewProfile,
272 TEST_F(ProfileSigninConfirmationHelperTest, PromptForNewProfile_Restarted) {
273 // Browser has been shut down since profile was created.
274 user_prefs_->set_read_error(PersistentPrefStore::PREF_READ_ERROR_NONE);
278 &ui::CheckShouldPromptForNewProfile,