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_factory.h"
24 #include "chrome/browser/prefs/browser_prefs.h"
25 #include "chrome/test/base/testing_pref_service_syncable.h"
26 #include "chrome/test/base/testing_profile.h"
27 #include "components/bookmarks/browser/bookmark_model.h"
28 #include "components/bookmarks/test/bookmark_test_helpers.h"
29 #include "components/history/core/browser/history_service.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"
53 using bookmarks::BookmarkModel
;
58 void GetValueAndQuit(T
* result
, const base::Closure
& quit
, T actual
) {
65 const base::Callback
<void(const base::Callback
<void(T
)>&)>& callback
) {
68 callback
.Run(base::Bind(&GetValueAndQuit
<T
>, &result
, loop
.QuitClosure()));
73 // A pref store that can have its read_error property changed for testing.
74 class TestingPrefStoreWithCustomReadError
: public TestingPrefStore
{
76 TestingPrefStoreWithCustomReadError()
77 : read_error_(PersistentPrefStore::PREF_READ_ERROR_NO_FILE
) {
78 // By default the profile is "new" (NO_FILE means that the profile
79 // wasn't found on disk, so it was created).
81 PrefReadError
GetReadError() const override
{ return read_error_
; }
82 bool IsInitializationComplete() const override
{ return true; }
83 void set_read_error(PrefReadError read_error
) {
84 read_error_
= read_error
;
87 ~TestingPrefStoreWithCustomReadError() override
{}
88 PrefReadError read_error_
;
91 #if defined(ENABLE_EXTENSIONS)
93 const base::FilePath::CharType kExtensionFilePath
[] =
94 FILE_PATH_LITERAL("c:\\foo");
95 #elif defined(OS_POSIX)
96 const base::FilePath::CharType kExtensionFilePath
[] =
97 FILE_PATH_LITERAL("/oo");
100 static scoped_refptr
<extensions::Extension
> CreateExtension(
101 const std::string
& name
,
102 const std::string
& id
,
103 extensions::Manifest::Location location
) {
104 base::DictionaryValue manifest
;
105 manifest
.SetString(extensions::manifest_keys::kVersion
, "1.0.0.0");
106 manifest
.SetString(extensions::manifest_keys::kName
, name
);
108 scoped_refptr
<extensions::Extension
> extension
=
109 extensions::Extension::Create(
110 base::FilePath(kExtensionFilePath
).AppendASCII(name
),
113 extensions::Extension::NO_FLAGS
,
122 class ProfileSigninConfirmationHelperTest
: public testing::Test
{
124 ProfileSigninConfirmationHelperTest()
129 void SetUp() override
{
130 // Create the profile.
131 TestingProfile::Builder builder
;
132 user_prefs_
= new TestingPrefStoreWithCustomReadError
;
133 TestingPrefServiceSyncable
* pref_service
= new TestingPrefServiceSyncable(
134 new TestingPrefStore(),
136 new TestingPrefStore(),
137 new user_prefs::PrefRegistrySyncable(),
138 new PrefNotifierImpl());
139 chrome::RegisterUserProfilePrefs(pref_service
->registry());
140 builder
.SetPrefService(make_scoped_ptr
<PrefServiceSyncable
>(pref_service
));
141 profile_
= builder
.Build();
143 // Initialize the services we check.
144 profile_
->CreateBookmarkModel(true);
145 model_
= BookmarkModelFactory::GetForProfile(profile_
.get());
146 bookmarks::test::WaitForBookmarkModelToLoad(model_
);
147 ASSERT_TRUE(profile_
->CreateHistoryService(true, false));
148 #if defined(ENABLE_EXTENSIONS)
149 extensions::TestExtensionSystem
* system
=
150 static_cast<extensions::TestExtensionSystem
*>(
151 extensions::ExtensionSystem::Get(profile_
.get()));
152 base::CommandLine
command_line(base::CommandLine::NO_PROGRAM
);
153 system
->CreateExtensionService(&command_line
,
154 base::FilePath(kExtensionFilePath
),
159 void TearDown() override
{
160 // TestExtensionSystem uses DeleteSoon, so we need to delete the profile
161 // and then run the message queue to clean up.
163 base::RunLoop().RunUntilIdle();
167 content::TestBrowserThreadBundle thread_bundle_
;
168 scoped_ptr
<TestingProfile
> profile_
;
169 TestingPrefStoreWithCustomReadError
* user_prefs_
;
170 BookmarkModel
* model_
;
172 #if defined OS_CHROMEOS
173 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
174 chromeos::ScopedTestCrosSettings test_cros_settings_
;
175 chromeos::ScopedTestUserManager test_user_manager_
;
179 // http://crbug.com/393149
180 TEST_F(ProfileSigninConfirmationHelperTest
, DISABLED_DoNotPromptForNewProfile
) {
181 // Profile is new and there's no profile data.
185 &ui::CheckShouldPromptForNewProfile
,
189 TEST_F(ProfileSigninConfirmationHelperTest
, PromptForNewProfile_Bookmarks
) {
192 // Profile is new but has bookmarks.
193 model_
->AddURL(model_
->bookmark_bar_node(), 0,
194 base::string16(base::ASCIIToUTF16("foo")),
195 GURL("http://foo.com"));
199 &ui::CheckShouldPromptForNewProfile
,
203 #if defined(ENABLE_EXTENSIONS)
204 TEST_F(ProfileSigninConfirmationHelperTest
, PromptForNewProfile_Extensions
) {
205 ExtensionService
* extensions
=
206 extensions::ExtensionSystem::Get(profile_
.get())->extension_service();
207 ASSERT_TRUE(extensions
);
209 // Profile is new but has synced extensions.
211 // (The web store doesn't count.)
212 scoped_refptr
<extensions::Extension
> webstore
=
213 CreateExtension("web store",
214 extensions::kWebStoreAppId
,
215 extensions::Manifest::COMPONENT
);
216 extensions::ExtensionPrefs::Get(profile_
.get())->AddGrantedPermissions(
217 webstore
->id(), make_scoped_refptr(new extensions::PermissionSet
).get());
218 extensions
->AddExtension(webstore
.get());
219 EXPECT_FALSE(GetCallbackResult(
220 base::Bind(&ui::CheckShouldPromptForNewProfile
, profile_
.get())));
222 scoped_refptr
<extensions::Extension
> extension
=
223 CreateExtension("foo", std::string(), extensions::Manifest::INTERNAL
);
224 extensions::ExtensionPrefs::Get(profile_
.get())->AddGrantedPermissions(
225 extension
->id(), make_scoped_refptr(new extensions::PermissionSet
).get());
226 extensions
->AddExtension(extension
.get());
227 EXPECT_TRUE(GetCallbackResult(
228 base::Bind(&ui::CheckShouldPromptForNewProfile
, profile_
.get())));
232 // http://crbug.com/393149
233 TEST_F(ProfileSigninConfirmationHelperTest
,
234 DISABLED_PromptForNewProfile_History
) {
235 history::HistoryService
* history
= HistoryServiceFactory::GetForProfile(
236 profile_
.get(), ServiceAccessType::EXPLICIT_ACCESS
);
237 ASSERT_TRUE(history
);
239 // Profile is new but has more than $(kHistoryEntriesBeforeNewProfilePrompt)
242 for (int i
= 0; i
< 10; i
++) {
243 base::snprintf(buf
, arraysize(buf
), "http://foo.com/%d", i
);
245 GURL(std::string(buf
)), base::Time::Now(), NULL
, 1,
246 GURL(), history::RedirectList(), ui::PAGE_TRANSITION_LINK
,
247 history::SOURCE_BROWSED
, false);
252 &ui::CheckShouldPromptForNewProfile
,
256 // http://crbug.com/393149
257 TEST_F(ProfileSigninConfirmationHelperTest
,
258 DISABLED_PromptForNewProfile_TypedURLs
) {
259 history::HistoryService
* history
= HistoryServiceFactory::GetForProfile(
260 profile_
.get(), ServiceAccessType::EXPLICIT_ACCESS
);
261 ASSERT_TRUE(history
);
263 // Profile is new but has a typed URL.
265 GURL("http://example.com"), base::Time::Now(), NULL
, 1,
266 GURL(), history::RedirectList(), ui::PAGE_TRANSITION_TYPED
,
267 history::SOURCE_BROWSED
, false);
271 &ui::CheckShouldPromptForNewProfile
,
275 TEST_F(ProfileSigninConfirmationHelperTest
, PromptForNewProfile_Restarted
) {
276 // Browser has been shut down since profile was created.
277 user_prefs_
->set_read_error(PersistentPrefStore::PREF_READ_ERROR_NONE
);
281 &ui::CheckShouldPromptForNewProfile
,