blob: 3faa89ff05c0ddffc5bbbd31211c8d435b9ac280 [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]5b1a04b42012-06-15 00:41:4413#include "chrome/common/chrome_notification_types.h"
[email protected]cfc6cca2011-12-01 02:30:0614#include "chrome/test/base/ui_test_utils.h"
[email protected]cfc6cca2011-12-01 02:30:0615
[email protected]bb81f382012-01-03 22:45:4416using content::WebContents;
[email protected]31bdbfef2012-05-22 19:59:1517using extensions::Extension;
18
[email protected]375003a2011-12-13 02:53:2119namespace {
20// Non-abstract RenderViewContextMenu class.
21class PlatformAppContextMenu : public RenderViewContextMenu {
22 public:
[email protected]bb81f382012-01-03 22:45:4423 PlatformAppContextMenu(WebContents* web_contents,
[email protected]35be7ec2012-02-12 20:42:5124 const content::ContextMenuParams& params)
[email protected]bb81f382012-01-03 22:45:4425 : RenderViewContextMenu(web_contents, params) {}
[email protected]375003a2011-12-13 02:53:2126
[email protected]ad687a72012-06-08 06:03:1127 bool HasCommandWithId(int command_id) {
28 return menu_model_.GetIndexOfCommandId(command_id) != -1;
29 }
30
[email protected]375003a2011-12-13 02:53:2131 protected:
32 // These two functions implement pure virtual methods of
33 // RenderViewContextMenu.
34 virtual bool GetAcceleratorForCommandId(int command_id,
35 ui::Accelerator* accelerator) {
36 return false;
37 }
38 virtual void PlatformInit() {}
[email protected]f5d5cd02012-05-04 13:59:3039 virtual void PlatformCancel() {}
[email protected]375003a2011-12-13 02:53:2140};
41
42} // namespace
43
[email protected]eb4bcac2012-06-27 01:32:0844// Tests that CreateShellWindow doesn't crash if you close it straight away.
45// LauncherPlatformAppBrowserTest relies on this behaviour, but is only run for
46// ash, so we test that it works here.
47IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, CreateAndCloseShellWindow) {
48 const Extension* extension = LoadAndLaunchPlatformApp("minimal");
49 ShellWindow* window = CreateShellWindow(extension);
50 CloseShellWindow(window);
51}
52
[email protected]dc37b002012-04-23 23:02:2653// Tests that platform apps received the "launch" event when launched.
54IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) {
55 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_;
[email protected]cfc6cca2011-12-01 02:30:0656}
[email protected]375003a2011-12-13 02:53:2157
[email protected]edd7ed692012-02-08 02:50:0358IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
[email protected]dc37b002012-04-23 23:02:2659 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]31bdbfef2012-05-22 19:59:1560 LoadAndLaunchPlatformApp("minimal");
[email protected]dc37b002012-04-23 23:02:2661
62 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:2163
[email protected]375003a2011-12-13 02:53:2164 // The empty app doesn't add any context menu items, so its menu should
[email protected]0cfacd82012-02-09 01:55:3365 // only include the developer tools.
[email protected]d72d3a62012-05-10 03:45:0866 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:3167 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:2168 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:5169 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:4470 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:2171 params);
72 menu->Init();
[email protected]ad687a72012-06-08 06:03:1173 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
74 ASSERT_TRUE(menu->HasCommandWithId(IDC_RELOAD));
75 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
76 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]375003a2011-12-13 02:53:2177}
78
[email protected]edd7ed692012-02-08 02:50:0379IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) {
[email protected]dc37b002012-04-23 23:02:2680 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]375003a2011-12-13 02:53:2181 LoadAndLaunchPlatformApp("context_menu");
82
[email protected]dc37b002012-04-23 23:02:2683 // Wait for the extension to tell us it's initialized its context menus and
84 // launched a window.
85 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:2186
[email protected]0cfacd82012-02-09 01:55:3387 // The context_menu app has one context menu item. This, along with a
88 // separator and the developer tools, is all that should be in the menu.
[email protected]d72d3a62012-05-10 03:45:0889 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:3190 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:2191 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:5192 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:4493 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:2194 params);
95 menu->Init();
[email protected]ad687a72012-06-08 06:03:1196 ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST));
97 ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
98 ASSERT_TRUE(menu->HasCommandWithId(IDC_RELOAD));
99 ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK));
100 ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE));
[email protected]375003a2011-12-13 02:53:21101}
[email protected]5f000f272012-01-19 05:25:08102
[email protected]edd7ed692012-02-08 02:50:03103IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) {
[email protected]dc37b002012-04-23 23:02:26104 ASSERT_TRUE(StartTestServer());
105 ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_;
[email protected]5f000f272012-01-19 05:25:08106}
[email protected]863e6472012-01-24 19:33:58107
[email protected]fd3238af2012-05-22 18:55:30108IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Iframes) {
109 ASSERT_TRUE(StartTestServer());
110 ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_;
111}
112
[email protected]2aac7ff2012-01-25 18:05:11113// Tests that localStorage and WebSQL are disabled for platform apps.
[email protected]edd7ed692012-02-08 02:50:03114IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) {
[email protected]2aac7ff2012-01-25 18:05:11115 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_;
116}
[email protected]6a5a2e52012-03-22 03:21:12117
[email protected]c0cecd1f2012-04-05 16:50:12118IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) {
119 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_;
120}
121
[email protected]eb4bcac2012-06-27 01:32:08122// Tests that platform apps can use the chrome.appWindow.* API.
[email protected]dc37b002012-04-23 23:02:26123IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) {
[email protected]6a5a2e52012-03-22 03:21:12124 ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_;
125}
[email protected]605fb8102012-05-04 01:36:55126
127// Tests that platform apps have isolated storage by default.
128IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Isolation) {
129 ASSERT_TRUE(StartTestServer());
130
131 // Load a (non-app) page under the "localhost" origin that sets a cookie.
132 GURL set_cookie_url = test_server()->GetURL(
133 "files/extensions/platform_apps/isolation/set_cookie.html");
134 GURL::Replacements replace_host;
135 std::string host_str("localhost"); // Must stay in scope with replace_host.
136 replace_host.SetHostStr(host_str);
137 set_cookie_url = set_cookie_url.ReplaceComponents(replace_host);
138
139 ui_test_utils::NavigateToURLWithDisposition(
140 browser(), set_cookie_url,
141 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
142
143 // Make sure the cookie is set.
144 int cookie_size;
145 std::string cookie_value;
146 automation_util::GetCookies(
147 set_cookie_url,
148 browser()->GetWebContentsAt(0),
149 &cookie_size,
150 &cookie_value);
151 ASSERT_EQ("testCookie=1", cookie_value);
152
153 // Let the platform app request the same URL, and make sure that it doesn't
154 // see the cookie.
155 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_;
156}
[email protected]31bdbfef2012-05-22 19:59:15157
158IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ExtensionWindowingApis) {
159 // Initially there should be just the one browser window visible to the
160 // extensions API.
161 const Extension* extension = LoadExtension(
162 test_data_dir_.AppendASCII("common/background_page"));
163 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
164
165 // And no shell windows.
166 ASSERT_EQ(0U, GetShellWindowCount());
167
168 // Launch a platform app that shows a window.
169 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]ad5bb8a92012-06-07 03:55:58170 LoadAndLaunchPlatformApp("minimal");
[email protected]31bdbfef2012-05-22 19:59:15171 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
172 ASSERT_EQ(1U, GetShellWindowCount());
173 ShellWindowRegistry::ShellWindowSet shell_windows =
174 ShellWindowRegistry::Get(browser()->profile())->shell_windows();
175 int shell_window_id = (*shell_windows.begin())->session_id().id();
176
177 // But it's not visible to the extensions API, it still thinks there's just
178 // one browser window.
179 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
180 // It can't look it up by ID either
181 ASSERT_FALSE(RunGetWindowFunctionForExtension(shell_window_id, extension));
182
183 // The app can also only see one window (its own).
[email protected]ad5bb8a92012-06-07 03:55:58184 // TODO(jeremya): add an extension function to get a shell window by ID, and
185 // to get a list of all the shell windows, so we can test this.
[email protected]31bdbfef2012-05-22 19:59:15186
187 // Launch another platform app that also shows a window.
188 ExtensionTestMessageListener launched_listener2("Launched", false);
[email protected]ad5bb8a92012-06-07 03:55:58189 LoadAndLaunchPlatformApp("context_menu");
[email protected]31bdbfef2012-05-22 19:59:15190 ASSERT_TRUE(launched_listener2.WaitUntilSatisfied());
191
192 // There are two total shell windows, but each app can only see its own.
193 ASSERT_EQ(2U, GetShellWindowCount());
[email protected]ad5bb8a92012-06-07 03:55:58194 // TODO(jeremya): as above, this requires more extension functions.
[email protected]31bdbfef2012-05-22 19:59:15195}
[email protected]12e540452012-05-26 07:09:36196
197// TODO(benwells): fix these tests for ChromeOS.
198#if !defined(OS_CHROMEOS)
199// Tests that command line parameters get passed through to platform apps
200// via launchData correctly when launching with a file.
201IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFile) {
202 SetCommandLineArg( "platform_apps/launch_files/test.txt");
203 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file"))
204 << message_;
205}
206
207// Tests that no launch data is sent through if the platform app provides
208// an intent with the wrong action.
209IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongIntent) {
210 SetCommandLineArg("platform_apps/launch_files/test.txt");
211 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_intent"))
212 << message_;
213}
214
215// Tests that no launch data is sent through if the file is of the wrong MIME
216// type.
217IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongType) {
218 SetCommandLineArg("platform_apps/launch_files/test.txt");
219 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_type"))
220 << message_;
221}
222
223// Tests that no launch data is sent through if the platform app does not
224// provide an intent.
225IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNoIntent) {
226 SetCommandLineArg("platform_apps/launch_files/test.txt");
227 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_no_intent"))
228 << message_;
229}
230
231// Tests that no launch data is sent through if the file MIME type cannot
232// be read.
233IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoType) {
234 SetCommandLineArg("platform_apps/launch_files/test.unknownextension");
235 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
236 << message_;
237}
238
239// Tests that no launch data is sent through if the file does not exist.
240IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoFile) {
241 SetCommandLineArg("platform_apps/launch_files/doesnotexist.txt");
242 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
243 << message_;
244}
245
246// Tests that no launch data is sent through if the argument is a directory.
247IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithDirectory) {
248 SetCommandLineArg("platform_apps/launch_files");
249 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
250 << message_;
251}
252
253// Tests that no launch data is sent through if there are no arguments passed
254// on the command line
255IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNothing) {
256 ClearCommandLineArgs();
257 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_nothing"))
258 << message_;
259}
[email protected]ffc7b4d2012-06-08 00:05:32260
261// Test that platform apps can use the chrome.fileSystem.getDisplayPath
262// function to get the native file system path of a file they are launched with.
263IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, GetDisplayPath) {
264 SetCommandLineArg("platform_apps/launch_files/test.txt");
265 ASSERT_TRUE(RunPlatformAppTest("platform_apps/get_display_path"))
266 << message_;
267}
268
[email protected]12e540452012-05-26 07:09:36269#endif // defined(OS_CHROMEOS)
[email protected]5b1a04b42012-06-15 00:41:44270
271IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OpenLink) {
272 ASSERT_TRUE(StartTestServer());
273 ui_test_utils::WindowedNotificationObserver observer(
274 chrome::NOTIFICATION_TAB_ADDED,
275 content::Source<content::WebContentsDelegate>(browser()));
276 LoadAndLaunchPlatformApp("open_link");
277 observer.Wait();
278 ASSERT_EQ(2, browser()->tab_count());
279}