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

Side by Side Diff: chrome/browser/extensions/extension_message_bubble_controller_unittest.cc

Issue 114153003: Add an extension bubble explaining which extensions are in dev mode. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync to head Created 7 years 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/command_line.h"
6 #include "base/strings/string_util.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/extensions/dev_mode_bubble_controller.h"
9 #include "chrome/browser/extensions/extension_function_test_utils.h"
10 #include "chrome/browser/extensions/extension_message_bubble.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/suspicious_extension_bubble_controller.h"
13 #include "chrome/browser/extensions/test_extension_system.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "extensions/common/extension.h"
17 #include "extensions/common/feature_switch.h"
18
19 namespace extensions {
20
21 class TestDelegate {
22 public:
23 TestDelegate()
24 : action_button_callback_count_(0),
25 dismiss_button_callback_count_(0),
26 link_click_callback_count_(0) {
27 }
28
29 // Returns how often the dismiss button has been called.
30 size_t action_click_count() {
31 return action_button_callback_count_;
32 }
33
34 // Returns how often the dismiss button has been called.
35 size_t dismiss_click_count() {
36 return dismiss_button_callback_count_;
37 }
38
39 // Returns how often the link has been clicked.
40 size_t link_click_count() {
41 return link_click_callback_count_;
42 }
43
44 protected:
45 size_t action_button_callback_count_;
46 size_t dismiss_button_callback_count_;
47 size_t link_click_callback_count_;
48 };
49
50 // A test class for the SuspiciousExtensionBubbleController.
51 class TestSuspiciousExtensionBubbleController
52 : public SuspiciousExtensionBubbleController,
53 public TestDelegate {
54 public:
55 explicit TestSuspiciousExtensionBubbleController(Profile* profile)
56 : SuspiciousExtensionBubbleController(profile) {
57 }
58
59 virtual void OnBubbleAction() OVERRIDE {
60 ++action_button_callback_count_;
61 SuspiciousExtensionBubbleController::OnBubbleAction();
62 }
63
64 virtual void OnBubbleDismiss() OVERRIDE {
65 ++dismiss_button_callback_count_;
66 SuspiciousExtensionBubbleController::OnBubbleDismiss();
67 }
68
69 virtual void OnLinkClicked() OVERRIDE {
70 ++link_click_callback_count_;
71 SuspiciousExtensionBubbleController::OnLinkClicked();
72 }
73 };
74
75 // A test class for the DevModeBubbleController.
76 class TestDevModeBubbleController
77 : public DevModeBubbleController,
78 public TestDelegate {
79 public:
80 explicit TestDevModeBubbleController(Profile* profile)
81 : DevModeBubbleController(profile) {
82 }
83
84 virtual void OnBubbleAction() OVERRIDE {
85 ++action_button_callback_count_;
86 DevModeBubbleController::OnBubbleAction();
87 }
88
89 virtual void OnBubbleDismiss() OVERRIDE {
90 ++dismiss_button_callback_count_;
91 DevModeBubbleController::OnBubbleDismiss();
92 }
93
94 virtual void OnLinkClicked() OVERRIDE {
95 ++link_click_callback_count_;
96 DevModeBubbleController::OnLinkClicked();
97 }
98 };
99
100 // A fake bubble used for testing the controller. Takes an action that specifies
101 // what should happen when the bubble is "shown" (the bubble is actually not
102 // shown, the corresponding action is taken immediately).
103 class FakeExtensionMessageBubble : public ExtensionMessageBubble {
104 public:
105 enum ExtensionBubbleAction {
106 BUBBLE_ACTION_CLICK_ACTION_BUTTON = 0,
107 BUBBLE_ACTION_CLICK_DISMISS_BUTTON,
108 BUBBLE_ACTION_CLICK_LINK,
109 };
110
111 FakeExtensionMessageBubble() {}
112
113 void set_action_on_show(ExtensionBubbleAction action) {
114 action_ = action;
115 }
116
117 virtual void Show() OVERRIDE {
118 if (action_ == BUBBLE_ACTION_CLICK_ACTION_BUTTON)
119 action_callback_.Run();
120 else if (action_ == BUBBLE_ACTION_CLICK_DISMISS_BUTTON)
121 dismiss_callback_.Run();
122 else if (action_ == BUBBLE_ACTION_CLICK_LINK)
123 link_callback_.Run();
124 }
125
126 virtual void OnActionButtonClicked(const base::Closure& callback) OVERRIDE {
127 action_callback_ = callback;
128 }
129
130 virtual void OnDismissButtonClicked(const base::Closure& callback) OVERRIDE {
131 dismiss_callback_ = callback;
132 }
133
134 virtual void OnLinkClicked(const base::Closure& callback) OVERRIDE {
135 link_callback_ = callback;
136 }
137
138 private:
139 ExtensionBubbleAction action_;
140
141 base::Closure action_callback_;
142 base::Closure dismiss_callback_;
143 base::Closure link_callback_;
144 };
145
146 class ExtensionMessageBubbleTest : public testing::Test {
147 public:
148 ExtensionMessageBubbleTest() {
149 // The two lines of magical incantation required to get the extension
150 // service to work inside a unit test and access the extension prefs.
151 thread_bundle_.reset(new content::TestBrowserThreadBundle);
152 profile_.reset(new TestingProfile);
153
154 static_cast<TestExtensionSystem*>(
155 ExtensionSystem::Get(profile()))->CreateExtensionService(
156 CommandLine::ForCurrentProcess(),
157 base::FilePath(),
158 false);
159 service_ = profile_->GetExtensionService();
160 service_->Init();
161
162 std::string basic_extension =
163 "{\"name\": \"Extension #\","
164 "\"version\": \"1.0\","
165 "\"manifest_version\": 2}";
166 std::string basic_extension_with_action =
167 "{\"name\": \"Extension #\","
168 "\"version\": \"1.0\","
169 "\"browser_action\": {"
170 " \"default_title\": \"Default title\""
171 "},"
172 "\"manifest_version\": 2}";
173
174 std::string extension_data;
175 base::ReplaceChars(basic_extension_with_action, "#", "1", &extension_data);
176 scoped_refptr<Extension> my_test_extension1(
177 CreateExtension(
178 Manifest::COMMAND_LINE,
179 extension_data,
180 "Autogenerated 1"));
181
182 base::ReplaceChars(basic_extension, "#", "2", &extension_data);
183 scoped_refptr<Extension> my_test_extension2(
184 CreateExtension(
185 Manifest::UNPACKED,
186 extension_data,
187 "Autogenerated 2"));
188
189 base::ReplaceChars(basic_extension, "#", "3", &extension_data);
190 scoped_refptr<Extension> regular_extension(
191 CreateExtension(
192 Manifest::EXTERNAL_POLICY,
193 extension_data,
194 "Autogenerated 3"));
195
196 extension_id1_ = my_test_extension1->id();
197 extension_id2_ = my_test_extension2->id();
198 extension_id3_ = regular_extension->id();
199
200 service_->AddExtension(regular_extension);
201 service_->AddExtension(my_test_extension1);
202 service_->AddExtension(my_test_extension2);
203 }
204 virtual ~ExtensionMessageBubbleTest() {
205 // Make sure the profile is destroyed before the thread bundle.
206 profile_.reset(NULL);
207 }
208
209 virtual void SetUp() {
210 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM));
211 }
212
213 protected:
214 Profile* profile() { return profile_.get(); }
215
216 scoped_refptr<Extension> CreateExtension(
217 Manifest::Location location,
218 const std::string& data,
219 const std::string& id) {
220 scoped_ptr<base::DictionaryValue> parsed_manifest(
221 extension_function_test_utils::ParseDictionary(data));
222 return extension_function_test_utils::CreateExtension(
223 location,
224 parsed_manifest.get(),
225 id);
226 }
227
228 ExtensionService* service_;
229 std::string extension_id1_;
230 std::string extension_id2_;
231 std::string extension_id3_;
232
233 private:
234 scoped_ptr<CommandLine> command_line_;
235 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_;
236 scoped_ptr<TestingProfile> profile_;
237
238 DISALLOW_COPY_AND_ASSIGN(ExtensionMessageBubbleTest);
239 };
240
241 // The feature this is meant to test is only implemented on Windows.
242 #if defined(OS_WIN)
243 #define MAYBE_WipeoutControllerTest WipeoutControllerTest
244 #else
245 #define MAYBE_WipeoutControllerTest DISABLED_WipeoutControllerTest
246 #endif
247
248 TEST_F(ExtensionMessageBubbleTest, MAYBE_WipeoutControllerTest) {
249 // The test base class adds three extensions, and we control two of them in
250 // this test (ids are: extension_id1_ and extension_id2_).
251 scoped_ptr<TestSuspiciousExtensionBubbleController> controller(
252 new TestSuspiciousExtensionBubbleController(profile()));
253 FakeExtensionMessageBubble bubble;
254 bubble.set_action_on_show(
255 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_DISMISS_BUTTON);
256
257 // Validate that we don't have a suppress value for the extensions.
258 ExtensionPrefs* prefs = service_->extension_prefs();
259 EXPECT_FALSE(prefs->HasWipeoutBeenAcknowledged(extension_id1_));
260 EXPECT_FALSE(prefs->HasWipeoutBeenAcknowledged(extension_id2_));
261
262 EXPECT_FALSE(controller->ShouldShow());
263 std::vector<string16> suspicious_extensions = controller->GetExtensionList();
264 EXPECT_EQ(0U, suspicious_extensions.size());
265 EXPECT_EQ(0U, controller->link_click_count());
266 EXPECT_EQ(0U, controller->dismiss_click_count());
267
268 // Now disable an extension, specifying the wipeout flag.
269 service_->DisableExtension(extension_id1_,
270 Extension::DISABLE_NOT_VERIFIED);
271
272 EXPECT_FALSE(prefs->HasWipeoutBeenAcknowledged(extension_id1_));
273 EXPECT_FALSE(prefs->HasWipeoutBeenAcknowledged(extension_id2_));
274 controller.reset(new TestSuspiciousExtensionBubbleController(
275 profile()));
276 EXPECT_TRUE(controller->ShouldShow());
277 suspicious_extensions = controller->GetExtensionList();
278 ASSERT_EQ(1U, suspicious_extensions.size());
279 EXPECT_TRUE(ASCIIToUTF16("Extension 1") == suspicious_extensions[0]);
280 controller->Show(&bubble); // Simulate showing the bubble.
281 EXPECT_EQ(0U, controller->link_click_count());
282 EXPECT_EQ(1U, controller->dismiss_click_count());
283 // Now the acknowledge flag should be set only for the first extension.
284 EXPECT_TRUE(prefs->HasWipeoutBeenAcknowledged(extension_id1_));
285 EXPECT_FALSE(prefs->HasWipeoutBeenAcknowledged(extension_id2_));
286 // Clear the flag.
287 prefs->SetWipeoutAcknowledged(extension_id1_, false);
288 EXPECT_FALSE(prefs->HasWipeoutBeenAcknowledged(extension_id1_));
289
290 // Now disable the other extension and exercise the link click code path.
291 service_->DisableExtension(extension_id2_,
292 Extension::DISABLE_NOT_VERIFIED);
293
294 bubble.set_action_on_show(
295 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_LINK);
296 controller.reset(new TestSuspiciousExtensionBubbleController(
297 profile()));
298 EXPECT_TRUE(controller->ShouldShow());
299 suspicious_extensions = controller->GetExtensionList();
300 ASSERT_EQ(2U, suspicious_extensions.size());
301 EXPECT_TRUE(ASCIIToUTF16("Extension 1") == suspicious_extensions[1]);
302 EXPECT_TRUE(ASCIIToUTF16("Extension 2") == suspicious_extensions[0]);
303 controller->Show(&bubble); // Simulate showing the bubble.
304 EXPECT_EQ(1U, controller->link_click_count());
305 EXPECT_EQ(0U, controller->dismiss_click_count());
306 EXPECT_TRUE(prefs->HasWipeoutBeenAcknowledged(extension_id1_));
307 }
308
309 // The feature this is meant to test is only implemented on Windows.
310 #if defined(OS_WIN)
311 #define MAYBE_DevModeControllerTest DevModeControllerTest
312 #else
313 #define MAYBE_DevModeControllerTest DISABLED_DevModeControllerTest
314 #endif
315
316 TEST_F(ExtensionMessageBubbleTest, MAYBE_DevModeControllerTest) {
317 FeatureSwitch::ScopedOverride force_dev_mode_highlighting(
318 FeatureSwitch::force_dev_mode_highlighting(), true);
319 // The test base class adds three extensions, and we control two of them in
320 // this test (ids are: extension_id1_ and extension_id2_). Extension 1 is a
321 // regular extension, Extension 2 is UNPACKED so it counts as a DevMode
322 // extension.
323 scoped_ptr<TestDevModeBubbleController> controller(
324 new TestDevModeBubbleController(profile()));
325
326 // The list will contain one enabled unpacked extension.
327 EXPECT_TRUE(controller->ShouldShow());
328 std::vector<string16> dev_mode_extensions = controller->GetExtensionList();
329 ASSERT_EQ(2U, dev_mode_extensions.size());
330 EXPECT_TRUE(ASCIIToUTF16("Extension 2") == dev_mode_extensions[0]);
331 EXPECT_TRUE(ASCIIToUTF16("Extension 1") == dev_mode_extensions[1]);
332 EXPECT_EQ(0U, controller->link_click_count());
333 EXPECT_EQ(0U, controller->dismiss_click_count());
334 EXPECT_EQ(0U, controller->action_click_count());
335
336 // Simulate showing the bubble.
337 FakeExtensionMessageBubble bubble;
338 bubble.set_action_on_show(
339 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_DISMISS_BUTTON);
340 controller->Show(&bubble);
341 EXPECT_EQ(0U, controller->link_click_count());
342 EXPECT_EQ(0U, controller->action_click_count());
343 EXPECT_EQ(1U, controller->dismiss_click_count());
344 EXPECT_TRUE(service_->GetExtensionById(extension_id1_, false) != NULL);
345 EXPECT_TRUE(service_->GetExtensionById(extension_id2_, false) != NULL);
346
347 // Do it again, but now press different button (Disable).
348 bubble.set_action_on_show(
349 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_ACTION_BUTTON);
350 controller.reset(new TestDevModeBubbleController(
351 profile()));
352 EXPECT_TRUE(controller->ShouldShow());
353 dev_mode_extensions = controller->GetExtensionList();
354 EXPECT_EQ(2U, dev_mode_extensions.size());
355 controller->Show(&bubble); // Simulate showing the bubble.
356 EXPECT_EQ(0U, controller->link_click_count());
357 EXPECT_EQ(1U, controller->action_click_count());
358 EXPECT_EQ(0U, controller->dismiss_click_count());
359 EXPECT_TRUE(service_->GetExtensionById(extension_id1_, false) == NULL);
360 EXPECT_TRUE(service_->GetExtensionById(extension_id2_, false) == NULL);
361
362 // Re-enable the extensions (disabled by the action button above).
363 service_->EnableExtension(extension_id1_);
364 service_->EnableExtension(extension_id2_);
365
366 // Show the dialog a third time, but now press the learn more link.
367 bubble.set_action_on_show(
368 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_LINK);
369 controller.reset(new TestDevModeBubbleController(
370 profile()));
371 EXPECT_TRUE(controller->ShouldShow());
372 dev_mode_extensions = controller->GetExtensionList();
373 EXPECT_EQ(2U, dev_mode_extensions.size());
374 controller->Show(&bubble); // Simulate showing the bubble.
375 EXPECT_EQ(1U, controller->link_click_count());
376 EXPECT_EQ(0U, controller->action_click_count());
377 EXPECT_EQ(0U, controller->dismiss_click_count());
378 EXPECT_TRUE(service_->GetExtensionById(extension_id1_, false) != NULL);
379 EXPECT_TRUE(service_->GetExtensionById(extension_id2_, false) != NULL);
380
381 // Now disable the unpacked extension.
382 service_->DisableExtension(extension_id1_, Extension::DISABLE_USER_ACTION);
383 service_->DisableExtension(extension_id2_, Extension::DISABLE_USER_ACTION);
384
385 controller.reset(new TestDevModeBubbleController(
386 profile()));
387 EXPECT_FALSE(controller->ShouldShow());
388 dev_mode_extensions = controller->GetExtensionList();
389 EXPECT_EQ(0U, dev_mode_extensions.size());
390 }
391
392 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698