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