blob: 738b6f19cd8d5407b7825776fddf57b66705d47b [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]d72d3a62012-05-10 03:45:0812#include "chrome/browser/ui/extensions/shell_window.h"
[email protected]cfc6cca2011-12-01 02:30:0613#include "chrome/test/base/ui_test_utils.h"
[email protected]cfc6cca2011-12-01 02:30:0614
[email protected]bb81f382012-01-03 22:45:4415using content::WebContents;
[email protected]31bdbfef2012-05-22 19:59:1516using extensions::Extension;
17
[email protected]375003a2011-12-13 02:53:2118namespace {
19// Non-abstract RenderViewContextMenu class.
20class PlatformAppContextMenu : public RenderViewContextMenu {
21 public:
[email protected]bb81f382012-01-03 22:45:4422 PlatformAppContextMenu(WebContents* web_contents,
[email protected]35be7ec2012-02-12 20:42:5123 const content::ContextMenuParams& params)
[email protected]bb81f382012-01-03 22:45:4424 : RenderViewContextMenu(web_contents, params) {}
[email protected]375003a2011-12-13 02:53:2125
[email protected]ad687a72012-06-08 06:03:1126 bool HasCommandWithId(int command_id) {
27 return menu_model_.GetIndexOfCommandId(command_id) != -1;
28 }
29
[email protected]375003a2011-12-13 02:53:2130 protected:
31 // These two functions implement pure virtual methods of
32 // RenderViewContextMenu.
33 virtual bool GetAcceleratorForCommandId(int command_id,
34 ui::Accelerator* accelerator) {
35 return false;
36 }
37 virtual void PlatformInit() {}
[email protected]f5d5cd02012-05-04 13:59:3038 virtual void PlatformCancel() {}
[email protected]375003a2011-12-13 02:53:2139};
40
41} // namespace
42
[email protected]dc37b002012-04-23 23:02:2643// Tests that platform apps received the "launch" event when launched.
44IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) {
45 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_;
[email protected]cfc6cca2011-12-01 02:30:0646}
[email protected]375003a2011-12-13 02:53:2147
[email protected]edd7ed692012-02-08 02:50:0348IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
[email protected]dc37b002012-04-23 23:02:2649 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]31bdbfef2012-05-22 19:59:1550 LoadAndLaunchPlatformApp("minimal");
[email protected]dc37b002012-04-23 23:02:2651
52 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:2153
[email protected]375003a2011-12-13 02:53:2154 // The empty app doesn't add any context menu items, so its menu should
[email protected]0cfacd82012-02-09 01:55:3355 // only include the developer tools.
[email protected]d72d3a62012-05-10 03:45:0856 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:3157 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:2158 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:5159 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:4460 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:2161 params);
62 menu->Init();
[email protected]ad687a72012-06-08 06:03:1163 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
64 ASSERT_TRUE(menu->HasCommandWithId(IDC_RELOAD));
65 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
66 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]375003a2011-12-13 02:53:2167}
68
[email protected]edd7ed692012-02-08 02:50:0369IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) {
[email protected]dc37b002012-04-23 23:02:2670 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]375003a2011-12-13 02:53:2171 LoadAndLaunchPlatformApp("context_menu");
72
[email protected]dc37b002012-04-23 23:02:2673 // Wait for the extension to tell us it's initialized its context menus and
74 // launched a window.
75 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:2176
[email protected]0cfacd82012-02-09 01:55:3377 // The context_menu app has one context menu item. This, along with a
78 // separator and the developer tools, is all that should be in the menu.
[email protected]d72d3a62012-05-10 03:45:0879 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:3180 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:2181 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:5182 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:4483 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:2184 params);
85 menu->Init();
[email protected]ad687a72012-06-08 06:03:1186 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
87 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
88 ASSERT_TRUE(menu->HasCommandWithId(IDC_RELOAD));
89 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
90 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]375003a2011-12-13 02:53:2191}
[email protected]5f000f272012-01-19 05:25:0892
[email protected]edd7ed692012-02-08 02:50:0393IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) {
[email protected]dc37b002012-04-23 23:02:2694 ASSERT_TRUE(StartTestServer());
95 ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_;
[email protected]5f000f272012-01-19 05:25:0896}
[email protected]863e6472012-01-24 19:33:5897
[email protected]fd3238af2012-05-22 18:55:3098IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Iframes) {
99 ASSERT_TRUE(StartTestServer());
100 ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_;
101}
102
[email protected]2aac7ff2012-01-25 18:05:11103// Tests that localStorage and WebSQL are disabled for platform apps.
[email protected]edd7ed692012-02-08 02:50:03104IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) {
[email protected]2aac7ff2012-01-25 18:05:11105 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_;
106}
[email protected]6a5a2e52012-03-22 03:21:12107
[email protected]c0cecd1f2012-04-05 16:50:12108IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) {
109 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_;
110}
111
[email protected]6a5a2e52012-03-22 03:21:12112// Tests that platform apps can use the chrome.windows.* API.
[email protected]dc37b002012-04-23 23:02:26113IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) {
[email protected]6a5a2e52012-03-22 03:21:12114 ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_;
115}
[email protected]605fb8102012-05-04 01:36:55116
117// Tests that platform apps have isolated storage by default.
118IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Isolation) {
119 ASSERT_TRUE(StartTestServer());
120
121 // Load a (non-app) page under the "localhost" origin that sets a cookie.
122 GURL set_cookie_url = test_server()->GetURL(
123 "files/extensions/platform_apps/isolation/set_cookie.html");
124 GURL::Replacements replace_host;
125 std::string host_str("localhost"); // Must stay in scope with replace_host.
126 replace_host.SetHostStr(host_str);
127 set_cookie_url = set_cookie_url.ReplaceComponents(replace_host);
128
129 ui_test_utils::NavigateToURLWithDisposition(
130 browser(), set_cookie_url,
131 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
132
133 // Make sure the cookie is set.
134 int cookie_size;
135 std::string cookie_value;
136 automation_util::GetCookies(
137 set_cookie_url,
138 browser()->GetWebContentsAt(0),
139 &cookie_size,
140 &cookie_value);
141 ASSERT_EQ("testCookie=1", cookie_value);
142
143 // Let the platform app request the same URL, and make sure that it doesn't
144 // see the cookie.
145 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_;
146}
[email protected]31bdbfef2012-05-22 19:59:15147
148IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ExtensionWindowingApis) {
149 // Initially there should be just the one browser window visible to the
150 // extensions API.
151 const Extension* extension = LoadExtension(
152 test_data_dir_.AppendASCII("common/background_page"));
153 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
154
155 // And no shell windows.
156 ASSERT_EQ(0U, GetShellWindowCount());
157
158 // Launch a platform app that shows a window.
159 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]ad5bb8a92012-06-07 03:55:58160 LoadAndLaunchPlatformApp("minimal");
[email protected]31bdbfef2012-05-22 19:59:15161 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
162 ASSERT_EQ(1U, GetShellWindowCount());
163 ShellWindowRegistry::ShellWindowSet shell_windows =
164 ShellWindowRegistry::Get(browser()->profile())->shell_windows();
165 int shell_window_id = (*shell_windows.begin())->session_id().id();
166
167 // But it's not visible to the extensions API, it still thinks there's just
168 // one browser window.
169 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
170 // It can't look it up by ID either
171 ASSERT_FALSE(RunGetWindowFunctionForExtension(shell_window_id, extension));
172
173 // The app can also only see one window (its own).
[email protected]ad5bb8a92012-06-07 03:55:58174 // TODO(jeremya): add an extension function to get a shell window by ID, and
175 // to get a list of all the shell windows, so we can test this.
[email protected]31bdbfef2012-05-22 19:59:15176
177 // Launch another platform app that also shows a window.
178 ExtensionTestMessageListener launched_listener2("Launched", false);
[email protected]ad5bb8a92012-06-07 03:55:58179 LoadAndLaunchPlatformApp("context_menu");
[email protected]31bdbfef2012-05-22 19:59:15180 ASSERT_TRUE(launched_listener2.WaitUntilSatisfied());
181
182 // There are two total shell windows, but each app can only see its own.
183 ASSERT_EQ(2U, GetShellWindowCount());
[email protected]ad5bb8a92012-06-07 03:55:58184 // TODO(jeremya): as above, this requires more extension functions.
[email protected]31bdbfef2012-05-22 19:59:15185}
[email protected]12e540452012-05-26 07:09:36186
187// TODO(benwells): fix these tests for ChromeOS.
188#if !defined(OS_CHROMEOS)
189// Tests that command line parameters get passed through to platform apps
190// via launchData correctly when launching with a file.
191IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFile) {
192 SetCommandLineArg( "platform_apps/launch_files/test.txt");
193 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file"))
194 << message_;
195}
196
197// Tests that no launch data is sent through if the platform app provides
198// an intent with the wrong action.
199IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongIntent) {
200 SetCommandLineArg("platform_apps/launch_files/test.txt");
201 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_intent"))
202 << message_;
203}
204
205// Tests that no launch data is sent through if the file is of the wrong MIME
206// type.
207IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongType) {
208 SetCommandLineArg("platform_apps/launch_files/test.txt");
209 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_type"))
210 << message_;
211}
212
213// Tests that no launch data is sent through if the platform app does not
214// provide an intent.
215IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNoIntent) {
216 SetCommandLineArg("platform_apps/launch_files/test.txt");
217 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_no_intent"))
218 << message_;
219}
220
221// Tests that no launch data is sent through if the file MIME type cannot
222// be read.
223IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoType) {
224 SetCommandLineArg("platform_apps/launch_files/test.unknownextension");
225 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
226 << message_;
227}
228
229// Tests that no launch data is sent through if the file does not exist.
230IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoFile) {
231 SetCommandLineArg("platform_apps/launch_files/doesnotexist.txt");
232 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
233 << message_;
234}
235
236// Tests that no launch data is sent through if the argument is a directory.
237IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithDirectory) {
238 SetCommandLineArg("platform_apps/launch_files");
239 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
240 << message_;
241}
242
243// Tests that no launch data is sent through if there are no arguments passed
244// on the command line
245IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNothing) {
246 ClearCommandLineArgs();
247 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_nothing"))
248 << message_;
249}
[email protected]ffc7b4d2012-06-08 00:05:32250
251// Test that platform apps can use the chrome.fileSystem.getDisplayPath
252// function to get the native file system path of a file they are launched with.
253IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, GetDisplayPath) {
254 SetCommandLineArg("platform_apps/launch_files/test.txt");
255 ASSERT_TRUE(RunPlatformAppTest("platform_apps/get_display_path"))
256 << message_;
257}
258
[email protected]12e540452012-05-26 07:09:36259#endif // defined(OS_CHROMEOS)