Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / sync / profile_signin_confirmation_helper_unittest.cc
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.
4
5 #include "chrome/browser/ui/sync/profile_signin_confirmation_helper.h"
6
7 #include "base/basictypes.h"
8 #include "base/bind.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"
35
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"
40 #endif
41
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"
51 #endif
52
53 namespace {
54
55 template<typename T>
56 void GetValueAndQuit(T* result, const base::Closure& quit, T actual) {
57   *result = actual;
58   quit.Run();
59 }
60
61 template<typename T>
62 T GetCallbackResult(
63     const base::Callback<void(const base::Callback<void(T)>&)>& callback) {
64   T result = false;
65   base::RunLoop loop;
66   callback.Run(base::Bind(&GetValueAndQuit<T>, &result, loop.QuitClosure()));
67   loop.Run();
68   return result;
69 }
70
71 // A pref store that can have its read_error property changed for testing.
72 class TestingPrefStoreWithCustomReadError : public TestingPrefStore {
73  public:
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).
78   }
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;
83   }
84  private:
85   ~TestingPrefStoreWithCustomReadError() override {}
86   PrefReadError read_error_;
87 };
88
89 #if defined(ENABLE_EXTENSIONS)
90 #if defined(OS_WIN)
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");
96 #endif
97
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);
104   std::string error;
105   scoped_refptr<extensions::Extension> extension =
106     extensions::Extension::Create(
107         base::FilePath(kExtensionFilePath).AppendASCII(name),
108         extensions::Manifest::INTERNAL,
109         manifest,
110         extensions::Extension::NO_FLAGS,
111         id,
112         &error);
113   return extension;
114 }
115 #endif
116
117 }  // namespace
118
119 class ProfileSigninConfirmationHelperTest : public testing::Test {
120  public:
121   ProfileSigninConfirmationHelperTest()
122       : user_prefs_(NULL),
123         model_(NULL) {
124   }
125
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(),
132         user_prefs_,
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();
139
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),
152                                    false);
153 #endif
154   }
155
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.
159     profile_.reset();
160     base::RunLoop().RunUntilIdle();
161   }
162
163  protected:
164   content::TestBrowserThreadBundle thread_bundle_;
165   scoped_ptr<TestingProfile> profile_;
166   TestingPrefStoreWithCustomReadError* user_prefs_;
167   BookmarkModel* model_;
168
169 #if defined OS_CHROMEOS
170   chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
171   chromeos::ScopedTestCrosSettings test_cros_settings_;
172   chromeos::ScopedTestUserManager test_user_manager_;
173 #endif
174 };
175
176 // http://crbug.com/393149
177 TEST_F(ProfileSigninConfirmationHelperTest, DISABLED_DoNotPromptForNewProfile) {
178   // Profile is new and there's no profile data.
179   EXPECT_FALSE(
180       GetCallbackResult(
181           base::Bind(
182               &ui::CheckShouldPromptForNewProfile,
183               profile_.get())));
184 }
185
186 TEST_F(ProfileSigninConfirmationHelperTest, PromptForNewProfile_Bookmarks) {
187   ASSERT_TRUE(model_);
188
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"));
193   EXPECT_TRUE(
194       GetCallbackResult(
195           base::Bind(
196               &ui::CheckShouldPromptForNewProfile,
197               profile_.get())));
198 }
199
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);
205
206   // Profile is new but has synced extensions.
207
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())));
216
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())));
224 }
225 #endif
226
227 // http://crbug.com/393149
228 TEST_F(ProfileSigninConfirmationHelperTest,
229        DISABLED_PromptForNewProfile_History) {
230   HistoryService* history = HistoryServiceFactory::GetForProfile(
231       profile_.get(),
232       Profile::EXPLICIT_ACCESS);
233   ASSERT_TRUE(history);
234
235   // Profile is new but has more than $(kHistoryEntriesBeforeNewProfilePrompt)
236   // history items.
237   char buf[18];
238   for (int i = 0; i < 10; i++) {
239     base::snprintf(buf, arraysize(buf), "http://foo.com/%d", i);
240     history->AddPage(
241         GURL(std::string(buf)), base::Time::Now(), NULL, 1,
242         GURL(), history::RedirectList(), ui::PAGE_TRANSITION_LINK,
243         history::SOURCE_BROWSED, false);
244   }
245   EXPECT_TRUE(
246       GetCallbackResult(
247           base::Bind(
248               &ui::CheckShouldPromptForNewProfile,
249               profile_.get())));
250 }
251
252 // http://crbug.com/393149
253 TEST_F(ProfileSigninConfirmationHelperTest,
254        DISABLED_PromptForNewProfile_TypedURLs) {
255   HistoryService* history = HistoryServiceFactory::GetForProfile(
256       profile_.get(),
257       Profile::EXPLICIT_ACCESS);
258   ASSERT_TRUE(history);
259
260   // Profile is new but has a typed URL.
261   history->AddPage(
262       GURL("http://example.com"), base::Time::Now(), NULL, 1,
263       GURL(), history::RedirectList(), ui::PAGE_TRANSITION_TYPED,
264       history::SOURCE_BROWSED, false);
265   EXPECT_TRUE(
266       GetCallbackResult(
267           base::Bind(
268               &ui::CheckShouldPromptForNewProfile,
269               profile_.get())));
270 }
271
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);
275   EXPECT_TRUE(
276       GetCallbackResult(
277           base::Bind(
278               &ui::CheckShouldPromptForNewProfile,
279               profile_.get())));
280 }