window_open_apitest.cc revision 4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7
1// Copyright (c) 2010 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/extensions/extension_apitest.h"
7#include "chrome/browser/tab_contents/tab_contents.h"
8#include "chrome/browser/ui/browser.h"
9#include "chrome/browser/ui/browser_list.h"
10#include "chrome/common/chrome_switches.h"
11#include "chrome/test/ui_test_utils.h"
12#include "net/base/mock_host_resolver.h"
13
14IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpen) {
15  CommandLine::ForCurrentProcess()->AppendSwitch(
16      switches::kEnableExperimentalExtensionApis);
17
18  ResultCatcher catcher;
19  ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
20      .AppendASCII("window_open").AppendASCII("spanning")));
21  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
22}
23
24void WaitForTabsAndPopups(Browser* browser, int num_tabs, int num_popups) {
25  // We start with one tab and one browser already open.
26  ++num_tabs;
27  size_t num_browsers = static_cast<size_t>(num_popups) + 1;
28
29  while (true) {
30    if (BrowserList::GetBrowserCount(browser->profile()) < num_browsers ||
31        browser->tab_count() < num_tabs) {
32      MessageLoopForUI::current()->RunAllPending();
33      continue;
34    }
35
36    ASSERT_EQ(num_browsers, BrowserList::GetBrowserCount(browser->profile()));
37    ASSERT_EQ(num_tabs, browser->tab_count());
38
39    for (BrowserList::const_iterator iter = BrowserList::begin();
40         iter != BrowserList::end(); ++iter) {
41      if (*iter == browser)
42        continue;
43
44      // Check for TYPE_POPUP or TYPE_APP_POPUP/PANEL.
45      ASSERT_TRUE((*iter)->type() & Browser::TYPE_POPUP);
46    }
47
48    break;
49  }
50}
51
52IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingExtension) {
53  host_resolver()->AddRule("*", "127.0.0.1");
54  ASSERT_TRUE(StartTestServer());
55
56  ASSERT_TRUE(LoadExtension(
57      test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
58      .AppendASCII("extension")));
59
60  WaitForTabsAndPopups(browser(), 5, 3);
61}
62
63IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingHostedApp) {
64  host_resolver()->AddRule("*", "127.0.0.1");
65  ASSERT_TRUE(test_server()->Start());
66
67  ASSERT_TRUE(LoadExtension(
68      test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
69      .AppendASCII("hosted_app")));
70
71  // The app being tested owns the domain a.com .  The test URLs we navigate
72  // to below must be within that domain, so that they fall within the app's
73  // web extent.
74  GURL::Replacements replace_host;
75  std::string a_dot_com = "a.com";
76  replace_host.SetHostStr(a_dot_com);
77
78  const std::string popup_app_contents_path(
79    "files/extensions/api_test/window_open/popup_blocking/hosted_app/");
80
81  GURL open_tab =
82      test_server()->GetURL(popup_app_contents_path + "open_tab.html")
83          .ReplaceComponents(replace_host);
84  GURL open_popup =
85      test_server()->GetURL(popup_app_contents_path + "open_popup.html")
86          .ReplaceComponents(replace_host);
87
88  browser()->OpenURL(open_tab, GURL(), NEW_FOREGROUND_TAB,
89                     PageTransition::TYPED);
90  browser()->OpenURL(open_popup, GURL(), NEW_FOREGROUND_TAB,
91                     PageTransition::TYPED);
92
93  WaitForTabsAndPopups(browser(), 3, 1);
94}
95