Remove keyboard_ui.css and manifest_keyboard.json
[chromium-blink-merge.git] / chrome / browser / extensions / extension_message_bubble_controller_unittest.cc
blob1a500921b86ce38ffdfa554970a087111bf4e3a5
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"
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
42 namespace {
44 const char kId1[] = "iccfkkhkfiphcjdakkmcjmkfboccmndk";
45 const char kId2[] = "ajjhifimiemdpmophmkkkcijegphclbl";
46 const char kId3[] = "ioibbbfddncmmabjmpokikkeiofalaek";
48 } // namespace
50 namespace extensions {
52 class TestDelegate {
53 public:
54 TestDelegate()
55 : action_button_callback_count_(0),
56 dismiss_button_callback_count_(0),
57 link_click_callback_count_(0) {
60 // Returns how often the dismiss button has been called.
61 size_t action_click_count() {
62 return action_button_callback_count_;
65 // Returns how often the dismiss button has been called.
66 size_t dismiss_click_count() {
67 return dismiss_button_callback_count_;
70 // Returns how often the link has been clicked.
71 size_t link_click_count() {
72 return link_click_callback_count_;
75 protected:
76 size_t action_button_callback_count_;
77 size_t dismiss_button_callback_count_;
78 size_t link_click_callback_count_;
81 // A test class for the SuspiciousExtensionBubbleController.
82 class TestSuspiciousExtensionBubbleController
83 : public SuspiciousExtensionBubbleController,
84 public TestDelegate {
85 public:
86 explicit TestSuspiciousExtensionBubbleController(Profile* profile)
87 : SuspiciousExtensionBubbleController(profile) {
90 void OnBubbleAction() override {
91 ++action_button_callback_count_;
92 SuspiciousExtensionBubbleController::OnBubbleAction();
95 void OnBubbleDismiss() override {
96 ++dismiss_button_callback_count_;
97 SuspiciousExtensionBubbleController::OnBubbleDismiss();
100 void OnLinkClicked() override {
101 ++link_click_callback_count_;
102 SuspiciousExtensionBubbleController::OnLinkClicked();
106 // A test class for the DevModeBubbleController.
107 class TestDevModeBubbleController
108 : public DevModeBubbleController,
109 public TestDelegate {
110 public:
111 explicit TestDevModeBubbleController(Profile* profile)
112 : DevModeBubbleController(profile) {
115 void OnBubbleAction() override {
116 ++action_button_callback_count_;
117 DevModeBubbleController::OnBubbleAction();
120 void OnBubbleDismiss() override {
121 ++dismiss_button_callback_count_;
122 DevModeBubbleController::OnBubbleDismiss();
125 void OnLinkClicked() override {
126 ++link_click_callback_count_;
127 DevModeBubbleController::OnLinkClicked();
131 // A test class for the SettingsApiBubbleController.
132 class TestSettingsApiBubbleController : public SettingsApiBubbleController,
133 public TestDelegate {
134 public:
135 TestSettingsApiBubbleController(Profile* profile,
136 SettingsApiOverrideType type)
137 : SettingsApiBubbleController(profile, type) {}
139 void OnBubbleAction() override {
140 ++action_button_callback_count_;
141 SettingsApiBubbleController::OnBubbleAction();
144 void OnBubbleDismiss() override {
145 ++dismiss_button_callback_count_;
146 SettingsApiBubbleController::OnBubbleDismiss();
149 void OnLinkClicked() override {
150 ++link_click_callback_count_;
151 SettingsApiBubbleController::OnLinkClicked();
155 // A test class for the NtpOverriddenBubbleController.
156 class TestNtpOverriddenBubbleController
157 : public NtpOverriddenBubbleController,
158 public TestDelegate {
159 public:
160 explicit TestNtpOverriddenBubbleController(Profile* profile)
161 : NtpOverriddenBubbleController(profile) {
164 void OnBubbleAction() override {
165 ++action_button_callback_count_;
166 NtpOverriddenBubbleController::OnBubbleAction();
169 void OnBubbleDismiss() override {
170 ++dismiss_button_callback_count_;
171 NtpOverriddenBubbleController::OnBubbleDismiss();
174 void OnLinkClicked() override {
175 ++link_click_callback_count_;
176 NtpOverriddenBubbleController::OnLinkClicked();
180 // A test class for the ProxyOverriddenBubbleController.
181 class TestProxyOverriddenBubbleController
182 : public ProxyOverriddenBubbleController,
183 public TestDelegate {
184 public:
185 explicit TestProxyOverriddenBubbleController(Profile* profile)
186 : ProxyOverriddenBubbleController(profile) {
189 void OnBubbleAction() override {
190 ++action_button_callback_count_;
191 ProxyOverriddenBubbleController::OnBubbleAction();
194 void OnBubbleDismiss() override {
195 ++dismiss_button_callback_count_;
196 ProxyOverriddenBubbleController::OnBubbleDismiss();
199 void OnLinkClicked() override {
200 ++link_click_callback_count_;
201 ProxyOverriddenBubbleController::OnLinkClicked();
205 // A fake bubble used for testing the controller. Takes an action that specifies
206 // what should happen when the bubble is "shown" (the bubble is actually not
207 // shown, the corresponding action is taken immediately).
208 class FakeExtensionMessageBubble : public ExtensionMessageBubble {
209 public:
210 enum ExtensionBubbleAction {
211 BUBBLE_ACTION_CLICK_ACTION_BUTTON = 0,
212 BUBBLE_ACTION_CLICK_DISMISS_BUTTON,
213 BUBBLE_ACTION_CLICK_LINK,
216 FakeExtensionMessageBubble() : controller_(nullptr) {}
218 void set_action_on_show(ExtensionBubbleAction action) {
219 action_ = action;
221 void set_controller(ExtensionMessageBubbleController* controller) {
222 controller_ = controller;
225 void Show() override {
226 if (action_ == BUBBLE_ACTION_CLICK_ACTION_BUTTON)
227 controller_->OnBubbleAction();
228 else if (action_ == BUBBLE_ACTION_CLICK_DISMISS_BUTTON)
229 controller_->OnBubbleDismiss();
230 else if (action_ == BUBBLE_ACTION_CLICK_LINK)
231 controller_->OnLinkClicked();
234 private:
235 ExtensionBubbleAction action_;
236 ExtensionMessageBubbleController* controller_;
238 DISALLOW_COPY_AND_ASSIGN(FakeExtensionMessageBubble);
241 class ExtensionMessageBubbleTest : public testing::Test {
242 public:
243 ExtensionMessageBubbleTest() {}
245 testing::AssertionResult LoadGenericExtension(const std::string& index,
246 const std::string& id,
247 Manifest::Location location) {
248 ExtensionBuilder builder;
249 builder.SetManifest(DictionaryBuilder()
250 .Set("name", std::string("Extension " + index))
251 .Set("version", "1.0")
252 .Set("manifest_version", 2));
253 builder.SetLocation(location);
254 builder.SetID(id);
255 service_->AddExtension(builder.Build().get());
257 if (ExtensionRegistry::Get(profile())->enabled_extensions().GetByID(id))
258 return testing::AssertionSuccess();
259 return testing::AssertionFailure() << "Could not install extension: " << id;
262 testing::AssertionResult LoadExtensionWithAction(
263 const std::string& index,
264 const std::string& id,
265 Manifest::Location location) {
266 ExtensionBuilder builder;
267 builder.SetManifest(DictionaryBuilder()
268 .Set("name", std::string("Extension " + index))
269 .Set("version", "1.0")
270 .Set("manifest_version", 2)
271 .Set("browser_action",
272 DictionaryBuilder().Set(
273 "default_title", "Default title")));
274 builder.SetLocation(location);
275 builder.SetID(id);
276 service_->AddExtension(builder.Build().get());
278 if (ExtensionRegistry::Get(profile())->enabled_extensions().GetByID(id))
279 return testing::AssertionSuccess();
280 return testing::AssertionFailure() << "Could not install extension: " << id;
283 testing::AssertionResult LoadExtensionOverridingHome(
284 const std::string& index,
285 const std::string& id,
286 Manifest::Location location) {
287 ExtensionBuilder builder;
288 builder.SetManifest(DictionaryBuilder()
289 .Set("name", std::string("Extension " + index))
290 .Set("version", "1.0")
291 .Set("manifest_version", 2)
292 .Set("chrome_settings_overrides",
293 DictionaryBuilder().Set(
294 "homepage", "http://www.google.com")));
295 builder.SetLocation(location);
296 builder.SetID(id);
297 service_->AddExtension(builder.Build().get());
299 if (ExtensionRegistry::Get(profile())->enabled_extensions().GetByID(id))
300 return testing::AssertionSuccess();
301 return testing::AssertionFailure() << "Could not install extension: " << id;
304 testing::AssertionResult LoadExtensionOverridingStart(
305 const std::string& index,
306 const std::string& id,
307 Manifest::Location location) {
308 ExtensionBuilder builder;
309 builder.SetManifest(DictionaryBuilder()
310 .Set("name", std::string("Extension " + index))
311 .Set("version", "1.0")
312 .Set("manifest_version", 2)
313 .Set("chrome_settings_overrides",
314 DictionaryBuilder().Set(
315 "startup_pages",
316 ListBuilder().Append(
317 "http://www.google.com"))));
318 builder.SetLocation(location);
319 builder.SetID(id);
320 service_->AddExtension(builder.Build().get());
322 if (ExtensionRegistry::Get(profile())->enabled_extensions().GetByID(id))
323 return testing::AssertionSuccess();
324 return testing::AssertionFailure() << "Could not install extension: " << id;
327 testing::AssertionResult LoadExtensionOverridingNtp(
328 const std::string& index,
329 const std::string& id,
330 Manifest::Location location) {
331 ExtensionBuilder builder;
332 builder.SetManifest(DictionaryBuilder()
333 .Set("name", std::string("Extension " + index))
334 .Set("version", "1.0")
335 .Set("manifest_version", 2)
336 .Set("chrome_url_overrides",
337 DictionaryBuilder().Set(
338 "newtab", "Default.html")));
340 builder.SetLocation(location);
341 builder.SetID(id);
342 service_->AddExtension(builder.Build().get());
344 if (ExtensionRegistry::Get(profile())->enabled_extensions().GetByID(id))
345 return testing::AssertionSuccess();
346 return testing::AssertionFailure() << "Could not install extension: " << id;
349 testing::AssertionResult LoadExtensionOverridingProxy(
350 const std::string& index,
351 const std::string& id,
352 Manifest::Location location) {
353 ExtensionBuilder builder;
354 builder.SetManifest(DictionaryBuilder()
355 .Set("name", std::string("Extension " + index))
356 .Set("version", "1.0")
357 .Set("manifest_version", 2)
358 .Set("permissions",
359 ListBuilder().Append("proxy")));
361 builder.SetLocation(location);
362 builder.SetID(id);
363 service_->AddExtension(builder.Build().get());
365 // The proxy check relies on ExtensionPrefValueMap being up to date as to
366 // specifying which extension is controlling the proxy, but unfortunately
367 // that Map is not updated automatically for unit tests, so we simulate the
368 // update here to avoid test failures.
369 ExtensionPrefValueMap* extension_prefs_value_map =
370 ExtensionPrefValueMapFactory::GetForBrowserContext(profile());
371 extension_prefs_value_map->RegisterExtension(
373 base::Time::Now(),
374 true, // is_enabled.
375 false); // is_incognito_enabled.
376 extension_prefs_value_map->SetExtensionPref(id,
377 prefs::kProxy,
378 kExtensionPrefsScopeRegular,
379 new base::StringValue(id));
381 if (ExtensionRegistry::Get(profile())->enabled_extensions().GetByID(id))
382 return testing::AssertionSuccess();
383 return testing::AssertionFailure() << "Could not install extension: " << id;
386 void Init() {
387 // The two lines of magical incantation required to get the extension
388 // service to work inside a unit test and access the extension prefs.
389 profile_.reset(new TestingProfile);
390 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()))
391 ->CreateExtensionService(base::CommandLine::ForCurrentProcess(),
392 base::FilePath(), false);
393 service_ = ExtensionSystem::Get(profile())->extension_service();
394 service_->Init();
397 ~ExtensionMessageBubbleTest() override {
398 // Make sure the profile is destroyed before the thread bundle.
399 profile_.reset(NULL);
402 void SetUp() override {
403 command_line_.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM));
406 protected:
407 Profile* profile() { return profile_.get(); }
409 scoped_refptr<Extension> CreateExtension(
410 Manifest::Location location,
411 const std::string& data,
412 const std::string& id) {
413 scoped_ptr<base::DictionaryValue> parsed_manifest(
414 api_test_utils::ParseDictionary(data));
415 return api_test_utils::CreateExtension(location, parsed_manifest.get(), id);
418 ExtensionService* service_;
420 private:
421 content::TestBrowserThreadBundle thread_bundle_;
422 scoped_ptr<base::CommandLine> command_line_;
423 scoped_ptr<TestingProfile> profile_;
425 #if defined OS_CHROMEOS
426 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
427 chromeos::ScopedTestCrosSettings test_cros_settings_;
428 chromeos::ScopedTestUserManager test_user_manager_;
429 #endif
431 DISALLOW_COPY_AND_ASSIGN(ExtensionMessageBubbleTest);
434 // The feature this is meant to test is only enacted on Windows, but it should
435 // pass on all platforms.
436 TEST_F(ExtensionMessageBubbleTest, WipeoutControllerTest) {
437 Init();
438 // Add three extensions, and control two of them in this test (extension 1
439 // and 2).
440 ASSERT_TRUE(LoadExtensionWithAction("1", kId1, Manifest::COMMAND_LINE));
441 ASSERT_TRUE(LoadGenericExtension("2", kId2, Manifest::UNPACKED));
442 ASSERT_TRUE(LoadGenericExtension("3", kId3, Manifest::EXTERNAL_POLICY));
444 scoped_ptr<TestSuspiciousExtensionBubbleController> controller(
445 new TestSuspiciousExtensionBubbleController(profile()));
446 FakeExtensionMessageBubble bubble;
447 bubble.set_action_on_show(
448 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_DISMISS_BUTTON);
450 // Validate that we don't have a suppress value for the extensions.
451 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId1));
452 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId1));
454 EXPECT_FALSE(controller->ShouldShow());
455 std::vector<base::string16> suspicious_extensions =
456 controller->GetExtensionList();
457 EXPECT_EQ(0U, suspicious_extensions.size());
458 EXPECT_EQ(0U, controller->link_click_count());
459 EXPECT_EQ(0U, controller->dismiss_click_count());
461 // Now disable an extension, specifying the wipeout flag.
462 service_->DisableExtension(kId1, Extension::DISABLE_NOT_VERIFIED);
464 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId1));
465 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId2));
466 controller.reset(new TestSuspiciousExtensionBubbleController(
467 profile()));
468 SuspiciousExtensionBubbleController::ClearProfileListForTesting();
469 EXPECT_TRUE(controller->ShouldShow());
470 suspicious_extensions = controller->GetExtensionList();
471 ASSERT_EQ(1U, suspicious_extensions.size());
472 EXPECT_TRUE(base::ASCIIToUTF16("Extension 1") == suspicious_extensions[0]);
473 bubble.set_controller(controller.get());
474 controller->Show(&bubble); // Simulate showing the bubble.
475 EXPECT_EQ(0U, controller->link_click_count());
476 EXPECT_EQ(1U, controller->dismiss_click_count());
477 // Now the acknowledge flag should be set only for the first extension.
478 EXPECT_TRUE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId1));
479 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId2));
480 // Clear the flag.
481 controller->delegate()->SetBubbleInfoBeenAcknowledged(kId1, false);
482 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId1));
484 // Now disable the other extension and exercise the link click code path.
485 service_->DisableExtension(kId2, Extension::DISABLE_NOT_VERIFIED);
487 bubble.set_action_on_show(
488 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_LINK);
489 controller.reset(new TestSuspiciousExtensionBubbleController(
490 profile()));
491 SuspiciousExtensionBubbleController::ClearProfileListForTesting();
492 EXPECT_TRUE(controller->ShouldShow());
493 suspicious_extensions = controller->GetExtensionList();
494 ASSERT_EQ(2U, suspicious_extensions.size());
495 EXPECT_TRUE(base::ASCIIToUTF16("Extension 1") == suspicious_extensions[1]);
496 EXPECT_TRUE(base::ASCIIToUTF16("Extension 2") == suspicious_extensions[0]);
497 bubble.set_controller(controller.get());
498 controller->Show(&bubble); // Simulate showing the bubble.
499 EXPECT_EQ(1U, controller->link_click_count());
500 EXPECT_EQ(0U, controller->dismiss_click_count());
501 EXPECT_TRUE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId1));
504 // The feature this is meant to test is only enacted on Windows, but it should
505 // pass on all platforms.
506 TEST_F(ExtensionMessageBubbleTest, DevModeControllerTest) {
507 FeatureSwitch::ScopedOverride force_dev_mode_highlighting(
508 FeatureSwitch::force_dev_mode_highlighting(), true);
509 Init();
510 // Add three extensions, and control two of them in this test (extension 1
511 // and 2). Extension 1 is a regular extension, Extension 2 is UNPACKED so it
512 // counts as a DevMode extension.
513 ASSERT_TRUE(LoadExtensionWithAction("1", kId1, Manifest::COMMAND_LINE));
514 ASSERT_TRUE(LoadGenericExtension("2", kId2, Manifest::UNPACKED));
515 ASSERT_TRUE(LoadGenericExtension("3", kId3, Manifest::EXTERNAL_POLICY));
517 scoped_ptr<TestDevModeBubbleController> controller(
518 new TestDevModeBubbleController(profile()));
520 // The list will contain one enabled unpacked extension.
521 EXPECT_TRUE(controller->ShouldShow());
522 std::vector<base::string16> dev_mode_extensions =
523 controller->GetExtensionList();
524 ASSERT_EQ(2U, dev_mode_extensions.size());
525 EXPECT_TRUE(base::ASCIIToUTF16("Extension 2") == dev_mode_extensions[0]);
526 EXPECT_TRUE(base::ASCIIToUTF16("Extension 1") == dev_mode_extensions[1]);
527 EXPECT_EQ(0U, controller->link_click_count());
528 EXPECT_EQ(0U, controller->dismiss_click_count());
529 EXPECT_EQ(0U, controller->action_click_count());
531 // Simulate showing the bubble.
532 FakeExtensionMessageBubble bubble;
533 bubble.set_action_on_show(
534 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_DISMISS_BUTTON);
535 bubble.set_controller(controller.get());
536 controller->Show(&bubble);
537 EXPECT_EQ(0U, controller->link_click_count());
538 EXPECT_EQ(0U, controller->action_click_count());
539 EXPECT_EQ(1U, controller->dismiss_click_count());
540 ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
541 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId1) != NULL);
542 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId2) != NULL);
544 // Do it again, but now press different button (Disable).
545 bubble.set_action_on_show(
546 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_ACTION_BUTTON);
547 controller.reset(new TestDevModeBubbleController(
548 profile()));
549 DevModeBubbleController::ClearProfileListForTesting();
550 EXPECT_TRUE(controller->ShouldShow());
551 dev_mode_extensions = controller->GetExtensionList();
552 EXPECT_EQ(2U, dev_mode_extensions.size());
553 bubble.set_controller(controller.get());
554 controller->Show(&bubble); // Simulate showing the bubble.
555 EXPECT_EQ(0U, controller->link_click_count());
556 EXPECT_EQ(1U, controller->action_click_count());
557 EXPECT_EQ(0U, controller->dismiss_click_count());
558 EXPECT_TRUE(registry->disabled_extensions().GetByID(kId1) != NULL);
559 EXPECT_TRUE(registry->disabled_extensions().GetByID(kId2) != NULL);
561 // Re-enable the extensions (disabled by the action button above).
562 service_->EnableExtension(kId1);
563 service_->EnableExtension(kId2);
565 // Show the dialog a third time, but now press the learn more link.
566 bubble.set_action_on_show(
567 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_LINK);
568 controller.reset(new TestDevModeBubbleController(
569 profile()));
570 DevModeBubbleController::ClearProfileListForTesting();
571 EXPECT_TRUE(controller->ShouldShow());
572 dev_mode_extensions = controller->GetExtensionList();
573 EXPECT_EQ(2U, dev_mode_extensions.size());
574 bubble.set_controller(controller.get());
575 controller->Show(&bubble); // Simulate showing the bubble.
576 EXPECT_EQ(1U, controller->link_click_count());
577 EXPECT_EQ(0U, controller->action_click_count());
578 EXPECT_EQ(0U, controller->dismiss_click_count());
579 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId1) != NULL);
580 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId2) != NULL);
582 // Now disable the unpacked extension.
583 service_->DisableExtension(kId1, Extension::DISABLE_USER_ACTION);
584 service_->DisableExtension(kId2, Extension::DISABLE_USER_ACTION);
586 controller.reset(new TestDevModeBubbleController(
587 profile()));
588 DevModeBubbleController::ClearProfileListForTesting();
589 EXPECT_FALSE(controller->ShouldShow());
590 dev_mode_extensions = controller->GetExtensionList();
591 EXPECT_EQ(0U, dev_mode_extensions.size());
594 // The feature this is meant to test is only implemented on Windows.
595 #if defined(OS_WIN)
596 #define MAYBE_SettingsApiControllerTest SettingsApiControllerTest
597 #else
598 #define MAYBE_SettingsApiControllerTest DISABLED_SettingsApiControllerTest
599 #endif
601 TEST_F(ExtensionMessageBubbleTest, MAYBE_SettingsApiControllerTest) {
602 Init();
604 for (int i = 0; i < 3; ++i) {
605 switch (static_cast<SettingsApiOverrideType>(i)) {
606 case BUBBLE_TYPE_HOME_PAGE:
607 // Load two extensions overriding home page and one overriding something
608 // unrelated (to check for interference). Extension 2 should still win
609 // on the home page setting.
610 ASSERT_TRUE(LoadExtensionOverridingHome("1", kId1, Manifest::UNPACKED));
611 ASSERT_TRUE(LoadExtensionOverridingHome("2", kId2, Manifest::UNPACKED));
612 ASSERT_TRUE(
613 LoadExtensionOverridingStart("3", kId3, Manifest::UNPACKED));
614 break;
615 case BUBBLE_TYPE_SEARCH_ENGINE:
616 // We deliberately skip testing the search engine since it relies on
617 // TemplateURLServiceFactory that isn't available while unit testing.
618 // This test is only simulating the bubble interaction with the user and
619 // that is more or less the same for the search engine as it is for the
620 // others.
621 continue;
622 case BUBBLE_TYPE_STARTUP_PAGES:
623 // Load two extensions overriding start page and one overriding
624 // something unrelated (to check for interference). Extension 2 should
625 // still win on the startup page setting.
626 ASSERT_TRUE(
627 LoadExtensionOverridingStart("1", kId1, Manifest::UNPACKED));
628 ASSERT_TRUE(
629 LoadExtensionOverridingStart("2", kId2, Manifest::UNPACKED));
630 ASSERT_TRUE(LoadExtensionOverridingHome("3", kId3, Manifest::UNPACKED));
631 break;
632 default:
633 NOTREACHED();
634 break;
637 scoped_ptr<TestSettingsApiBubbleController> controller(
638 new TestSettingsApiBubbleController(
639 profile(), static_cast<SettingsApiOverrideType>(i)));
641 // The list will contain one enabled unpacked extension (ext 2).
642 EXPECT_TRUE(controller->ShouldShow());
643 std::vector<base::string16> override_extensions =
644 controller->GetExtensionList();
645 ASSERT_EQ(1U, override_extensions.size());
646 EXPECT_TRUE(base::ASCIIToUTF16("Extension 2") ==
647 override_extensions[0].c_str());
648 EXPECT_EQ(0U, controller->link_click_count());
649 EXPECT_EQ(0U, controller->dismiss_click_count());
650 EXPECT_EQ(0U, controller->action_click_count());
652 // Simulate showing the bubble and dismissing it.
653 FakeExtensionMessageBubble bubble;
654 bubble.set_action_on_show(
655 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_DISMISS_BUTTON);
656 bubble.set_controller(controller.get());
657 controller->Show(&bubble);
658 EXPECT_EQ(0U, controller->link_click_count());
659 EXPECT_EQ(0U, controller->action_click_count());
660 EXPECT_EQ(1U, controller->dismiss_click_count());
661 // No extension should have become disabled.
662 ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
663 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId1) != NULL);
664 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId2) != NULL);
665 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId3) != NULL);
666 // Only extension 2 should have been acknowledged.
667 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId1));
668 EXPECT_TRUE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId2));
669 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId3));
670 // Clean up after ourselves.
671 controller->delegate()->SetBubbleInfoBeenAcknowledged(kId2, false);
673 // Simulate clicking the learn more link to dismiss it.
674 bubble.set_action_on_show(
675 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_LINK);
676 controller.reset(new TestSettingsApiBubbleController(
677 profile(), static_cast<SettingsApiOverrideType>(i)));
678 bubble.set_controller(controller.get());
679 controller->Show(&bubble);
680 EXPECT_EQ(1U, controller->link_click_count());
681 EXPECT_EQ(0U, controller->action_click_count());
682 EXPECT_EQ(0U, controller->dismiss_click_count());
683 // No extension should have become disabled.
684 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId1) != NULL);
685 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId2) != NULL);
686 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId3) != NULL);
687 // Only extension 2 should have been acknowledged.
688 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId1));
689 EXPECT_TRUE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId2));
690 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId3));
691 // Clean up after ourselves.
692 controller->delegate()->SetBubbleInfoBeenAcknowledged(kId2, false);
694 // Do it again, but now opt to disable the extension.
695 bubble.set_action_on_show(
696 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_ACTION_BUTTON);
697 controller.reset(new TestSettingsApiBubbleController(
698 profile(), static_cast<SettingsApiOverrideType>(i)));
699 EXPECT_TRUE(controller->ShouldShow());
700 override_extensions = controller->GetExtensionList();
701 EXPECT_EQ(1U, override_extensions.size());
702 bubble.set_controller(controller.get());
703 controller->Show(&bubble); // Simulate showing the bubble.
704 EXPECT_EQ(0U, controller->link_click_count());
705 EXPECT_EQ(1U, controller->action_click_count());
706 EXPECT_EQ(0U, controller->dismiss_click_count());
707 // Only extension 2 should have become disabled.
708 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId1) != NULL);
709 EXPECT_TRUE(registry->disabled_extensions().GetByID(kId2) != NULL);
710 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId3) != NULL);
711 // No extension should have been acknowledged (it got disabled).
712 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId1));
713 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId2));
714 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId3));
716 // Clean up after ourselves.
717 service_->UninstallExtension(kId1,
718 extensions::UNINSTALL_REASON_FOR_TESTING,
719 base::Bind(&base::DoNothing),
720 NULL);
721 service_->UninstallExtension(kId2,
722 extensions::UNINSTALL_REASON_FOR_TESTING,
723 base::Bind(&base::DoNothing),
724 NULL);
725 service_->UninstallExtension(kId3,
726 extensions::UNINSTALL_REASON_FOR_TESTING,
727 base::Bind(&base::DoNothing),
728 NULL);
732 // The feature this is meant to test is only enacted on Windows, but it should
733 // pass on all platforms.
734 TEST_F(ExtensionMessageBubbleTest, NtpOverriddenControllerTest) {
735 Init();
736 // Load two extensions overriding new tab page and one overriding something
737 // unrelated (to check for interference). Extension 2 should still win
738 // on the new tab page setting.
739 ASSERT_TRUE(LoadExtensionOverridingNtp("1", kId1, Manifest::UNPACKED));
740 ASSERT_TRUE(LoadExtensionOverridingNtp("2", kId2, Manifest::UNPACKED));
741 ASSERT_TRUE(LoadExtensionOverridingStart("3", kId3, Manifest::UNPACKED));
743 scoped_ptr<TestNtpOverriddenBubbleController> controller(
744 new TestNtpOverriddenBubbleController(profile()));
746 // The list will contain one enabled unpacked extension (ext 2).
747 EXPECT_TRUE(controller->ShouldShow(kId2));
748 std::vector<base::string16> override_extensions =
749 controller->GetExtensionList();
750 ASSERT_EQ(1U, override_extensions.size());
751 EXPECT_TRUE(base::ASCIIToUTF16("Extension 2") ==
752 override_extensions[0].c_str());
753 EXPECT_EQ(0U, controller->link_click_count());
754 EXPECT_EQ(0U, controller->dismiss_click_count());
755 EXPECT_EQ(0U, controller->action_click_count());
757 // Simulate showing the bubble and dismissing it.
758 FakeExtensionMessageBubble bubble;
759 bubble.set_action_on_show(
760 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_DISMISS_BUTTON);
761 EXPECT_TRUE(controller->ShouldShow(kId2));
762 bubble.set_controller(controller.get());
763 controller->Show(&bubble);
764 EXPECT_EQ(0U, controller->link_click_count());
765 EXPECT_EQ(0U, controller->action_click_count());
766 EXPECT_EQ(1U, controller->dismiss_click_count());
767 // No extension should have become disabled.
768 ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
769 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId1) != NULL);
770 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId2) != NULL);
771 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId3) != NULL);
772 // Only extension 2 should have been acknowledged.
773 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId1));
774 EXPECT_TRUE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId2));
775 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId3));
776 // Clean up after ourselves.
777 controller->delegate()->SetBubbleInfoBeenAcknowledged(kId2, false);
779 // Simulate clicking the learn more link to dismiss it.
780 bubble.set_action_on_show(
781 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_LINK);
782 controller.reset(new TestNtpOverriddenBubbleController(profile()));
783 EXPECT_TRUE(controller->ShouldShow(kId2));
784 bubble.set_controller(controller.get());
785 controller->Show(&bubble);
786 EXPECT_EQ(1U, controller->link_click_count());
787 EXPECT_EQ(0U, controller->action_click_count());
788 EXPECT_EQ(0U, controller->dismiss_click_count());
789 // No extension should have become disabled.
790 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId1) != NULL);
791 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId2) != NULL);
792 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId3) != NULL);
793 // Only extension 2 should have been acknowledged.
794 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId1));
795 EXPECT_TRUE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId2));
796 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId3));
797 // Clean up after ourselves.
798 controller->delegate()->SetBubbleInfoBeenAcknowledged(kId2, false);
800 // Do it again, but now opt to disable the extension.
801 bubble.set_action_on_show(
802 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_ACTION_BUTTON);
803 controller.reset(new TestNtpOverriddenBubbleController(profile()));
804 EXPECT_TRUE(controller->ShouldShow(kId2));
805 override_extensions = controller->GetExtensionList();
806 EXPECT_EQ(1U, override_extensions.size());
807 bubble.set_controller(controller.get());
808 controller->Show(&bubble); // Simulate showing the bubble.
809 EXPECT_EQ(0U, controller->link_click_count());
810 EXPECT_EQ(1U, controller->action_click_count());
811 EXPECT_EQ(0U, controller->dismiss_click_count());
812 // Only extension 2 should have become disabled.
813 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId1) != NULL);
814 EXPECT_TRUE(registry->disabled_extensions().GetByID(kId2) != NULL);
815 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId3) != NULL);
816 // No extension should have been acknowledged (it got disabled).
817 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId1));
818 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId2));
819 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId3));
821 // Clean up after ourselves.
822 service_->UninstallExtension(kId1,
823 extensions::UNINSTALL_REASON_FOR_TESTING,
824 base::Bind(&base::DoNothing),
825 NULL);
826 service_->UninstallExtension(kId2,
827 extensions::UNINSTALL_REASON_FOR_TESTING,
828 base::Bind(&base::DoNothing),
829 NULL);
830 service_->UninstallExtension(kId3,
831 extensions::UNINSTALL_REASON_FOR_TESTING,
832 base::Bind(&base::DoNothing),
833 NULL);
836 void SetInstallTime(const std::string& extension_id,
837 const base::Time& time,
838 ExtensionPrefs* prefs) {
839 std::string time_str = base::Int64ToString(time.ToInternalValue());
840 prefs->UpdateExtensionPref(extension_id,
841 "install_time",
842 new base::StringValue(time_str));
845 // The feature this is meant to test is only implemented on Windows.
846 #if defined(OS_WIN)
847 // http://crbug.com/397426
848 #define MAYBE_ProxyOverriddenControllerTest DISABLED_ProxyOverriddenControllerTest
849 #else
850 #define MAYBE_ProxyOverriddenControllerTest DISABLED_ProxyOverriddenControllerTest
851 #endif
853 TEST_F(ExtensionMessageBubbleTest, MAYBE_ProxyOverriddenControllerTest) {
854 Init();
855 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
856 // Load two extensions overriding proxy and one overriding something
857 // unrelated (to check for interference). Extension 2 should still win
858 // on the proxy setting.
859 ASSERT_TRUE(LoadExtensionOverridingProxy("1", kId1, Manifest::UNPACKED));
860 ASSERT_TRUE(LoadExtensionOverridingProxy("2", kId2, Manifest::UNPACKED));
861 ASSERT_TRUE(LoadExtensionOverridingStart("3", kId3, Manifest::UNPACKED));
863 // The bubble will not show if the extension was installed in the last 7 days
864 // so we artificially set the install time to simulate an old install during
865 // testing.
866 base::Time old_enough = base::Time::Now() - base::TimeDelta::FromDays(8);
867 SetInstallTime(kId1, old_enough, prefs);
868 SetInstallTime(kId2, base::Time::Now(), prefs);
869 SetInstallTime(kId3, old_enough, prefs);
871 scoped_ptr<TestProxyOverriddenBubbleController> controller(
872 new TestProxyOverriddenBubbleController(profile()));
874 // The second extension is too new to warn about.
875 EXPECT_FALSE(controller->ShouldShow(kId1));
876 EXPECT_FALSE(controller->ShouldShow(kId2));
877 // Lets make it old enough.
878 SetInstallTime(kId2, old_enough, prefs);
880 // The list will contain one enabled unpacked extension (ext 2).
881 EXPECT_TRUE(controller->ShouldShow(kId2));
882 EXPECT_FALSE(controller->ShouldShow(kId3));
883 std::vector<base::string16> override_extensions =
884 controller->GetExtensionList();
885 ASSERT_EQ(1U, override_extensions.size());
886 EXPECT_EQ(base::ASCIIToUTF16("Extension 2"), override_extensions[0]);
887 EXPECT_EQ(0U, controller->link_click_count());
888 EXPECT_EQ(0U, controller->dismiss_click_count());
889 EXPECT_EQ(0U, controller->action_click_count());
891 // Simulate showing the bubble and dismissing it.
892 FakeExtensionMessageBubble bubble;
893 bubble.set_action_on_show(
894 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_DISMISS_BUTTON);
895 bubble.set_controller(controller.get());
896 controller->Show(&bubble);
897 EXPECT_EQ(0U, controller->link_click_count());
898 EXPECT_EQ(0U, controller->action_click_count());
899 EXPECT_EQ(1U, controller->dismiss_click_count());
900 // No extension should have become disabled.
901 ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
902 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId1) != NULL);
903 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId2) != NULL);
904 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId3) != NULL);
905 // Only extension 2 should have been acknowledged.
906 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId1));
907 EXPECT_TRUE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId2));
908 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId3));
909 // Clean up after ourselves.
910 controller->delegate()->SetBubbleInfoBeenAcknowledged(kId2, false);
912 // Simulate clicking the learn more link to dismiss it.
913 bubble.set_action_on_show(
914 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_LINK);
915 controller.reset(new TestProxyOverriddenBubbleController(profile()));
916 EXPECT_TRUE(controller->ShouldShow(kId2));
917 bubble.set_controller(controller.get());
918 controller->Show(&bubble);
919 EXPECT_EQ(1U, controller->link_click_count());
920 EXPECT_EQ(0U, controller->action_click_count());
921 EXPECT_EQ(0U, controller->dismiss_click_count());
922 // No extension should have become disabled.
923 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId1) != NULL);
924 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId2) != NULL);
925 EXPECT_TRUE(registry->enabled_extensions().GetByID(kId3) != NULL);
926 // Only extension 2 should have been acknowledged.
927 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId1));
928 EXPECT_TRUE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId2));
929 EXPECT_FALSE(controller->delegate()->HasBubbleInfoBeenAcknowledged(kId3));
930 // Clean up after ourselves.
931 controller->delegate()->SetBubbleInfoBeenAcknowledged(kId2, false);
933 // Do it again, but now opt to disable the extension.
934 bubble.set_action_on_show(
935 FakeExtensionMessageBubble::BUBBLE_ACTION_CLICK_ACTION_BUTTON);
936 controller.reset(new TestProxyOverriddenBubbleController(profile()));
937 EXPECT_TRUE(controller->ShouldShow(kId2));
938 override_extensions = controller->GetExtensionList();
939 EXPECT_EQ(1U, override_extensions.size());
940 bubble.set_controller(controller.get());
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),
959 NULL);
960 service_->UninstallExtension(kId2,
961 extensions::UNINSTALL_REASON_FOR_TESTING,
962 base::Bind(&base::DoNothing),
963 NULL);
964 service_->UninstallExtension(kId3,
965 extensions::UNINSTALL_REASON_FOR_TESTING,
966 base::Bind(&base::DoNothing),
967 NULL);
970 } // namespace extensions