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 "chrome/test/ui/ui_test.h"
     21 #include "content/public/browser/notification_service.h"
     22 #include "net/test/embedded_test_server/embedded_test_server.h"
     23 
     24 class InfoBarsTest : public InProcessBrowserTest {
     25  public:
     26   InfoBarsTest() {}
     27 
     28   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
     29     command_line->AppendSwitchASCII(
     30         switches::kAppsGalleryInstallAutoConfirmForTests, "accept");
     31   }
     32 
     33   void InstallExtension(const char* filename) {
     34     base::FilePath path = ui_test_utils::GetTestFilePath(
     35         base::FilePath().AppendASCII("extensions"),
     36         base::FilePath().AppendASCII(filename));
     37     Profile* profile = browser()->profile();
     38     ExtensionService* service = profile->GetExtensionService();
     39 
     40     content::WindowedNotificationObserver observer(
     41         chrome::NOTIFICATION_EXTENSION_LOADED,
     42         content::NotificationService::AllSources());
     43 
     44     scoped_ptr<ExtensionInstallPrompt> client(new ExtensionInstallPrompt(
     45         browser()->tab_strip_model()->GetActiveWebContents()));
     46     scoped_refptr<extensions::CrxInstaller> installer(
     47         extensions::CrxInstaller::Create(service, client.Pass()));
     48     installer->set_install_cause(extension_misc::INSTALL_CAUSE_AUTOMATION);
     49     installer->InstallCrx(path);
     50 
     51     observer.Wait();
     52   }
     53 };
     54 
     55 IN_PROC_BROWSER_TEST_F(InfoBarsTest, TestInfoBarsCloseOnNewTheme) {
     56 #if defined(OS_WIN) && defined(USE_ASH)
     57   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
     58   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
     59     return;
     60 #endif
     61 
     62   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
     63 
     64   ui_test_utils::NavigateToURL(
     65       browser(), embedded_test_server()->GetURL("/simple.html"));
     66 
     67   content::WindowedNotificationObserver infobar_added_1(
     68         chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
     69         content::NotificationService::AllSources());
     70   InstallExtension("theme.crx");
     71   infobar_added_1.Wait();
     72 
     73   ui_test_utils::NavigateToURLWithDisposition(
     74       browser(), embedded_test_server()->GetURL("/simple.html"),
     75       NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
     76   content::WindowedNotificationObserver infobar_added_2(
     77         chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
     78         content::NotificationService::AllSources());
     79   content::WindowedNotificationObserver infobar_removed_1(
     80       chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
     81         content::NotificationService::AllSources());
     82   InstallExtension("theme2.crx");
     83   infobar_added_2.Wait();
     84   infobar_removed_1.Wait();
     85   EXPECT_EQ(0u,
     86             InfoBarService::FromWebContents(browser()->tab_strip_model()->
     87                 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(browser()->tab_strip_model()->
     96                 GetActiveWebContents())->infobar_count());
     97 }
     98