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