Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: chrome/browser/profile_resetter/profile_resetter_unittest.cc

Issue 16980002: Reset profile: unpin all pinned tabs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Clean up all the checkboxes Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/profile_resetter/profile_resetter.h" 5 #include "chrome/browser/profile_resetter/profile_resetter.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/prefs/pref_service.h" 8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/extensions/extension_service_unittest.h" 9 #include "chrome/browser/extensions/extension_service_unittest.h"
10 #include "chrome/browser/extensions/tab_helper.h"
10 #include "chrome/browser/prefs/session_startup_pref.h" 11 #include "chrome/browser/prefs/session_startup_pref.h"
11 #include "chrome/browser/search_engines/template_url_service_test_util.h" 12 #include "chrome/browser/search_engines/template_url_service_test_util.h"
12 #include "chrome/browser/themes/theme_service.h" 13 #include "chrome/browser/themes/theme_service.h"
13 #include "chrome/browser/themes/theme_service_factory.h" 14 #include "chrome/browser/themes/theme_service_factory.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/common/extensions/extension.h" 16 #include "chrome/common/extensions/extension.h"
15 #include "chrome/common/extensions/extension_manifest_constants.h" 17 #include "chrome/common/extensions/extension_manifest_constants.h"
16 #include "chrome/common/pref_names.h" 18 #include "chrome/common/pref_names.h"
19 #include "chrome/test/base/browser_with_test_window_test.h"
17 #include "chrome/test/base/testing_profile.h" 20 #include "chrome/test/base/testing_profile.h"
21 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/test_utils.h" 22 #include "content/public/test/test_utils.h"
19 #include "testing/gmock/include/gmock/gmock.h" 23 #include "testing/gmock/include/gmock/gmock.h"
20 24
21 namespace { 25 namespace {
22 26
23 using extensions::Extension; 27 using extensions::Extension;
24 using extensions::Manifest; 28 using extensions::Manifest;
25 29
26 class MockObject { 30 class MockObject {
27 public: 31 public:
28 void RunLoop() { 32 void RunLoop() {
29 EXPECT_CALL(*this, Callback()); 33 EXPECT_CALL(*this, Callback());
30 runner_ = new content::MessageLoopRunner; 34 runner_ = new content::MessageLoopRunner;
31 runner_->Run(); 35 runner_->Run();
32 } 36 }
33 37
34 void StopLoop() { 38 void StopLoop() {
35 DCHECK(runner_.get()); 39 DCHECK(runner_.get());
36 Callback(); 40 Callback();
37 runner_->Quit(); 41 runner_->Quit();
38 } 42 }
39 43
40 private: 44 private:
41 MOCK_METHOD0(Callback, void(void)); 45 MOCK_METHOD0(Callback, void(void));
42 46
43 scoped_refptr<content::MessageLoopRunner> runner_; 47 scoped_refptr<content::MessageLoopRunner> runner_;
44 }; 48 };
45 49
46 class ProfileResetterTest : public testing::Test { 50 class ProfileResetterTestBase {
51 public:
52 ProfileResetterTestBase();
53 ~ProfileResetterTestBase();
47 protected: 54 protected:
48 ProfileResetterTest(); 55 testing::StrictMock<MockObject> mock_object_;
49 ~ProfileResetterTest(); 56 scoped_ptr<ProfileResetter> resetter_;
50 57
58 private:
59 DISALLOW_COPY_AND_ASSIGN(ProfileResetterTestBase);
60 };
61
62 ProfileResetterTestBase::ProfileResetterTestBase() {}
63
64 ProfileResetterTestBase::~ProfileResetterTestBase() {}
65
66 class ProfileResetterTest : public testing::Test,
67 public ProfileResetterTestBase {
68 protected:
51 // testing::Test: 69 // testing::Test:
52 virtual void SetUp() OVERRIDE; 70 virtual void SetUp() OVERRIDE;
53 virtual void TearDown() OVERRIDE; 71 virtual void TearDown() OVERRIDE;
54 72
55 testing::StrictMock<MockObject> mock_object_;
56 TemplateURLServiceTestUtil test_util_; 73 TemplateURLServiceTestUtil test_util_;
57 scoped_ptr<ProfileResetter> resetter_;
58
59 DISALLOW_COPY_AND_ASSIGN(ProfileResetterTest);
60 }; 74 };
61 75
62 ProfileResetterTest::ProfileResetterTest() {}
63
64 ProfileResetterTest::~ProfileResetterTest() {}
65
66 void ProfileResetterTest::SetUp() { 76 void ProfileResetterTest::SetUp() {
67 test_util_.SetUp(); 77 test_util_.SetUp();
68 resetter_.reset(new ProfileResetter(test_util_.profile())); 78 resetter_.reset(new ProfileResetter(test_util_.profile()));
69 } 79 }
70 80
71 void ProfileResetterTest::TearDown() { 81 void ProfileResetterTest::TearDown() {
72 test_util_.TearDown(); 82 test_util_.TearDown();
73 } 83 }
74 84
75 // ExtensionsResetTest sets up the extension service. 85 // ExtensionsResetTest sets up the extension service.
76 class ExtensionsResetTest : public ExtensionServiceTestBase { 86 class ExtensionsResetTest : public ExtensionServiceTestBase,
87 public ProfileResetterTestBase {
77 protected: 88 protected:
78 virtual void SetUp() OVERRIDE; 89 virtual void SetUp() OVERRIDE;
79
80 testing::StrictMock<MockObject> mock_object_;
81 scoped_ptr<ProfileResetter> resetter_;
82 }; 90 };
83 91
84 void ExtensionsResetTest::SetUp() { 92 void ExtensionsResetTest::SetUp() {
85 ExtensionServiceTestBase::SetUp(); 93 ExtensionServiceTestBase::SetUp();
86 InitializeEmptyExtensionService(); 94 InitializeEmptyExtensionService();
87 resetter_.reset(new ProfileResetter(profile_.get())); 95 resetter_.reset(new ProfileResetter(profile_.get()));
88 } 96 }
89 97
90 // Returns a barebones test Extension object with the specified |name|. The 98 // Returns a barebones test Extension object with the specified |name|. The
91 // returned extension will include background permission iff 99 // returned extension will include background permission iff
92 // |background_permission| is true. 100 // |background_permission| is true.
93 scoped_refptr<Extension> CreateExtension(const std::string& name, 101 scoped_refptr<Extension> CreateExtension(const std::string& name,
94 const base::FilePath& path, 102 const base::FilePath& path,
95 Manifest::Location location, 103 Manifest::Location location,
96 bool theme) { 104 bool theme) {
97 DictionaryValue manifest; 105 DictionaryValue manifest;
98 manifest.SetString(extension_manifest_keys::kVersion, "1.0.0.0"); 106 manifest.SetString(extension_manifest_keys::kVersion, "1.0.0.0");
99 manifest.SetString(extension_manifest_keys::kName, name); 107 manifest.SetString(extension_manifest_keys::kName, name);
108 manifest.SetString("app.launch.web_url", "http://www.google.com");
100 if (theme) 109 if (theme)
101 manifest.Set(extension_manifest_keys::kTheme, new DictionaryValue()); 110 manifest.Set(extension_manifest_keys::kTheme, new DictionaryValue());
102 std::string error; 111 std::string error;
103 scoped_refptr<Extension> extension = Extension::Create( 112 scoped_refptr<Extension> extension = Extension::Create(
104 path, 113 path,
105 location, 114 location,
106 manifest, 115 manifest,
107 Extension::NO_FLAGS, 116 Extension::NO_FLAGS,
108 &error); 117 &error);
109 EXPECT_TRUE(extension.get() != NULL) << error; 118 EXPECT_TRUE(extension.get() != NULL) << error;
110 return extension; 119 return extension;
111 } 120 }
112 121
122 class PinnedTabsResetTest : public BrowserWithTestWindowTest,
123 public ProfileResetterTestBase {
124 protected:
125 virtual void SetUp() OVERRIDE;
126
127 content::WebContents* CreateWebContents();
128 };
129
130 void PinnedTabsResetTest::SetUp() {
131 BrowserWithTestWindowTest::SetUp();
132 resetter_.reset(new ProfileResetter(profile()));
133 }
134
135 content::WebContents* PinnedTabsResetTest::CreateWebContents() {
136 return content::WebContents::Create(
137 content::WebContents::CreateParams(profile()));
138 }
139
140 /********************* Tests *********************/
113 141
114 TEST_F(ProfileResetterTest, ResetDefaultSearchEngine) { 142 TEST_F(ProfileResetterTest, ResetDefaultSearchEngine) {
115 resetter_->Reset( 143 resetter_->Reset(
116 ProfileResetter::DEFAULT_SEARCH_ENGINE, 144 ProfileResetter::DEFAULT_SEARCH_ENGINE,
117 ProfileResetter::DISABLE_EXTENSIONS,
118 base::Bind(&MockObject::StopLoop, base::Unretained(&mock_object_))); 145 base::Bind(&MockObject::StopLoop, base::Unretained(&mock_object_)));
119 mock_object_.RunLoop(); 146 mock_object_.RunLoop();
120 } 147 }
121 148
122 TEST_F(ProfileResetterTest, ResetHomepage) { 149 TEST_F(ProfileResetterTest, ResetHomepage) {
123 PrefService* prefs = test_util_.profile()->GetPrefs(); 150 PrefService* prefs = test_util_.profile()->GetPrefs();
124 DCHECK(prefs); 151 DCHECK(prefs);
125 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, false); 152 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, false);
126 prefs->SetString(prefs::kHomePage, "http://google.com"); 153 prefs->SetString(prefs::kHomePage, "http://google.com");
127 prefs->SetBoolean(prefs::kShowHomeButton, true); 154 prefs->SetBoolean(prefs::kShowHomeButton, true);
128 155
129 resetter_->Reset( 156 resetter_->Reset(
130 ProfileResetter::HOMEPAGE, 157 ProfileResetter::HOMEPAGE,
131 ProfileResetter::DISABLE_EXTENSIONS,
132 base::Bind(&MockObject::StopLoop, base::Unretained(&mock_object_))); 158 base::Bind(&MockObject::StopLoop, base::Unretained(&mock_object_)));
133 mock_object_.RunLoop(); 159 mock_object_.RunLoop();
134 160
135 EXPECT_TRUE(prefs->GetBoolean(prefs::kHomePageIsNewTabPage)); 161 EXPECT_TRUE(prefs->GetBoolean(prefs::kHomePageIsNewTabPage));
136 EXPECT_EQ(std::string(), prefs->GetString(prefs::kHomePage)); 162 EXPECT_EQ(std::string(), prefs->GetString(prefs::kHomePage));
137 EXPECT_FALSE(prefs->GetBoolean(prefs::kShowHomeButton)); 163 EXPECT_FALSE(prefs->GetBoolean(prefs::kShowHomeButton));
138 } 164 }
139 165
140 TEST_F(ProfileResetterTest, ResetContentSettings) { 166 TEST_F(ProfileResetterTest, ResetContentSettings) {
141 resetter_->Reset( 167 resetter_->Reset(
142 ProfileResetter::CONTENT_SETTINGS, 168 ProfileResetter::CONTENT_SETTINGS,
143 ProfileResetter::DISABLE_EXTENSIONS,
144 base::Bind(&MockObject::StopLoop, base::Unretained(&mock_object_))); 169 base::Bind(&MockObject::StopLoop, base::Unretained(&mock_object_)));
145 mock_object_.RunLoop(); 170 mock_object_.RunLoop();
146 } 171 }
147 172
148 TEST_F(ProfileResetterTest, ResetCookiesAndSiteData) { 173 TEST_F(ProfileResetterTest, ResetCookiesAndSiteData) {
149 resetter_->Reset( 174 resetter_->Reset(
150 ProfileResetter::COOKIES_AND_SITE_DATA, 175 ProfileResetter::COOKIES_AND_SITE_DATA,
151 ProfileResetter::DISABLE_EXTENSIONS,
152 base::Bind(&MockObject::StopLoop, base::Unretained(&mock_object_))); 176 base::Bind(&MockObject::StopLoop, base::Unretained(&mock_object_)));
153 mock_object_.RunLoop(); 177 mock_object_.RunLoop();
154 } 178 }
155 179
156 TEST_F(ExtensionsResetTest, ResetExtensionsByDisabling) { 180 TEST_F(ExtensionsResetTest, ResetExtensionsByDisabling) {
157 base::ScopedTempDir temp_dir; 181 base::ScopedTempDir temp_dir;
158 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 182 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
159 183
160 scoped_refptr<Extension> theme = CreateExtension("example1", temp_dir.path(), 184 scoped_refptr<Extension> theme = CreateExtension("example1", temp_dir.path(),
161 Manifest::INVALID_LOCATION, 185 Manifest::INVALID_LOCATION,
162 true); 186 true);
163 service_->FinishInstallationForTest(theme.get()); 187 service_->FinishInstallationForTest(theme.get());
164 // Let ThemeService finish creating the theme pack. 188 // Let ThemeService finish creating the theme pack.
165 base::MessageLoop::current()->RunUntilIdle(); 189 base::MessageLoop::current()->RunUntilIdle();
166 190
167 #if !defined(OS_ANDROID)
168 // ThemeService isn't compiled for Android. 191 // ThemeService isn't compiled for Android.
169 ThemeService* theme_service = 192 ThemeService* theme_service =
170 ThemeServiceFactory::GetForProfile(profile_.get()); 193 ThemeServiceFactory::GetForProfile(profile_.get());
171 EXPECT_FALSE(theme_service->UsingDefaultTheme()); 194 EXPECT_FALSE(theme_service->UsingDefaultTheme());
172 #endif
173 195
174 scoped_refptr<Extension> ext2 = CreateExtension( 196 scoped_refptr<Extension> ext2 = CreateExtension(
175 "example2", 197 "example2",
176 base::FilePath(FILE_PATH_LITERAL("//nonexistent")), 198 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
177 Manifest::INVALID_LOCATION, 199 Manifest::INVALID_LOCATION,
178 false); 200 false);
179 service_->AddExtension(ext2.get()); 201 service_->AddExtension(ext2.get());
180 // Components and external policy extensions shouldn't be deleted. 202 // Components and external policy extensions shouldn't be deleted.
181 scoped_refptr<Extension> ext3 = CreateExtension( 203 scoped_refptr<Extension> ext3 = CreateExtension(
182 "example3", 204 "example3",
183 base::FilePath(FILE_PATH_LITERAL("//nonexistent2")), 205 base::FilePath(FILE_PATH_LITERAL("//nonexistent2")),
184 Manifest::COMPONENT, 206 Manifest::COMPONENT,
185 false); 207 false);
186 service_->AddExtension(ext3.get()); 208 service_->AddExtension(ext3.get());
187 scoped_refptr<Extension> ext4 = 209 scoped_refptr<Extension> ext4 =
188 CreateExtension("example4", 210 CreateExtension("example4",
189 base::FilePath(FILE_PATH_LITERAL("//nonexistent3")), 211 base::FilePath(FILE_PATH_LITERAL("//nonexistent3")),
190 Manifest::EXTERNAL_POLICY_DOWNLOAD, 212 Manifest::EXTERNAL_POLICY_DOWNLOAD,
191 false); 213 false);
192 service_->AddExtension(ext4.get()); 214 service_->AddExtension(ext4.get());
193 EXPECT_EQ(4u, service_->extensions()->size()); 215 EXPECT_EQ(4u, service_->extensions()->size());
194 216
195 resetter_->Reset( 217 resetter_->Reset(
196 ProfileResetter::EXTENSIONS, 218 ProfileResetter::EXTENSIONS,
197 ProfileResetter::DISABLE_EXTENSIONS,
198 base::Bind(&MockObject::StopLoop, base::Unretained(&mock_object_))); 219 base::Bind(&MockObject::StopLoop, base::Unretained(&mock_object_)));
199 mock_object_.RunLoop(); 220 mock_object_.RunLoop();
200 221
201 EXPECT_EQ(2u, service_->extensions()->size()); 222 EXPECT_EQ(2u, service_->extensions()->size());
202 EXPECT_TRUE(service_->extensions()->Contains(ext3->id())); 223 EXPECT_TRUE(service_->extensions()->Contains(ext3->id()));
203 EXPECT_TRUE(service_->extensions()->Contains(ext4->id())); 224 EXPECT_TRUE(service_->extensions()->Contains(ext4->id()));
204 #if !defined(OS_ANDROID)
205 EXPECT_TRUE(theme_service->UsingDefaultTheme()); 225 EXPECT_TRUE(theme_service->UsingDefaultTheme());
206 #endif
207 } 226 }
208 227
209 TEST_F(ProfileResetterTest, ResetStartPage) { 228 TEST_F(ProfileResetterTest, ResetStartPage) {
210 PrefService* prefs = test_util_.profile()->GetPrefs(); 229 PrefService* prefs = test_util_.profile()->GetPrefs();
211 DCHECK(prefs); 230 DCHECK(prefs);
212 231
213 SessionStartupPref startup_pref(SessionStartupPref::URLS); 232 SessionStartupPref startup_pref(SessionStartupPref::URLS);
214 startup_pref.urls.push_back(GURL("http://foo")); 233 startup_pref.urls.push_back(GURL("http://foo"));
215 startup_pref.urls.push_back(GURL("http://bar")); 234 startup_pref.urls.push_back(GURL("http://bar"));
216 SessionStartupPref::SetStartupPref(prefs, startup_pref); 235 SessionStartupPref::SetStartupPref(prefs, startup_pref);
217 236
218 resetter_->Reset( 237 resetter_->Reset(
219 ProfileResetter::STARTUP_PAGE, 238 ProfileResetter::STARTUP_PAGES,
220 ProfileResetter::DISABLE_EXTENSIONS,
221 base::Bind(&MockObject::StopLoop, base::Unretained(&mock_object_))); 239 base::Bind(&MockObject::StopLoop, base::Unretained(&mock_object_)));
222 mock_object_.RunLoop(); 240 mock_object_.RunLoop();
223 241
224 startup_pref = SessionStartupPref::GetStartupPref(prefs); 242 startup_pref = SessionStartupPref::GetStartupPref(prefs);
225 EXPECT_EQ(SessionStartupPref::GetDefaultStartupType(), startup_pref.type); 243 EXPECT_EQ(SessionStartupPref::GetDefaultStartupType(), startup_pref.type);
226 EXPECT_EQ(std::vector<GURL>(), startup_pref.urls); 244 EXPECT_EQ(std::vector<GURL>(), startup_pref.urls);
227 } 245 }
228 246
247 TEST_F(PinnedTabsResetTest, ResetPinnedTabs) {
248 scoped_refptr<Extension> extension_app = CreateExtension(
249 "hello!",
250 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
251 Manifest::INVALID_LOCATION,
252 false);
253 content::WebContents* contents1 = CreateWebContents();
254 extensions::TabHelper::CreateForWebContents(contents1);
255 extensions::TabHelper::FromWebContents(contents1)->
256 SetExtensionApp(extension_app.get());
257 content::WebContents* contents2 = CreateWebContents();
258 content::WebContents* contents3 = CreateWebContents();
259 content::WebContents* contents4 = CreateWebContents();
260 TabStripModel* tab_strip_model = browser()->tab_strip_model();
261
262 tab_strip_model->AppendWebContents(contents4, true);
263 tab_strip_model->AppendWebContents(contents3, true);
264 tab_strip_model->AppendWebContents(contents2, true);
265 tab_strip_model->SetTabPinned(2, true);
266 tab_strip_model->AppendWebContents(contents1, true);
267 tab_strip_model->SetTabPinned(3, true);
268
269 EXPECT_EQ(contents2, tab_strip_model->GetWebContentsAt(0));
270 EXPECT_EQ(contents1, tab_strip_model->GetWebContentsAt(1));
271 EXPECT_EQ(contents3, tab_strip_model->GetWebContentsAt(2));
272 EXPECT_EQ(contents4, tab_strip_model->GetWebContentsAt(3));
273 EXPECT_EQ(3, tab_strip_model->IndexOfFirstNonMiniTab());
274
275 resetter_->Reset(
276 ProfileResetter::PINNED_TABS,
277 base::Bind(&MockObject::StopLoop, base::Unretained(&mock_object_)));
278 mock_object_.RunLoop();
279
280 EXPECT_EQ(contents1, tab_strip_model->GetWebContentsAt(0));
281 EXPECT_EQ(contents2, tab_strip_model->GetWebContentsAt(1));
282 EXPECT_EQ(contents3, tab_strip_model->GetWebContentsAt(2));
283 EXPECT_EQ(contents4, tab_strip_model->GetWebContentsAt(3));
284 EXPECT_EQ(1, tab_strip_model->IndexOfFirstNonMiniTab());
285 }
286
229 TEST_F(ProfileResetterTest, ResetFewFlags) { 287 TEST_F(ProfileResetterTest, ResetFewFlags) {
230 // mock_object_ is a StrictMock, so we verify that it is called only once. 288 // mock_object_ is a StrictMock, so we verify that it is called only once.
231 resetter_->Reset( 289 resetter_->Reset(
232 ProfileResetter::DEFAULT_SEARCH_ENGINE | ProfileResetter::HOMEPAGE, 290 ProfileResetter::DEFAULT_SEARCH_ENGINE | ProfileResetter::HOMEPAGE,
233 ProfileResetter::DISABLE_EXTENSIONS,
234 base::Bind(&MockObject::StopLoop, base::Unretained(&mock_object_))); 291 base::Bind(&MockObject::StopLoop, base::Unretained(&mock_object_)));
235 mock_object_.RunLoop(); 292 mock_object_.RunLoop();
236 } 293 }
237 294
238 } // namespace 295 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698