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.
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/bookmark_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 typedef ExtensionBrowserTest BookmarkAppTest
;
35 // Used by ShouldLocationBarForXXX. Performs a navigation and then checks that
36 // the location bar visibility is as expcted.
37 void NavigateAndCheckForLocationBar(Browser
* browser
,
38 const std::string
& url_string
,
39 bool expected_visibility
) {
41 ui_test_utils::NavigateToURL(browser
, url
);
42 EXPECT_EQ(expected_visibility
,
43 browser
->bookmark_app_controller()->ShouldShowLocationBar());
48 // Check that the location bar is shown correctly for HTTP bookmark apps.
49 IN_PROC_BROWSER_TEST_F(BookmarkAppTest
,
50 ShouldShowLocationBarForHTTPBookmarkApp
) {
51 base::CommandLine::ForCurrentProcess()->AppendSwitch(
52 switches::kEnableNewBookmarkApps
);
53 ASSERT_TRUE(test_server()->Start());
55 // Load a http bookmark app.
56 const Extension
* http_bookmark_app
= InstallExtensionWithSourceAndFlags(
57 test_data_dir_
.AppendASCII("app/"),
59 extensions::Manifest::INTERNAL
,
60 extensions::Extension::FROM_BOOKMARK
);
61 ASSERT_TRUE(http_bookmark_app
);
63 // Launch it in a window.
64 WebContents
* app_window
= OpenApplication(AppLaunchParams(
65 browser()->profile(), http_bookmark_app
,
66 extensions::LAUNCH_CONTAINER_WINDOW
, NEW_WINDOW
,
67 extensions::SOURCE_TEST
));
68 ASSERT_TRUE(app_window
);
70 // Find the new browser.
71 Browser
* http_app_browser
= NULL
;
72 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
74 web_app::GetExtensionIdFromApplicationName((*it
)->app_name());
75 if (*it
== browser()) {
77 } else if (app_id
== http_bookmark_app
->id()) {
78 http_app_browser
= *it
;
81 ASSERT_TRUE(http_app_browser
);
82 ASSERT_TRUE(http_app_browser
!= browser());
84 // Navigate to the app's launch page; the location bar should be hidden.
85 NavigateAndCheckForLocationBar(
86 http_app_browser
, "http://www.example.com/empty.html", false);
88 // Navigate to another page on the same origin; the location bar should still
90 NavigateAndCheckForLocationBar(
91 http_app_browser
, "http://www.example.com/blah", false);
93 // Navigate to the https version of the site; the location bar should
94 // be hidden for both browsers.
95 NavigateAndCheckForLocationBar(
96 http_app_browser
, "https://www.example.com/blah", false);
98 // Navigate to different origin; the location bar should now be visible.
99 NavigateAndCheckForLocationBar(
100 http_app_browser
, "http://www.foo.com/blah", true);
102 // Navigate back to the app's origin; the location bar should now be hidden.
103 NavigateAndCheckForLocationBar(
104 http_app_browser
, "http://www.example.com/blah", false);
107 // Check that the location bar is shown correctly for HTTPS bookmark apps.
108 IN_PROC_BROWSER_TEST_F(BookmarkAppTest
,
109 ShouldShowLocationBarForHTTPSBookmarkApp
) {
110 base::CommandLine::ForCurrentProcess()->AppendSwitch(
111 switches::kEnableNewBookmarkApps
);
112 ASSERT_TRUE(test_server()->Start());
114 // Load a https bookmark app.
115 const Extension
* https_bookmark_app
= InstallExtensionWithSourceAndFlags(
116 test_data_dir_
.AppendASCII("https_app/"),
118 extensions::Manifest::INTERNAL
,
119 extensions::Extension::FROM_BOOKMARK
);
120 ASSERT_TRUE(https_bookmark_app
);
122 // Launch it in a window.
123 WebContents
* app_window
= OpenApplication(AppLaunchParams(
124 browser()->profile(), https_bookmark_app
,
125 extensions::LAUNCH_CONTAINER_WINDOW
, NEW_WINDOW
,
126 extensions::SOURCE_TEST
));
127 ASSERT_TRUE(app_window
);
129 // Find the new browser.
130 Browser
* https_app_browser
= NULL
;
131 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
133 web_app::GetExtensionIdFromApplicationName((*it
)->app_name());
134 if (*it
== browser()) {
136 } else if (app_id
== https_bookmark_app
->id()) {
137 https_app_browser
= *it
;
140 ASSERT_TRUE(https_app_browser
);
141 ASSERT_TRUE(https_app_browser
!= browser());
143 // Navigate to the app's launch page; the location bar should be hidden.
144 NavigateAndCheckForLocationBar(
145 https_app_browser
, "https://www.example.com/empty.html", false);
147 // Navigate to another page on the same origin; the location bar should still
149 NavigateAndCheckForLocationBar(
150 https_app_browser
, "https://www.example.com/blah", false);
152 // Navigate to the http version of the site; the location bar should
153 // be visible for the https version as it is now on a less secure version
155 NavigateAndCheckForLocationBar(
156 https_app_browser
, "http://www.example.com/blah", true);
158 // Navigate to different origin; the location bar should now be visible.
159 NavigateAndCheckForLocationBar(
160 https_app_browser
, "http://www.foo.com/blah", true);
162 // Navigate back to the app's origin; the location bar should now be hidden.
163 NavigateAndCheckForLocationBar(
164 https_app_browser
, "https://www.example.com/blah", false);
167 // Open a normal browser window, a hosted app window, a legacy packaged app
168 // window and a dev tools window, and check that the web app frame feature is
169 // supported correctly.
170 IN_PROC_BROWSER_TEST_F(BookmarkAppTest
, ShouldUseWebAppFrame
) {
171 ASSERT_TRUE(test_server()->Start());
172 base::CommandLine::ForCurrentProcess()->AppendSwitch(
173 switches::kEnableWebAppFrame
);
175 // Load a hosted app.
176 const Extension
* bookmark_app
= InstallExtensionWithSourceAndFlags(
177 test_data_dir_
.AppendASCII("app/"),
179 extensions::Manifest::INTERNAL
,
180 extensions::Extension::FROM_BOOKMARK
);
181 ASSERT_TRUE(bookmark_app
);
183 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
184 WebContents
* bookmark_app_window
= OpenApplication(AppLaunchParams(
185 browser()->profile(), bookmark_app
, extensions::LAUNCH_CONTAINER_WINDOW
,
186 NEW_WINDOW
, extensions::SOURCE_UNTRACKED
));
187 ASSERT_TRUE(bookmark_app_window
);
189 // Load a packaged app.
190 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("packaged_app/")));
191 const Extension
* packaged_app
= nullptr;
192 extensions::ExtensionRegistry
* registry
=
193 extensions::ExtensionRegistry::Get(browser()->profile());
194 for (const scoped_refptr
<const extensions::Extension
>& extension
:
195 registry
->enabled_extensions()) {
196 if (extension
->name() == "Packaged App Test")
197 packaged_app
= extension
.get();
199 ASSERT_TRUE(packaged_app
);
201 // Launch it in a window, as AppLauncherHandler::HandleLaunchApp() would.
202 WebContents
* packaged_app_window
= OpenApplication(AppLaunchParams(
203 browser()->profile(), packaged_app
, extensions::LAUNCH_CONTAINER_WINDOW
,
204 NEW_WINDOW
, extensions::SOURCE_UNTRACKED
));
205 ASSERT_TRUE(packaged_app_window
);
207 DevToolsWindow
* devtools_window
=
208 DevToolsWindowTesting::OpenDevToolsWindowSync(browser(), false);
210 // The launch should have created a new app browser and a dev tools browser.
211 ASSERT_EQ(4u, chrome::GetBrowserCount(browser()->profile(),
212 browser()->host_desktop_type()));
214 // Find the new browsers.
215 Browser
* bookmark_app_browser
= NULL
;
216 Browser
* packaged_app_browser
= NULL
;
217 Browser
* dev_tools_browser
= NULL
;
218 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
219 if (*it
== browser()) {
221 } else if ((*it
)->app_name() == DevToolsWindow::kDevToolsApp
) {
222 dev_tools_browser
= *it
;
223 } else if ((*it
)->tab_strip_model()->GetActiveWebContents() ==
224 bookmark_app_window
) {
225 bookmark_app_browser
= *it
;
227 packaged_app_browser
= *it
;
230 ASSERT_TRUE(dev_tools_browser
);
231 ASSERT_TRUE(bookmark_app_browser
);
232 ASSERT_TRUE(bookmark_app_browser
!= browser());
233 ASSERT_TRUE(packaged_app_browser
);
234 ASSERT_TRUE(packaged_app_browser
!= browser());
235 ASSERT_TRUE(packaged_app_browser
!= bookmark_app_browser
);
237 EXPECT_FALSE(browser()->SupportsWindowFeature(Browser::FEATURE_WEBAPPFRAME
));
239 dev_tools_browser
->SupportsWindowFeature(Browser::FEATURE_WEBAPPFRAME
));
240 EXPECT_EQ(browser()->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH
,
241 bookmark_app_browser
->SupportsWindowFeature(
242 Browser::FEATURE_WEBAPPFRAME
));
243 EXPECT_FALSE(packaged_app_browser
->SupportsWindowFeature(
244 Browser::FEATURE_WEBAPPFRAME
));
246 DevToolsWindowTesting::CloseDevToolsWindowSync(devtools_window
);