Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / extensions / hosted_app_browsertest.cc
blob55abe04ffb570550426f6255a6b0e46dd2e17ae0
1 // Copyright 2015 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.
5 #include <string>
7 #include "base/command_line.h"
8 #include "base/compiler_specific.h"
9 #include "chrome/browser/devtools/devtools_window_testing.h"
10 #include "chrome/browser/extensions/extension_browsertest.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_util.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/browser_iterator.h"
15 #include "chrome/browser/ui/extensions/app_launch_params.h"
16 #include "chrome/browser/ui/extensions/application_launch.h"
17 #include "chrome/browser/ui/extensions/hosted_app_browser_controller.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/browser/web_applications/web_app.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/test/base/ui_test_utils.h"
22 #include "content/public/browser/web_contents.h"
23 #include "extensions/browser/extension_registry.h"
24 #include "extensions/common/constants.h"
25 #include "extensions/common/extension.h"
26 #include "extensions/common/extension_set.h"
28 using content::WebContents;
29 using extensions::Extension;
31 namespace {
33 // Used by ShouldLocationBarForXXX. Performs a navigation and then checks that
34 // the location bar visibility is as expcted.
35 void NavigateAndCheckForLocationBar(Browser* browser,
36 const std::string& url_string,
37 bool expected_visibility) {
38 GURL url(url_string);
39 ui_test_utils::NavigateToURL(browser, url);
40 EXPECT_EQ(expected_visibility,
41 browser->hosted_app_controller()->ShouldShowLocationBar());
44 } // namespace
46 class HostedAppTest : public ExtensionBrowserTest {
47 public:
48 HostedAppTest() : app_browser_(nullptr) {}
49 ~HostedAppTest() override {}
51 protected:
52 void SetupApp(const std::string& app_folder, bool is_bookmark_app) {
53 const Extension* app = InstallExtensionWithSourceAndFlags(
54 test_data_dir_.AppendASCII(app_folder), 1,
55 extensions::Manifest::INTERNAL,
56 is_bookmark_app ? extensions::Extension::FROM_BOOKMARK
57 : extensions::Extension::NO_FLAGS);
58 ASSERT_TRUE(app);
60 // Launch it in a window.
61 ASSERT_TRUE(OpenApplication(AppLaunchParams(
62 browser()->profile(), app, extensions::LAUNCH_CONTAINER_WINDOW,
63 NEW_WINDOW, extensions::SOURCE_TEST)));
65 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
66 if (*it == browser())
67 continue;
69 std::string browser_app_id =
70 web_app::GetExtensionIdFromApplicationName((*it)->app_name());
71 if (browser_app_id == app->id()) {
72 app_browser_ = *it;
73 break;
77 ASSERT_TRUE(app_browser_);
78 ASSERT_TRUE(app_browser_ != browser());
81 Browser* app_browser_;
84 // Check that the location bar is shown correctly for bookmark apps.
85 IN_PROC_BROWSER_TEST_F(HostedAppTest,
86 ShouldShowLocationBarForBookmarkApp) {
87 base::CommandLine::ForCurrentProcess()->AppendSwitch(
88 switches::kEnableNewBookmarkApps);
90 SetupApp("app", true);
92 // Navigate to the app's launch page; the location bar should be hidden.
93 NavigateAndCheckForLocationBar(
94 app_browser_, "http://www.example.com/empty.html", false);
96 // Navigate to another page on the same origin; the location bar should still
97 // hidden.
98 NavigateAndCheckForLocationBar(
99 app_browser_, "http://www.example.com/blah", false);
101 // Navigate to different origin; the location bar should now be visible.
102 NavigateAndCheckForLocationBar(
103 app_browser_, "http://www.foo.com/blah", true);
106 // Check that the location bar is shown correctly for HTTP bookmark apps when
107 // they navigate to a HTTPS page on the same origin.
108 IN_PROC_BROWSER_TEST_F(HostedAppTest,
109 ShouldShowLocationBarForHTTPBookmarkApp) {
110 base::CommandLine::ForCurrentProcess()->AppendSwitch(
111 switches::kEnableNewBookmarkApps);
113 SetupApp("app", true);
115 // Navigate to the app's launch page; the location bar should be hidden.
116 NavigateAndCheckForLocationBar(
117 app_browser_, "http://www.example.com/empty.html", false);
119 // Navigate to the https version of the site; the location bar should
120 // be hidden.
121 NavigateAndCheckForLocationBar(
122 app_browser_, "https://www.example.com/blah", false);
125 // Check that the location bar is shown correctly for HTTPS bookmark apps when
126 // they navigate to a HTTP page on the same origin.
127 IN_PROC_BROWSER_TEST_F(HostedAppTest,
128 ShouldShowLocationBarForHTTPSBookmarkApp) {
129 base::CommandLine::ForCurrentProcess()->AppendSwitch(
130 switches::kEnableNewBookmarkApps);
132 SetupApp("https_app", true);
134 // Navigate to the app's launch page; the location bar should be hidden.
135 NavigateAndCheckForLocationBar(
136 app_browser_, "https://www.example.com/empty.html", false);
138 // Navigate to the http version of the site; the location bar should
139 // be visible for the https version as it is now on a less secure version
140 // of its host.
141 NavigateAndCheckForLocationBar(
142 app_browser_, "http://www.example.com/blah", true);
145 // Check that the location bar is shown correctly for normal hosted apps.
146 IN_PROC_BROWSER_TEST_F(HostedAppTest,
147 ShouldShowLocationBarForHostedApp) {
148 SetupApp("app", false);
150 // Navigate to the app's launch page; the location bar should be hidden.
151 NavigateAndCheckForLocationBar(
152 app_browser_, "http://www.example.com/empty.html", false);
154 // Navigate to another page on the same origin; the location bar should still
155 // hidden.
156 NavigateAndCheckForLocationBar(
157 app_browser_, "http://www.example.com/blah", false);
159 // Navigate to different origin; the location bar should now be visible.
160 NavigateAndCheckForLocationBar(
161 app_browser_, "http://www.foo.com/blah", true);
164 // Check that the location bar is shown correctly for hosted apps that specify
165 // start URLs without the 'www.' prefix.
166 IN_PROC_BROWSER_TEST_F(HostedAppTest,
167 LocationBarForHostedAppWithoutWWW) {
168 SetupApp("app_no_www", false);
170 // Navigate to the app's launch page; the location bar should be hidden.
171 NavigateAndCheckForLocationBar(
172 app_browser_, "http://example.com/empty.html", false);
174 // Navigate to the app's launch page with the 'www.' prefis; the location bar
175 // should be hidden.
176 NavigateAndCheckForLocationBar(
177 app_browser_, "http://www.example.com/empty.html", false);
179 // Navigate to different origin; the location bar should now be visible.
180 NavigateAndCheckForLocationBar(
181 app_browser_, "http://www.foo.com/blah", true);
184 // Open a normal browser window, a hosted app window, a legacy packaged app
185 // window and a dev tools window, and check that the web app frame feature is
186 // supported correctly.
187 IN_PROC_BROWSER_TEST_F(HostedAppTest, ShouldUseWebAppFrame) {
188 base::CommandLine::ForCurrentProcess()->AppendSwitch(
189 switches::kEnableWebAppFrame);
191 // Load a hosted app.
192 const Extension* bookmark_app = InstallExtensionWithSourceAndFlags(
193 test_data_dir_.AppendASCII("app"),
195 extensions::Manifest::INTERNAL,
196 extensions::Extension::FROM_BOOKMARK);
197 ASSERT_TRUE(bookmark_app);
199 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
200 WebContents* bookmark_app_window = OpenApplication(AppLaunchParams(
201 browser()->profile(), bookmark_app, extensions::LAUNCH_CONTAINER_WINDOW,
202 NEW_WINDOW, extensions::SOURCE_UNTRACKED));
203 ASSERT_TRUE(bookmark_app_window);
205 // Load a packaged app.
206 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("packaged_app")));
207 const Extension* packaged_app = nullptr;
208 extensions::ExtensionRegistry* registry =
209 extensions::ExtensionRegistry::Get(browser()->profile());
210 for (const scoped_refptr<const extensions::Extension>& extension :
211 registry->enabled_extensions()) {
212 if (extension->name() == "Packaged App Test")
213 packaged_app = extension.get();
215 ASSERT_TRUE(packaged_app);
217 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
218 WebContents* packaged_app_window = OpenApplication(AppLaunchParams(
219 browser()->profile(), packaged_app, extensions::LAUNCH_CONTAINER_WINDOW,
220 NEW_WINDOW, extensions::SOURCE_UNTRACKED));
221 ASSERT_TRUE(packaged_app_window);
223 DevToolsWindow* devtools_window =
224 DevToolsWindowTesting::OpenDevToolsWindowSync(browser(), false);
226 // The launch should have created a new app browser and a dev tools browser.
227 ASSERT_EQ(4u, chrome::GetBrowserCount(browser()->profile(),
228 browser()->host_desktop_type()));
230 // Find the new browsers.
231 Browser* bookmark_app_browser = nullptr;
232 Browser* packaged_app_browser = nullptr;
233 Browser* dev_tools_browser = nullptr;
234 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
235 if (*it == browser()) {
236 continue;
237 } else if ((*it)->app_name() == DevToolsWindow::kDevToolsApp) {
238 dev_tools_browser = *it;
239 } else if ((*it)->tab_strip_model()->GetActiveWebContents() ==
240 bookmark_app_window) {
241 bookmark_app_browser = *it;
242 } else {
243 packaged_app_browser = *it;
246 ASSERT_TRUE(dev_tools_browser);
247 ASSERT_TRUE(bookmark_app_browser);
248 ASSERT_TRUE(bookmark_app_browser != browser());
249 ASSERT_TRUE(packaged_app_browser);
250 ASSERT_TRUE(packaged_app_browser != browser());
251 ASSERT_TRUE(packaged_app_browser != bookmark_app_browser);
253 EXPECT_FALSE(browser()->SupportsWindowFeature(Browser::FEATURE_WEBAPPFRAME));
254 EXPECT_FALSE(
255 dev_tools_browser->SupportsWindowFeature(Browser::FEATURE_WEBAPPFRAME));
256 EXPECT_EQ(browser()->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH,
257 bookmark_app_browser->SupportsWindowFeature(
258 Browser::FEATURE_WEBAPPFRAME));
259 EXPECT_FALSE(packaged_app_browser->SupportsWindowFeature(
260 Browser::FEATURE_WEBAPPFRAME));
262 DevToolsWindowTesting::CloseDevToolsWindowSync(devtools_window);