blob: 49f83f1408fff653c14a63cc3a9ffa54ba7aa2ef [file] [log] [blame]
[email protected]a44657202012-01-09 05:48:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]cfc6cca2011-12-01 02:30:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]ad687a72012-06-08 06:03:115#include "chrome/app/chrome_command_ids.h"
[email protected]605fb8102012-05-04 01:36:556#include "chrome/browser/automation/automation_util.h"
[email protected]375003a2011-12-13 02:53:217#include "chrome/browser/tab_contents/render_view_context_menu.h"
[email protected]259771102012-05-31 16:52:208#include "chrome/browser/extensions/extension_test_message_listener.h"
9#include "chrome/browser/extensions/platform_app_browsertest_util.h"
10#include "chrome/browser/extensions/shell_window_registry.h"
[email protected]cfc6cca2011-12-01 02:30:0611#include "chrome/browser/ui/browser.h"
[email protected]52877dbc62012-06-29 22:22:0312#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]d72d3a62012-05-10 03:45:0813#include "chrome/browser/ui/extensions/shell_window.h"
[email protected]5b1a04b42012-06-15 00:41:4414#include "chrome/common/chrome_notification_types.h"
[email protected]cfc6cca2011-12-01 02:30:0615#include "chrome/test/base/ui_test_utils.h"
[email protected]cfc6cca2011-12-01 02:30:0616
[email protected]bb81f382012-01-03 22:45:4417using content::WebContents;
[email protected]31bdbfef2012-05-22 19:59:1518using extensions::Extension;
19
[email protected]375003a2011-12-13 02:53:2120namespace {
21// Non-abstract RenderViewContextMenu class.
22class PlatformAppContextMenu : public RenderViewContextMenu {
23 public:
[email protected]bb81f382012-01-03 22:45:4424 PlatformAppContextMenu(WebContents* web_contents,
[email protected]35be7ec2012-02-12 20:42:5125 const content::ContextMenuParams& params)
[email protected]bb81f382012-01-03 22:45:4426 : RenderViewContextMenu(web_contents, params) {}
[email protected]375003a2011-12-13 02:53:2127
[email protected]ad687a72012-06-08 06:03:1128 bool HasCommandWithId(int command_id) {
29 return menu_model_.GetIndexOfCommandId(command_id) != -1;
30 }
31
[email protected]375003a2011-12-13 02:53:2132 protected:
33 // These two functions implement pure virtual methods of
34 // RenderViewContextMenu.
35 virtual bool GetAcceleratorForCommandId(int command_id,
36 ui::Accelerator* accelerator) {
37 return false;
38 }
39 virtual void PlatformInit() {}
[email protected]f5d5cd02012-05-04 13:59:3040 virtual void PlatformCancel() {}
[email protected]375003a2011-12-13 02:53:2141};
42
43} // namespace
44
[email protected]eb4bcac2012-06-27 01:32:0845// Tests that CreateShellWindow doesn't crash if you close it straight away.
46// LauncherPlatformAppBrowserTest relies on this behaviour, but is only run for
47// ash, so we test that it works here.
48IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, CreateAndCloseShellWindow) {
49 const Extension* extension = LoadAndLaunchPlatformApp("minimal");
50 ShellWindow* window = CreateShellWindow(extension);
51 CloseShellWindow(window);
52}
53
[email protected]dc37b002012-04-23 23:02:2654// Tests that platform apps received the "launch" event when launched.
55IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) {
56 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_;
[email protected]cfc6cca2011-12-01 02:30:0657}
[email protected]375003a2011-12-13 02:53:2158
[email protected]edd7ed692012-02-08 02:50:0359IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
[email protected]dc37b002012-04-23 23:02:2660 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]31bdbfef2012-05-22 19:59:1561 LoadAndLaunchPlatformApp("minimal");
[email protected]dc37b002012-04-23 23:02:2662
63 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:2164
[email protected]375003a2011-12-13 02:53:2165 // The empty app doesn't add any context menu items, so its menu should
[email protected]0cfacd82012-02-09 01:55:3366 // only include the developer tools.
[email protected]d72d3a62012-05-10 03:45:0867 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:3168 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:2169 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:5170 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:4471 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:2172 params);
73 menu->Init();
[email protected]ad687a72012-06-08 06:03:1174 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
75 ASSERT_TRUE(menu->HasCommandWithId(IDC_RELOAD));
76 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
77 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]375003a2011-12-13 02:53:2178}
79
[email protected]edd7ed692012-02-08 02:50:0380IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) {
[email protected]dc37b002012-04-23 23:02:2681 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]375003a2011-12-13 02:53:2182 LoadAndLaunchPlatformApp("context_menu");
83
[email protected]dc37b002012-04-23 23:02:2684 // Wait for the extension to tell us it's initialized its context menus and
85 // launched a window.
86 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:2187
[email protected]0cfacd82012-02-09 01:55:3388 // The context_menu app has one context menu item. This, along with a
89 // separator and the developer tools, is all that should be in the menu.
[email protected]d72d3a62012-05-10 03:45:0890 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:3191 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:2192 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:5193 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:4494 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:2195 params);
96 menu->Init();
[email protected]ad687a72012-06-08 06:03:1197 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
98 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
99 ASSERT_TRUE(menu->HasCommandWithId(IDC_RELOAD));
100 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
101 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]375003a2011-12-13 02:53:21102}
[email protected]5f000f272012-01-19 05:25:08103
[email protected]edd7ed692012-02-08 02:50:03104IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) {
[email protected]dc37b002012-04-23 23:02:26105 ASSERT_TRUE(StartTestServer());
106 ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_;
[email protected]5f000f272012-01-19 05:25:08107}
[email protected]863e6472012-01-24 19:33:58108
[email protected]fd3238af2012-05-22 18:55:30109IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Iframes) {
110 ASSERT_TRUE(StartTestServer());
111 ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_;
112}
113
[email protected]2aac7ff2012-01-25 18:05:11114// Tests that localStorage and WebSQL are disabled for platform apps.
[email protected]edd7ed692012-02-08 02:50:03115IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) {
[email protected]2aac7ff2012-01-25 18:05:11116 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_;
117}
[email protected]6a5a2e52012-03-22 03:21:12118
[email protected]c0cecd1f2012-04-05 16:50:12119IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) {
120 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_;
121}
122
[email protected]eb4bcac2012-06-27 01:32:08123// Tests that platform apps can use the chrome.appWindow.* API.
[email protected]dc37b002012-04-23 23:02:26124IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) {
[email protected]6a5a2e52012-03-22 03:21:12125 ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_;
126}
[email protected]605fb8102012-05-04 01:36:55127
128// Tests that platform apps have isolated storage by default.
129IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Isolation) {
130 ASSERT_TRUE(StartTestServer());
131
132 // Load a (non-app) page under the "localhost" origin that sets a cookie.
133 GURL set_cookie_url = test_server()->GetURL(
134 "files/extensions/platform_apps/isolation/set_cookie.html");
135 GURL::Replacements replace_host;
136 std::string host_str("localhost"); // Must stay in scope with replace_host.
137 replace_host.SetHostStr(host_str);
138 set_cookie_url = set_cookie_url.ReplaceComponents(replace_host);
139
140 ui_test_utils::NavigateToURLWithDisposition(
141 browser(), set_cookie_url,
142 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
143
144 // Make sure the cookie is set.
145 int cookie_size;
146 std::string cookie_value;
147 automation_util::GetCookies(
148 set_cookie_url,
[email protected]52877dbc62012-06-29 22:22:03149 chrome::GetWebContentsAt(browser(), 0),
[email protected]605fb8102012-05-04 01:36:55150 &cookie_size,
151 &cookie_value);
152 ASSERT_EQ("testCookie=1", cookie_value);
153
154 // Let the platform app request the same URL, and make sure that it doesn't
155 // see the cookie.
156 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_;
157}
[email protected]31bdbfef2012-05-22 19:59:15158
159IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ExtensionWindowingApis) {
160 // Initially there should be just the one browser window visible to the
161 // extensions API.
162 const Extension* extension = LoadExtension(
163 test_data_dir_.AppendASCII("common/background_page"));
164 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
165
166 // And no shell windows.
167 ASSERT_EQ(0U, GetShellWindowCount());
168
169 // Launch a platform app that shows a window.
170 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]ad5bb8a92012-06-07 03:55:58171 LoadAndLaunchPlatformApp("minimal");
[email protected]31bdbfef2012-05-22 19:59:15172 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
173 ASSERT_EQ(1U, GetShellWindowCount());
174 ShellWindowRegistry::ShellWindowSet shell_windows =
175 ShellWindowRegistry::Get(browser()->profile())->shell_windows();
176 int shell_window_id = (*shell_windows.begin())->session_id().id();
177
178 // But it's not visible to the extensions API, it still thinks there's just
179 // one browser window.
180 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
181 // It can't look it up by ID either
182 ASSERT_FALSE(RunGetWindowFunctionForExtension(shell_window_id, extension));
183
184 // The app can also only see one window (its own).
[email protected]ad5bb8a92012-06-07 03:55:58185 // TODO(jeremya): add an extension function to get a shell window by ID, and
186 // to get a list of all the shell windows, so we can test this.
[email protected]31bdbfef2012-05-22 19:59:15187
188 // Launch another platform app that also shows a window.
189 ExtensionTestMessageListener launched_listener2("Launched", false);
[email protected]ad5bb8a92012-06-07 03:55:58190 LoadAndLaunchPlatformApp("context_menu");
[email protected]31bdbfef2012-05-22 19:59:15191 ASSERT_TRUE(launched_listener2.WaitUntilSatisfied());
192
193 // There are two total shell windows, but each app can only see its own.
194 ASSERT_EQ(2U, GetShellWindowCount());
[email protected]ad5bb8a92012-06-07 03:55:58195 // TODO(jeremya): as above, this requires more extension functions.
[email protected]31bdbfef2012-05-22 19:59:15196}
[email protected]12e540452012-05-26 07:09:36197
198// TODO(benwells): fix these tests for ChromeOS.
199#if !defined(OS_CHROMEOS)
200// Tests that command line parameters get passed through to platform apps
201// via launchData correctly when launching with a file.
202IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFile) {
203 SetCommandLineArg( "platform_apps/launch_files/test.txt");
204 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file"))
205 << message_;
206}
207
208// Tests that no launch data is sent through if the platform app provides
209// an intent with the wrong action.
210IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongIntent) {
211 SetCommandLineArg("platform_apps/launch_files/test.txt");
212 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_intent"))
213 << message_;
214}
215
216// Tests that no launch data is sent through if the file is of the wrong MIME
217// type.
218IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongType) {
219 SetCommandLineArg("platform_apps/launch_files/test.txt");
220 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_type"))
221 << message_;
222}
223
224// Tests that no launch data is sent through if the platform app does not
225// provide an intent.
226IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNoIntent) {
227 SetCommandLineArg("platform_apps/launch_files/test.txt");
228 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_no_intent"))
229 << message_;
230}
231
232// Tests that no launch data is sent through if the file MIME type cannot
233// be read.
234IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoType) {
235 SetCommandLineArg("platform_apps/launch_files/test.unknownextension");
236 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
237 << message_;
238}
239
240// Tests that no launch data is sent through if the file does not exist.
241IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoFile) {
242 SetCommandLineArg("platform_apps/launch_files/doesnotexist.txt");
243 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
244 << message_;
245}
246
247// Tests that no launch data is sent through if the argument is a directory.
248IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithDirectory) {
249 SetCommandLineArg("platform_apps/launch_files");
250 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
251 << message_;
252}
253
254// Tests that no launch data is sent through if there are no arguments passed
255// on the command line
256IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNothing) {
257 ClearCommandLineArgs();
258 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_nothing"))
259 << message_;
260}
[email protected]ffc7b4d2012-06-08 00:05:32261
262// Test that platform apps can use the chrome.fileSystem.getDisplayPath
263// function to get the native file system path of a file they are launched with.
264IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, GetDisplayPath) {
265 SetCommandLineArg("platform_apps/launch_files/test.txt");
266 ASSERT_TRUE(RunPlatformAppTest("platform_apps/get_display_path"))
267 << message_;
268}
269
[email protected]12e540452012-05-26 07:09:36270#endif // defined(OS_CHROMEOS)
[email protected]5b1a04b42012-06-15 00:41:44271
272IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OpenLink) {
273 ASSERT_TRUE(StartTestServer());
274 ui_test_utils::WindowedNotificationObserver observer(
275 chrome::NOTIFICATION_TAB_ADDED,
276 content::Source<content::WebContentsDelegate>(browser()));
277 LoadAndLaunchPlatformApp("open_link");
278 observer.Wait();
279 ASSERT_EQ(2, browser()->tab_count());
280}