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.
5 #include "base/command_line.h"
6 #include "base/strings/string_number_conversions.h"
7 #include "base/strings/string_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "chrome/browser/extensions/dev_mode_bubble_controller.h"
11 #include "chrome/browser/extensions/extension_function_test_utils.h"
12 #include "chrome/browser/extensions/extension_message_bubble.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/ntp_overridden_bubble_controller.h"
15 #include "chrome/browser/extensions/proxy_overridden_bubble_controller.h"
16 #include "chrome/browser/extensions/settings_api_bubble_controller.h"
17 #include "chrome/browser/extensions/suspicious_extension_bubble_controller.h"
18 #include "chrome/browser/extensions/test_extension_system.h"
19 #include "chrome/common/chrome_version_info.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "extensions/browser/api_test_utils.h"
24 #include "extensions/browser/extension_pref_value_map.h"
25 #include "extensions/browser/extension_pref_value_map_factory.h"
26 #include "extensions/browser/extension_prefs.h"
27 #include "extensions/browser/extension_registry.h"
28 #include "extensions/browser/extension_system.h"
29 #include "extensions/browser/uninstall_reason.h"
30 #include "extensions/common/extension.h"
31 #include "extensions/common/extension_builder.h"
32 #include "extensions/common/feature_switch.h"
33 #include "extensions/common/value_builder.h"
34 #include "testing/gtest/include/gtest/gtest.h"
38 const char kId1
[] = "iccfkkhkfiphcjdakkmcjmkfboccmndk";
39 const char kId2
[] = "ajjhifimiemdpmophmkkkcijegphclbl";
40 const char kId3
[] = "ioibbbfddncmmabjmpokikkeiofalaek";
44 namespace extensions
{
49 : action_button_callback_count_(0),
50 dismiss_button_callback_count_(0),
51 link_click_callback_count_(0) {
54 // Returns how often the dismiss button has been called.
55 size_t action_click_count() {
56 return action_button_callback_count_
;
59 // Returns how often the dismiss button has been called.
60 size_t dismiss_click_count() {
61 return dismiss_button_callback_count_
;
64 // Returns how often the link has been clicked.
65 size_t link_click_count() {
66 return link_click_callback_count_
;
70 size_t action_button_callback_count_
;
71 size_t dismiss_button_callback_count_
;
72 size_t link_click_callback_count_
;
75 // A test class for the SuspiciousExtensionBubbleController.
76 class TestSuspiciousExtensionBubbleController
77 : public SuspiciousExtensionBubbleController
,
80 explicit TestSuspiciousExtensionBubbleController(Profile
* profile
)
81 : SuspiciousExtensionBubbleController(profile
) {
84 void OnBubbleAction() override
{
85 ++action_button_callback_count_
;
86 SuspiciousExtensionBubbleController::OnBubbleAction();
89 void OnBubbleDismiss() override
{
90 ++dismiss_button_callback_count_
;
91 SuspiciousExtensionBubbleController::OnBubbleDismiss();
94 void OnLinkClicked() override
{
95 ++link_click_callback_count_
;
96 SuspiciousExtensionBubbleController::OnLinkClicked();
100 // A test class for the DevModeBubbleController.
101 class TestDevModeBubbleController
102 : public DevModeBubbleController
,
103 public TestDelegate
{
105 explicit TestDevModeBubbleController(Profile
* profile
)
106 : DevModeBubbleController(profile
) {
109 void OnBubbleAction() override
{
110 ++action_button_callback_count_
;
111 DevModeBubbleController::OnBubbleAction();
114 void OnBubbleDismiss() override
{
115 ++dismiss_button_callback_count_
;
116 DevModeBubbleController::OnBubbleDismiss();
119 void OnLinkClicked() override
{
120 ++link_click_callback_count_
;
121 DevModeBubbleController::OnLinkClicked();
125 // A test class for the SettingsApiBubbleController.
126 class TestSettingsApiBubbleController
: public SettingsApiBubbleController
,
127 public TestDelegate
{
129 TestSettingsApiBubbleController(Profile
* profile
,
130 SettingsApiOverrideType type
)
131 : SettingsApiBubbleController(profile
, type
) {}
133 void OnBubbleAction() override
{
134 ++action_button_callback_count_
;
135 SettingsApiBubbleController::OnBubbleAction();
138 void OnBubbleDismiss() override
{
139 ++dismiss_button_callback_count_
;
140 SettingsApiBubbleController::OnBubbleDismiss();
143 void OnLinkClicked() override
{
144 ++link_click_callback_count_
;
145 SettingsApiBubbleController::OnLinkClicked();
149 // A test class for the NtpOverriddenBubbleController.
150 class TestNtpOverriddenBubbleController
151 : public NtpOverriddenBubbleController
,
152 public TestDelegate
{
154 explicit TestNtpOverriddenBubbleController(Profile
* profile
)
155 : NtpOverriddenBubbleController(profile
) {
158 void OnBubbleAction() override
{
159 ++action_button_callback_count_
;
160 NtpOverriddenBubbleController::OnBubbleAction();
163 void OnBubbleDismiss() override
{
164 ++dismiss_button_callback_count_
;
165 NtpOverriddenBubbleController::OnBubbleDismiss();
168 void OnLinkClicked() override
{
169 ++link_click_callback_count_
;
170 NtpOverriddenBubbleController::OnLinkClicked();
174 // A test class for the ProxyOverriddenBubbleController.
175 class TestProxyOverriddenBubbleController
176 : public ProxyOverriddenBubbleController
,
177 public TestDelegate
{
179 explicit TestProxyOverriddenBubbleController(Profile
* profile
)
180 : ProxyOverriddenBubbleController(profile
) {
183 void OnBubbleAction() override
{
184 ++action_button_callback_count_
;
185 ProxyOverriddenBubbleController::OnBubbleAction();
188 void OnBubbleDismiss() override
{
189 ++dismiss_button_callback_count_
;
190 ProxyOverriddenBubbleController::OnBubbleDismiss();
193 void OnLinkClicked() override
{
194 ++link_click_callback_count_
;
195 ProxyOverriddenBubbleController::OnLinkClicked();
199 // A fake bubble used for testing the controller. Takes an action that specifies
200 // what should happen when the bubble is "shown" (the bubble is actually not
201 // shown, the corresponding action is taken immediately).
202 class FakeExtensionMessageBubble
: public ExtensionMessageBubble
{
204 enum ExtensionBubbleAction
{
205 BUBBLE_ACTION_CLICK_ACTION_BUTTON
= 0,
206 BUBBLE_ACTION_CLICK_DISMISS_BUTTON
,
207 BUBBLE_ACTION_CLICK_LINK
,
210 FakeExtensionMessageBubble() {}
212 void set_action_on_show(ExtensionBubbleAction action
) {
216 void Show() override
{
217 if (action_
== BUBBLE_ACTION_CLICK_ACTION_BUTTON
)
218 action_callback_
.Run();
219 else if (action_
== BUBBLE_ACTION_CLICK_DISMISS_BUTTON
)
220 dismiss_callback_
.Run();
221 else if (action_
== BUBBLE_ACTION_CLICK_LINK
)
222 link_callback_
.Run();
225 void OnActionButtonClicked(const base::Closure
& callback
) override
{
226 action_callback_
= callback
;
229 void OnDismissButtonClicked(const base::Closure
& callback
) override
{
230 dismiss_callback_
= callback
;
233 void OnLinkClicked(const base::Closure
& callback
) override
{
234 link_callback_
= callback
;
238 ExtensionBubbleAction action_
;
240 base::Closure action_callback_
;
241 base::Closure dismiss_callback_
;
242 base::Closure link_callback_
;
245 class ExtensionMessageBubbleTest
: public testing::Test
{
247 ExtensionMessageBubbleTest() {}
249 testing::AssertionResult
LoadGenericExtension(const std::string
& index
,
250 const std::string
& id
,
251 Manifest::Location location
) {
252 ExtensionBuilder builder
;
253 builder
.SetManifest(DictionaryBuilder()
254 .Set("name", std::string("Extension " + index
))
255 .Set("version", "1.0")
256 .Set("manifest_version", 2));
257 builder
.SetLocation(location
);
259 service_
->AddExtension(builder
.Build().get());
261 if (ExtensionRegistry::Get(profile())->enabled_extensions().GetByID(id
))
262 return testing::AssertionSuccess();
263 return testing::AssertionFailure() << "Could not install extension: " << id
;
266 testing::AssertionResult
LoadExtensionWithAction(
267 const std::string
& index
,
268 const std::string
& id
,
269 Manifest::Location location
) {
270 ExtensionBuilder builder
;
271 builder
.SetManifest(DictionaryBuilder()
272 .Set("name", std::string("Extension " + index
))
273 .Set("version", "1.0")
274 .Set("manifest_version", 2)
275 .Set("browser_action",
276 DictionaryBuilder().Set(
277 "default_title", "Default title")));
278 builder
.SetLocation(location
);
280 service_
->AddExtension(builder
.Build().get());
282 if (ExtensionRegistry::Get(profile())->enabled_extensions().GetByID(id
))
283 return testing::AssertionSuccess();
284 return testing::AssertionFailure() << "Could not install extension: " << id
;
287 testing::AssertionResult
LoadExtensionOverridingHome(
288 const std::string
& index
,
289 const std::string
& id
,
290 Manifest::Location location
) {
291 ExtensionBuilder builder
;
292 builder
.SetManifest(DictionaryBuilder()
293 .Set("name", std::string("Extension " + index
))
294 .Set("version", "1.0")
295 .Set("manifest_version", 2)
296 .Set("chrome_settings_overrides",
297 DictionaryBuilder().Set(
298 "homepage", "http://www.google.com")));
299 builder
.SetLocation(location
);
301 service_
->AddExtension(builder
.Build().get());
303 if (ExtensionRegistry::Get(profile())->enabled_extensions().GetByID(id
))
304 return testing::AssertionSuccess();
305 return testing::AssertionFailure() << "Could not install extension: " << id
;
308 testing::AssertionResult
LoadExtensionOverridingStart(
309 const std::string
& index
,
310 const std::string
& id
,
311 Manifest::Location location
) {
312 ExtensionBuilder builder
;
313 builder
.SetManifest(DictionaryBuilder()
314 .Set("name", std::string("Extension " + index
))
315 .Set("version", "1.0")
316 .Set("manifest_version", 2)
317 .Set("chrome_settings_overrides",
318 DictionaryBuilder().Set(
320 ListBuilder().Append(
321 "http://www.google.com"))));
322 builder
.SetLocation(location
);
324 service_
->AddExtension(builder
.Build().get());
326 if (ExtensionRegistry::Get(profile())->enabled_extensions().GetByID(id
))
327 return testing::AssertionSuccess();
328 return testing::AssertionFailure() << "Could not install extension: " << id
;
331 testing::AssertionResult
LoadExtensionOverridingNtp(
332 const std::string
& index
,
333 const std::string
& id
,
334 Manifest::Location location
) {
335 ExtensionBuilder builder
;
336 builder
.SetManifest(DictionaryBuilder()
337 .Set("name", std::string("Extension " + index
))
338 .Set("version", "1.0")
339 .Set("manifest_version", 2)
340 .Set("chrome_url_overrides",
341 DictionaryBuilder().Set(
342 "newtab", "Default.html")));
344 builder
.SetLocation(location
);
346 service_
->AddExtension(builder
.Build().get());
348 if (ExtensionRegistry::Get(profile())->enabled_extensions().GetByID(id
))
349 return testing::AssertionSuccess();
350 return testing::AssertionFailure() << "Could not install extension: " << id
;
353 testing::AssertionResult
LoadExtensionOverridingProxy(
354 const std::string
& index
,
355 const std::string
& id
,
356 Manifest::Location location
) {
357 ExtensionBuilder builder
;
358 builder
.SetManifest(DictionaryBuilder()
359 .Set("name", std::string("Extension " + index
))
360 .Set("version", "1.0")
361 .Set("manifest_version", 2)
363 ListBuilder().Append("proxy")));
365 builder
.SetLocation(location
);
367 service_
->AddExtension(builder
.Build().get());
369 // The proxy check relies on ExtensionPrefValueMap being up to date as to
370 // specifying which extension is controlling the proxy, but unfortunately
371 // that Map is not updated automatically for unit tests, so we simulate the
372 // update here to avoid test failures.
373 ExtensionPrefValueMap
* extension_prefs_value_map
=
374 ExtensionPrefValueMapFactory::GetForBrowserContext(profile());
375 extension_prefs_value_map
->RegisterExtension(
379 false); // is_incognito_enabled.
380 extension_prefs_value_map
->SetExtensionPref(id
,
382 kExtensionPrefsScopeRegular
,
383 new base::StringValue(id
));
385 if (ExtensionRegistry::Get(profile())->enabled_extensions().GetByID(id
))
386 return testing::AssertionSuccess();
387 return testing::AssertionFailure() << "Could not install extension: " << id
;
391 // The two lines of magical incantation required to get the extension
392 // service to work inside a unit test and access the extension prefs.
393 thread_bundle_
.reset(new content::TestBrowserThreadBundle
);
394 profile_
.reset(new TestingProfile
);
395 static_cast<TestExtensionSystem
*>(ExtensionSystem::Get(profile()))
396 ->CreateExtensionService(base::CommandLine::ForCurrentProcess(),
397 base::FilePath(), false);
398 service_
= ExtensionSystem::Get(profile())->extension_service();
402 ~ExtensionMessageBubbleTest() override
{
403 // Make sure the profile is destroyed before the thread bundle.
404 profile_
.reset(NULL
);
407 void SetUp() override
{
408 command_line_
.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM
));
412 Profile
* profile() { return profile_
.get(); }
414 scoped_refptr
<Extension
> CreateExtension(
415 Manifest::Location location
,
416 const std::string
& data
,
417 const std::string
& id
) {
418 scoped_ptr
<base::DictionaryValue
> parsed_manifest(
419 api_test_utils::ParseDictionary(data
));
420 return api_test_utils::CreateExtension(location
, parsed_manifest
.get(), id
);
423 ExtensionService
* service_
;
426 scoped_ptr
<base::CommandLine
> command_line_
;
427 scoped_ptr
<content::TestBrowserThreadBundle
> thread_bundle_
;
428 scoped_ptr
<TestingProfile
> profile_
;
430 DISALLOW_COPY_AND_ASSIGN(ExtensionMessageBubbleTest
);
433 // The feature this is meant to test is only implemented on Windows.
435 #define MAYBE_WipeoutControllerTest WipeoutControllerTest
437 #define MAYBE_WipeoutControllerTest DISABLED_WipeoutControllerTest
440 TEST_F(ExtensionMessageBubbleTest
, MAYBE_WipeoutControllerTest
) {
442 // Add three extensions, and control two of them in this test (extension 1
444 ASSERT_TRUE(LoadExtensionWithAction("1", kId1
, Manifest::COMMAND_LINE
));
445 ASSERT_TRUE(LoadGenericExtension("2", kId2
, Manifest::UNPACKED
));
446 ASSERT_TRUE(LoadGenericExtension("3", kId3
, Manifest::EXTERNAL_POLICY
));
448 scoped_ptr
<TestSuspiciousExtensionBubbleController
> controller(
449 new TestSuspiciousExtensionBubbleController(profile()));
450 FakeExtensionMessageBubble bubble
;
451 bubble
.set_action_on_show(
452 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_DISMISS_BUTTON
);
454 // Validate that we don't have a suppress value for the extensions.
455 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId1
));
456 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId1
));
458 EXPECT_FALSE(controller
->ShouldShow());
459 std::vector
<base::string16
> suspicious_extensions
=
460 controller
->GetExtensionList();
461 EXPECT_EQ(0U, suspicious_extensions
.size());
462 EXPECT_EQ(0U, controller
->link_click_count());
463 EXPECT_EQ(0U, controller
->dismiss_click_count());
465 // Now disable an extension, specifying the wipeout flag.
466 service_
->DisableExtension(kId1
, Extension::DISABLE_NOT_VERIFIED
);
468 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId1
));
469 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId2
));
470 controller
.reset(new TestSuspiciousExtensionBubbleController(
472 SuspiciousExtensionBubbleController::ClearProfileListForTesting();
473 EXPECT_TRUE(controller
->ShouldShow());
474 suspicious_extensions
= controller
->GetExtensionList();
475 ASSERT_EQ(1U, suspicious_extensions
.size());
476 EXPECT_TRUE(base::ASCIIToUTF16("Extension 1") == suspicious_extensions
[0]);
477 controller
->Show(&bubble
); // Simulate showing the bubble.
478 EXPECT_EQ(0U, controller
->link_click_count());
479 EXPECT_EQ(1U, controller
->dismiss_click_count());
480 // Now the acknowledge flag should be set only for the first extension.
481 EXPECT_TRUE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId1
));
482 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId2
));
484 controller
->delegate()->SetBubbleInfoBeenAcknowledged(kId1
, false);
485 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId1
));
487 // Now disable the other extension and exercise the link click code path.
488 service_
->DisableExtension(kId2
, Extension::DISABLE_NOT_VERIFIED
);
490 bubble
.set_action_on_show(
491 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_LINK
);
492 controller
.reset(new TestSuspiciousExtensionBubbleController(
494 SuspiciousExtensionBubbleController::ClearProfileListForTesting();
495 EXPECT_TRUE(controller
->ShouldShow());
496 suspicious_extensions
= controller
->GetExtensionList();
497 ASSERT_EQ(2U, suspicious_extensions
.size());
498 EXPECT_TRUE(base::ASCIIToUTF16("Extension 1") == suspicious_extensions
[1]);
499 EXPECT_TRUE(base::ASCIIToUTF16("Extension 2") == suspicious_extensions
[0]);
500 controller
->Show(&bubble
); // Simulate showing the bubble.
501 EXPECT_EQ(1U, controller
->link_click_count());
502 EXPECT_EQ(0U, controller
->dismiss_click_count());
503 EXPECT_TRUE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId1
));
506 // The feature this is meant to test is only implemented on Windows.
508 #define MAYBE_DevModeControllerTest DevModeControllerTest
510 #define MAYBE_DevModeControllerTest DISABLED_DevModeControllerTest
513 TEST_F(ExtensionMessageBubbleTest
, MAYBE_DevModeControllerTest
) {
514 FeatureSwitch::ScopedOverride
force_dev_mode_highlighting(
515 FeatureSwitch::force_dev_mode_highlighting(), true);
517 // Add three extensions, and control two of them in this test (extension 1
518 // and 2). Extension 1 is a regular extension, Extension 2 is UNPACKED so it
519 // counts as a DevMode extension.
520 ASSERT_TRUE(LoadExtensionWithAction("1", kId1
, Manifest::COMMAND_LINE
));
521 ASSERT_TRUE(LoadGenericExtension("2", kId2
, Manifest::UNPACKED
));
522 ASSERT_TRUE(LoadGenericExtension("3", kId3
, Manifest::EXTERNAL_POLICY
));
524 scoped_ptr
<TestDevModeBubbleController
> controller(
525 new TestDevModeBubbleController(profile()));
527 // The list will contain one enabled unpacked extension.
528 EXPECT_TRUE(controller
->ShouldShow());
529 std::vector
<base::string16
> dev_mode_extensions
=
530 controller
->GetExtensionList();
531 ASSERT_EQ(2U, dev_mode_extensions
.size());
532 EXPECT_TRUE(base::ASCIIToUTF16("Extension 2") == dev_mode_extensions
[0]);
533 EXPECT_TRUE(base::ASCIIToUTF16("Extension 1") == dev_mode_extensions
[1]);
534 EXPECT_EQ(0U, controller
->link_click_count());
535 EXPECT_EQ(0U, controller
->dismiss_click_count());
536 EXPECT_EQ(0U, controller
->action_click_count());
538 // Simulate showing the bubble.
539 FakeExtensionMessageBubble bubble
;
540 bubble
.set_action_on_show(
541 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_DISMISS_BUTTON
);
542 controller
->Show(&bubble
);
543 EXPECT_EQ(0U, controller
->link_click_count());
544 EXPECT_EQ(0U, controller
->action_click_count());
545 EXPECT_EQ(1U, controller
->dismiss_click_count());
546 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile());
547 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId1
) != NULL
);
548 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId2
) != NULL
);
550 // Do it again, but now press different button (Disable).
551 bubble
.set_action_on_show(
552 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_ACTION_BUTTON
);
553 controller
.reset(new TestDevModeBubbleController(
555 DevModeBubbleController::ClearProfileListForTesting();
556 EXPECT_TRUE(controller
->ShouldShow());
557 dev_mode_extensions
= controller
->GetExtensionList();
558 EXPECT_EQ(2U, dev_mode_extensions
.size());
559 controller
->Show(&bubble
); // Simulate showing the bubble.
560 EXPECT_EQ(0U, controller
->link_click_count());
561 EXPECT_EQ(1U, controller
->action_click_count());
562 EXPECT_EQ(0U, controller
->dismiss_click_count());
563 EXPECT_TRUE(registry
->disabled_extensions().GetByID(kId1
) != NULL
);
564 EXPECT_TRUE(registry
->disabled_extensions().GetByID(kId2
) != NULL
);
566 // Re-enable the extensions (disabled by the action button above).
567 service_
->EnableExtension(kId1
);
568 service_
->EnableExtension(kId2
);
570 // Show the dialog a third time, but now press the learn more link.
571 bubble
.set_action_on_show(
572 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_LINK
);
573 controller
.reset(new TestDevModeBubbleController(
575 DevModeBubbleController::ClearProfileListForTesting();
576 EXPECT_TRUE(controller
->ShouldShow());
577 dev_mode_extensions
= controller
->GetExtensionList();
578 EXPECT_EQ(2U, dev_mode_extensions
.size());
579 controller
->Show(&bubble
); // Simulate showing the bubble.
580 EXPECT_EQ(1U, controller
->link_click_count());
581 EXPECT_EQ(0U, controller
->action_click_count());
582 EXPECT_EQ(0U, controller
->dismiss_click_count());
583 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId1
) != NULL
);
584 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId2
) != NULL
);
586 // Now disable the unpacked extension.
587 service_
->DisableExtension(kId1
, Extension::DISABLE_USER_ACTION
);
588 service_
->DisableExtension(kId2
, Extension::DISABLE_USER_ACTION
);
590 controller
.reset(new TestDevModeBubbleController(
592 DevModeBubbleController::ClearProfileListForTesting();
593 EXPECT_FALSE(controller
->ShouldShow());
594 dev_mode_extensions
= controller
->GetExtensionList();
595 EXPECT_EQ(0U, dev_mode_extensions
.size());
598 // The feature this is meant to test is only implemented on Windows.
600 #define MAYBE_SettingsApiControllerTest SettingsApiControllerTest
602 #define MAYBE_SettingsApiControllerTest DISABLED_SettingsApiControllerTest
605 TEST_F(ExtensionMessageBubbleTest
, MAYBE_SettingsApiControllerTest
) {
608 for (int i
= 0; i
< 3; ++i
) {
609 switch (static_cast<SettingsApiOverrideType
>(i
)) {
610 case BUBBLE_TYPE_HOME_PAGE
:
611 // Load two extensions overriding home page and one overriding something
612 // unrelated (to check for interference). Extension 2 should still win
613 // on the home page setting.
614 ASSERT_TRUE(LoadExtensionOverridingHome("1", kId1
, Manifest::UNPACKED
));
615 ASSERT_TRUE(LoadExtensionOverridingHome("2", kId2
, Manifest::UNPACKED
));
617 LoadExtensionOverridingStart("3", kId3
, Manifest::UNPACKED
));
619 case BUBBLE_TYPE_SEARCH_ENGINE
:
620 // We deliberately skip testing the search engine since it relies on
621 // TemplateURLServiceFactory that isn't available while unit testing.
622 // This test is only simulating the bubble interaction with the user and
623 // that is more or less the same for the search engine as it is for the
626 case BUBBLE_TYPE_STARTUP_PAGES
:
627 // Load two extensions overriding start page and one overriding
628 // something unrelated (to check for interference). Extension 2 should
629 // still win on the startup page setting.
631 LoadExtensionOverridingStart("1", kId1
, Manifest::UNPACKED
));
633 LoadExtensionOverridingStart("2", kId2
, Manifest::UNPACKED
));
634 ASSERT_TRUE(LoadExtensionOverridingHome("3", kId3
, Manifest::UNPACKED
));
641 scoped_ptr
<TestSettingsApiBubbleController
> controller(
642 new TestSettingsApiBubbleController(
643 profile(), static_cast<SettingsApiOverrideType
>(i
)));
645 // The list will contain one enabled unpacked extension (ext 2).
646 EXPECT_TRUE(controller
->ShouldShow(kId2
));
647 std::vector
<base::string16
> override_extensions
=
648 controller
->GetExtensionList();
649 ASSERT_EQ(1U, override_extensions
.size());
650 EXPECT_TRUE(base::ASCIIToUTF16("Extension 2") ==
651 override_extensions
[0].c_str());
652 EXPECT_EQ(0U, controller
->link_click_count());
653 EXPECT_EQ(0U, controller
->dismiss_click_count());
654 EXPECT_EQ(0U, controller
->action_click_count());
656 // Simulate showing the bubble and dismissing it.
657 FakeExtensionMessageBubble bubble
;
658 bubble
.set_action_on_show(
659 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_DISMISS_BUTTON
);
660 controller
->Show(&bubble
);
661 EXPECT_EQ(0U, controller
->link_click_count());
662 EXPECT_EQ(0U, controller
->action_click_count());
663 EXPECT_EQ(1U, controller
->dismiss_click_count());
664 // No extension should have become disabled.
665 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile());
666 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId1
) != NULL
);
667 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId2
) != NULL
);
668 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId3
) != NULL
);
669 // Only extension 2 should have been acknowledged.
670 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId1
));
671 EXPECT_TRUE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId2
));
672 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId3
));
673 // Clean up after ourselves.
674 controller
->delegate()->SetBubbleInfoBeenAcknowledged(kId2
, false);
676 // Simulate clicking the learn more link to dismiss it.
677 bubble
.set_action_on_show(
678 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_LINK
);
679 controller
.reset(new TestSettingsApiBubbleController(
680 profile(), static_cast<SettingsApiOverrideType
>(i
)));
681 controller
->Show(&bubble
);
682 EXPECT_EQ(1U, controller
->link_click_count());
683 EXPECT_EQ(0U, controller
->action_click_count());
684 EXPECT_EQ(0U, controller
->dismiss_click_count());
685 // No extension should have become disabled.
686 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId1
) != NULL
);
687 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId2
) != NULL
);
688 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId3
) != NULL
);
689 // Only extension 2 should have been acknowledged.
690 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId1
));
691 EXPECT_TRUE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId2
));
692 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId3
));
693 // Clean up after ourselves.
694 controller
->delegate()->SetBubbleInfoBeenAcknowledged(kId2
, false);
696 // Do it again, but now opt to disable the extension.
697 bubble
.set_action_on_show(
698 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_ACTION_BUTTON
);
699 controller
.reset(new TestSettingsApiBubbleController(
700 profile(), static_cast<SettingsApiOverrideType
>(i
)));
701 EXPECT_TRUE(controller
->ShouldShow(kId2
));
702 override_extensions
= controller
->GetExtensionList();
703 EXPECT_EQ(1U, override_extensions
.size());
704 controller
->Show(&bubble
); // Simulate showing the bubble.
705 EXPECT_EQ(0U, controller
->link_click_count());
706 EXPECT_EQ(1U, controller
->action_click_count());
707 EXPECT_EQ(0U, controller
->dismiss_click_count());
708 // Only extension 2 should have become disabled.
709 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId1
) != NULL
);
710 EXPECT_TRUE(registry
->disabled_extensions().GetByID(kId2
) != NULL
);
711 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId3
) != NULL
);
712 // No extension should have been acknowledged (it got disabled).
713 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId1
));
714 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId2
));
715 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId3
));
717 // Clean up after ourselves.
718 service_
->UninstallExtension(kId1
,
719 extensions::UNINSTALL_REASON_FOR_TESTING
,
720 base::Bind(&base::DoNothing
),
722 service_
->UninstallExtension(kId2
,
723 extensions::UNINSTALL_REASON_FOR_TESTING
,
724 base::Bind(&base::DoNothing
),
726 service_
->UninstallExtension(kId3
,
727 extensions::UNINSTALL_REASON_FOR_TESTING
,
728 base::Bind(&base::DoNothing
),
733 // The feature this is meant to test is only implemented on Windows.
735 #define MAYBE_NtpOverriddenControllerTest NtpOverriddenControllerTest
737 #define MAYBE_NtpOverriddenControllerTest DISABLED_NtpOverriddenControllerTest
740 TEST_F(ExtensionMessageBubbleTest
, MAYBE_NtpOverriddenControllerTest
) {
742 // Load two extensions overriding new tab page and one overriding something
743 // unrelated (to check for interference). Extension 2 should still win
744 // on the new tab page setting.
745 ASSERT_TRUE(LoadExtensionOverridingNtp("1", kId1
, Manifest::UNPACKED
));
746 ASSERT_TRUE(LoadExtensionOverridingNtp("2", kId2
, Manifest::UNPACKED
));
747 ASSERT_TRUE(LoadExtensionOverridingStart("3", kId3
, Manifest::UNPACKED
));
749 scoped_ptr
<TestNtpOverriddenBubbleController
> controller(
750 new TestNtpOverriddenBubbleController(profile()));
752 // The list will contain one enabled unpacked extension (ext 2).
753 EXPECT_TRUE(controller
->ShouldShow(kId2
));
754 std::vector
<base::string16
> override_extensions
=
755 controller
->GetExtensionList();
756 ASSERT_EQ(1U, override_extensions
.size());
757 EXPECT_TRUE(base::ASCIIToUTF16("Extension 2") ==
758 override_extensions
[0].c_str());
759 EXPECT_EQ(0U, controller
->link_click_count());
760 EXPECT_EQ(0U, controller
->dismiss_click_count());
761 EXPECT_EQ(0U, controller
->action_click_count());
763 // Simulate showing the bubble and dismissing it.
764 FakeExtensionMessageBubble bubble
;
765 bubble
.set_action_on_show(
766 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_DISMISS_BUTTON
);
767 EXPECT_TRUE(controller
->ShouldShow(kId2
));
768 controller
->Show(&bubble
);
769 EXPECT_EQ(0U, controller
->link_click_count());
770 EXPECT_EQ(0U, controller
->action_click_count());
771 EXPECT_EQ(1U, controller
->dismiss_click_count());
772 // No extension should have become disabled.
773 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile());
774 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId1
) != NULL
);
775 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId2
) != NULL
);
776 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId3
) != NULL
);
777 // Only extension 2 should have been acknowledged.
778 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId1
));
779 EXPECT_TRUE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId2
));
780 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId3
));
781 // Clean up after ourselves.
782 controller
->delegate()->SetBubbleInfoBeenAcknowledged(kId2
, false);
784 // Simulate clicking the learn more link to dismiss it.
785 bubble
.set_action_on_show(
786 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_LINK
);
787 controller
.reset(new TestNtpOverriddenBubbleController(profile()));
788 EXPECT_TRUE(controller
->ShouldShow(kId2
));
789 controller
->Show(&bubble
);
790 EXPECT_EQ(1U, controller
->link_click_count());
791 EXPECT_EQ(0U, controller
->action_click_count());
792 EXPECT_EQ(0U, controller
->dismiss_click_count());
793 // No extension should have become disabled.
794 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId1
) != NULL
);
795 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId2
) != NULL
);
796 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId3
) != NULL
);
797 // Only extension 2 should have been acknowledged.
798 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId1
));
799 EXPECT_TRUE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId2
));
800 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId3
));
801 // Clean up after ourselves.
802 controller
->delegate()->SetBubbleInfoBeenAcknowledged(kId2
, false);
804 // Do it again, but now opt to disable the extension.
805 bubble
.set_action_on_show(
806 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_ACTION_BUTTON
);
807 controller
.reset(new TestNtpOverriddenBubbleController(profile()));
808 EXPECT_TRUE(controller
->ShouldShow(kId2
));
809 override_extensions
= controller
->GetExtensionList();
810 EXPECT_EQ(1U, override_extensions
.size());
811 controller
->Show(&bubble
); // Simulate showing the bubble.
812 EXPECT_EQ(0U, controller
->link_click_count());
813 EXPECT_EQ(1U, controller
->action_click_count());
814 EXPECT_EQ(0U, controller
->dismiss_click_count());
815 // Only extension 2 should have become disabled.
816 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId1
) != NULL
);
817 EXPECT_TRUE(registry
->disabled_extensions().GetByID(kId2
) != NULL
);
818 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId3
) != NULL
);
819 // No extension should have been acknowledged (it got disabled).
820 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId1
));
821 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId2
));
822 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId3
));
824 // Clean up after ourselves.
825 service_
->UninstallExtension(kId1
,
826 extensions::UNINSTALL_REASON_FOR_TESTING
,
827 base::Bind(&base::DoNothing
),
829 service_
->UninstallExtension(kId2
,
830 extensions::UNINSTALL_REASON_FOR_TESTING
,
831 base::Bind(&base::DoNothing
),
833 service_
->UninstallExtension(kId3
,
834 extensions::UNINSTALL_REASON_FOR_TESTING
,
835 base::Bind(&base::DoNothing
),
839 void SetInstallTime(const std::string
& extension_id
,
840 const base::Time
& time
,
841 ExtensionPrefs
* prefs
) {
842 std::string time_str
= base::Int64ToString(time
.ToInternalValue());
843 prefs
->UpdateExtensionPref(extension_id
,
845 new base::StringValue(time_str
));
848 // The feature this is meant to test is only implemented on Windows.
850 // http://crbug.com/397426
851 #define MAYBE_ProxyOverriddenControllerTest DISABLED_ProxyOverriddenControllerTest
853 #define MAYBE_ProxyOverriddenControllerTest DISABLED_ProxyOverriddenControllerTest
856 TEST_F(ExtensionMessageBubbleTest
, MAYBE_ProxyOverriddenControllerTest
) {
858 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile());
859 // Load two extensions overriding proxy and one overriding something
860 // unrelated (to check for interference). Extension 2 should still win
861 // on the proxy setting.
862 ASSERT_TRUE(LoadExtensionOverridingProxy("1", kId1
, Manifest::UNPACKED
));
863 ASSERT_TRUE(LoadExtensionOverridingProxy("2", kId2
, Manifest::UNPACKED
));
864 ASSERT_TRUE(LoadExtensionOverridingStart("3", kId3
, Manifest::UNPACKED
));
866 // The bubble will not show if the extension was installed in the last 7 days
867 // so we artificially set the install time to simulate an old install during
869 base::Time old_enough
= base::Time::Now() - base::TimeDelta::FromDays(8);
870 SetInstallTime(kId1
, old_enough
, prefs
);
871 SetInstallTime(kId2
, base::Time::Now(), prefs
);
872 SetInstallTime(kId3
, old_enough
, prefs
);
874 scoped_ptr
<TestProxyOverriddenBubbleController
> controller(
875 new TestProxyOverriddenBubbleController(profile()));
877 // The second extension is too new to warn about.
878 EXPECT_FALSE(controller
->ShouldShow(kId1
));
879 EXPECT_FALSE(controller
->ShouldShow(kId2
));
880 // Lets make it old enough.
881 SetInstallTime(kId2
, old_enough
, prefs
);
883 // The list will contain one enabled unpacked extension (ext 2).
884 EXPECT_TRUE(controller
->ShouldShow(kId2
));
885 EXPECT_FALSE(controller
->ShouldShow(kId3
));
886 std::vector
<base::string16
> override_extensions
=
887 controller
->GetExtensionList();
888 ASSERT_EQ(1U, override_extensions
.size());
889 EXPECT_EQ(base::ASCIIToUTF16("Extension 2"), override_extensions
[0]);
890 EXPECT_EQ(0U, controller
->link_click_count());
891 EXPECT_EQ(0U, controller
->dismiss_click_count());
892 EXPECT_EQ(0U, controller
->action_click_count());
894 // Simulate showing the bubble and dismissing it.
895 FakeExtensionMessageBubble bubble
;
896 bubble
.set_action_on_show(
897 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_DISMISS_BUTTON
);
898 controller
->Show(&bubble
);
899 EXPECT_EQ(0U, controller
->link_click_count());
900 EXPECT_EQ(0U, controller
->action_click_count());
901 EXPECT_EQ(1U, controller
->dismiss_click_count());
902 // No extension should have become disabled.
903 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile());
904 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId1
) != NULL
);
905 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId2
) != NULL
);
906 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId3
) != NULL
);
907 // Only extension 2 should have been acknowledged.
908 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId1
));
909 EXPECT_TRUE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId2
));
910 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId3
));
911 // Clean up after ourselves.
912 controller
->delegate()->SetBubbleInfoBeenAcknowledged(kId2
, false);
914 // Simulate clicking the learn more link to dismiss it.
915 bubble
.set_action_on_show(
916 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_LINK
);
917 controller
.reset(new TestProxyOverriddenBubbleController(profile()));
918 EXPECT_TRUE(controller
->ShouldShow(kId2
));
919 controller
->Show(&bubble
);
920 EXPECT_EQ(1U, controller
->link_click_count());
921 EXPECT_EQ(0U, controller
->action_click_count());
922 EXPECT_EQ(0U, controller
->dismiss_click_count());
923 // No extension should have become disabled.
924 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId1
) != NULL
);
925 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId2
) != NULL
);
926 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId3
) != NULL
);
927 // Only extension 2 should have been acknowledged.
928 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId1
));
929 EXPECT_TRUE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId2
));
930 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId3
));
931 // Clean up after ourselves.
932 controller
->delegate()->SetBubbleInfoBeenAcknowledged(kId2
, false);
934 // Do it again, but now opt to disable the extension.
935 bubble
.set_action_on_show(
936 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_ACTION_BUTTON
);
937 controller
.reset(new TestProxyOverriddenBubbleController(profile()));
938 EXPECT_TRUE(controller
->ShouldShow(kId2
));
939 override_extensions
= controller
->GetExtensionList();
940 EXPECT_EQ(1U, override_extensions
.size());
941 controller
->Show(&bubble
); // Simulate showing the bubble.
942 EXPECT_EQ(0U, controller
->link_click_count());
943 EXPECT_EQ(1U, controller
->action_click_count());
944 EXPECT_EQ(0U, controller
->dismiss_click_count());
945 // Only extension 2 should have become disabled.
946 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId1
) != NULL
);
947 EXPECT_TRUE(registry
->disabled_extensions().GetByID(kId2
) != NULL
);
948 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kId3
) != NULL
);
950 // No extension should have been acknowledged (it got disabled).
951 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId1
));
952 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId2
));
953 EXPECT_FALSE(controller
->delegate()->HasBubbleInfoBeenAcknowledged(kId3
));
955 // Clean up after ourselves.
956 service_
->UninstallExtension(kId1
,
957 extensions::UNINSTALL_REASON_FOR_TESTING
,
958 base::Bind(&base::DoNothing
),
960 service_
->UninstallExtension(kId2
,
961 extensions::UNINSTALL_REASON_FOR_TESTING
,
962 base::Bind(&base::DoNothing
),
964 service_
->UninstallExtension(kId3
,
965 extensions::UNINSTALL_REASON_FOR_TESTING
,
966 base::Bind(&base::DoNothing
),
970 } // namespace extensions