Home | History | Annotate | Download | only in infobars
      1 // Copyright (c) 2012 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 "chrome/browser/chrome_notification_types.h"
      7 #include "chrome/browser/extensions/crx_installer.h"
      8 #include "chrome/browser/extensions/extension_install_prompt.h"
      9 #include "chrome/browser/extensions/extension_service.h"
     10 #include "chrome/browser/infobars/infobar_service.h"
     11 #include "chrome/browser/profiles/profile.h"
     12 #include "chrome/browser/themes/theme_service.h"
     13 #include "chrome/browser/themes/theme_service_factory.h"
     14 #include "chrome/browser/ui/browser.h"
     15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     16 #include "chrome/common/chrome_switches.h"
     17 #include "chrome/test/base/in_process_browser_test.h"
     18 #include "chrome/test/base/test_switches.h"
     19 #include "chrome/test/base/ui_test_utils.h"
     20 #include "content/public/browser/notification_service.h"
     21 #include "net/test/embedded_test_server/embedded_test_server.h"
     22 
     23 class InfoBarsTest : public InProcessBrowserTest {
     24  public:
     25   InfoBarsTest() {}
     26 
     27   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
     28     command_line->AppendSwitchASCII(
     29         switches::kAppsGalleryInstallAutoConfirmForTests, "accept");
     30   }
     31 
     32   void InstallExtension(const char* filename) {
     33     base::FilePath path = ui_test_utils::GetTestFilePath(
     34         base::FilePath().AppendASCII("extensions"),
     35         base::FilePath().AppendASCII(filename));
     36     Profile* profile = browser()->profile();
     37     ExtensionService* service = profile->GetExtensionService();
     38 
     39     content::WindowedNotificationObserver observer(
     40         chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
     41         content::NotificationService::AllSources());
     42 
     43     scoped_ptr<ExtensionInstallPrompt> client(new ExtensionInstallPrompt(
     44         browser()->tab_strip_model()->GetActiveWebContents()));
     45     scoped_refptr<extensions::CrxInstaller> installer(
     46         extensions::CrxInstaller::Create(service, client.Pass()));
     47     installer->set_install_cause(extension_misc::INSTALL_CAUSE_AUTOMATION);
     48     installer->InstallCrx(path);
     49 
     50     observer.Wait();
     51   }
     52 };
     53 
     54 IN_PROC_BROWSER_TEST_F(InfoBarsTest, TestInfoBarsCloseOnNewTheme) {
     55 #if defined(OS_WIN) && defined(USE_ASH)
     56   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
     57   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
     58     return;
     59 #endif
     60 
     61   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
     62 
     63   ui_test_utils::NavigateToURL(
     64       browser(), embedded_test_server()->GetURL("/simple.html"));
     65 
     66   content::WindowedNotificationObserver infobar_added_1(
     67         chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
     68         content::NotificationService::AllSources());
     69   InstallExtension("theme.crx");
     70   infobar_added_1.Wait();
     71 
     72   ui_test_utils::NavigateToURLWithDisposition(
     73       browser(), embedded_test_server()->GetURL("/simple.html"),
     74       NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
     75   content::WindowedNotificationObserver infobar_added_2(
     76         chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
     77         content::NotificationService::AllSources());
     78   content::WindowedNotificationObserver infobar_removed_1(
     79       chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
     80         content::NotificationService::AllSources());
     81   InstallExtension("theme2.crx");
     82   infobar_added_2.Wait();
     83   infobar_removed_1.Wait();
     84   EXPECT_EQ(
     85       0u,
     86       InfoBarService::FromWebContents(
     87           browser()->tab_strip_model()->GetWebContentsAt(0))->infobar_count());
     88 
     89   content::WindowedNotificationObserver infobar_removed_2(
     90       chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
     91         content::NotificationService::AllSources());
     92   ThemeServiceFactory::GetForProfile(browser()->profile())->UseDefaultTheme();
     93   infobar_removed_2.Wait();
     94   EXPECT_EQ(0u,
     95             InfoBarService::FromWebContents(
     96                 browser()->tab_strip_model()->GetActiveWebContents())->
     97                 infobar_count());
     98 }
     99