blob: 748090eeaa1d4010c4eda7276173c880269da896 [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]605fb8102012-05-04 01:36:555#include "chrome/browser/automation/automation_util.h"
[email protected]375003a2011-12-13 02:53:216#include "chrome/browser/tab_contents/render_view_context_menu.h"
[email protected]259771102012-05-31 16:52:207#include "chrome/browser/extensions/extension_test_message_listener.h"
8#include "chrome/browser/extensions/platform_app_browsertest_util.h"
9#include "chrome/browser/extensions/shell_window_registry.h"
[email protected]cfc6cca2011-12-01 02:30:0610#include "chrome/browser/ui/browser.h"
[email protected]d72d3a62012-05-10 03:45:0811#include "chrome/browser/ui/extensions/shell_window.h"
[email protected]cfc6cca2011-12-01 02:30:0612#include "chrome/test/base/ui_test_utils.h"
[email protected]cfc6cca2011-12-01 02:30:0613
[email protected]bb81f382012-01-03 22:45:4414using content::WebContents;
[email protected]31bdbfef2012-05-22 19:59:1515using extensions::Extension;
16
[email protected]375003a2011-12-13 02:53:2117namespace {
18// Non-abstract RenderViewContextMenu class.
19class PlatformAppContextMenu : public RenderViewContextMenu {
20 public:
[email protected]bb81f382012-01-03 22:45:4421 PlatformAppContextMenu(WebContents* web_contents,
[email protected]35be7ec2012-02-12 20:42:5122 const content::ContextMenuParams& params)
[email protected]bb81f382012-01-03 22:45:4423 : RenderViewContextMenu(web_contents, params) {}
[email protected]375003a2011-12-13 02:53:2124
25 protected:
26 // These two functions implement pure virtual methods of
27 // RenderViewContextMenu.
28 virtual bool GetAcceleratorForCommandId(int command_id,
29 ui::Accelerator* accelerator) {
30 return false;
31 }
32 virtual void PlatformInit() {}
[email protected]f5d5cd02012-05-04 13:59:3033 virtual void PlatformCancel() {}
[email protected]375003a2011-12-13 02:53:2134};
35
36} // namespace
37
[email protected]dc37b002012-04-23 23:02:2638// Tests that platform apps received the "launch" event when launched.
39IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) {
40 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_;
[email protected]cfc6cca2011-12-01 02:30:0641}
[email protected]375003a2011-12-13 02:53:2142
[email protected]edd7ed692012-02-08 02:50:0343IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
[email protected]dc37b002012-04-23 23:02:2644 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]31bdbfef2012-05-22 19:59:1545 LoadAndLaunchPlatformApp("minimal");
[email protected]dc37b002012-04-23 23:02:2646
47 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:2148
[email protected]375003a2011-12-13 02:53:2149 // The empty app doesn't add any context menu items, so its menu should
[email protected]0cfacd82012-02-09 01:55:3350 // only include the developer tools.
[email protected]d72d3a62012-05-10 03:45:0851 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:3152 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:2153 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:5154 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:4455 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:2156 params);
57 menu->Init();
[email protected]d72d3a62012-05-10 03:45:0858 // TODO(benwells): Remove the constant below. Instead of checking the
59 // number of menu items check certain item's absense and presence.
[email protected]40b13002012-03-06 02:23:1560 // 3 including separator
61 ASSERT_EQ(3, menu->menu_model().GetItemCount());
[email protected]375003a2011-12-13 02:53:2162}
63
[email protected]edd7ed692012-02-08 02:50:0364IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) {
[email protected]dc37b002012-04-23 23:02:2665 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]375003a2011-12-13 02:53:2166 LoadAndLaunchPlatformApp("context_menu");
67
[email protected]dc37b002012-04-23 23:02:2668 // Wait for the extension to tell us it's initialized its context menus and
69 // launched a window.
70 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:2171
[email protected]0cfacd82012-02-09 01:55:3372 // The context_menu app has one context menu item. This, along with a
73 // separator and the developer tools, is all that should be in the menu.
[email protected]d72d3a62012-05-10 03:45:0874 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:3175 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:2176 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:5177 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:4478 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:2179 params);
80 menu->Init();
[email protected]d72d3a62012-05-10 03:45:0881 // TODO(benwells): Remove the constant below. Instead of checking the
82 // number of menu items check certain item's absense and presence.
[email protected]40b13002012-03-06 02:23:1583 ASSERT_EQ(4, menu->menu_model().GetItemCount());
[email protected]375003a2011-12-13 02:53:2184}
[email protected]5f000f272012-01-19 05:25:0885
[email protected]edd7ed692012-02-08 02:50:0386IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) {
[email protected]dc37b002012-04-23 23:02:2687 ASSERT_TRUE(StartTestServer());
88 ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_;
[email protected]5f000f272012-01-19 05:25:0889}
[email protected]863e6472012-01-24 19:33:5890
[email protected]fd3238af2012-05-22 18:55:3091IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Iframes) {
92 ASSERT_TRUE(StartTestServer());
93 ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_;
94}
95
[email protected]2aac7ff2012-01-25 18:05:1196// Tests that localStorage and WebSQL are disabled for platform apps.
[email protected]edd7ed692012-02-08 02:50:0397IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) {
[email protected]2aac7ff2012-01-25 18:05:1198 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_;
99}
[email protected]6a5a2e52012-03-22 03:21:12100
[email protected]c0cecd1f2012-04-05 16:50:12101IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) {
102 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_;
103}
104
[email protected]6a5a2e52012-03-22 03:21:12105// Tests that platform apps can use the chrome.windows.* API.
[email protected]dc37b002012-04-23 23:02:26106IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) {
[email protected]6a5a2e52012-03-22 03:21:12107 ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_;
108}
[email protected]605fb8102012-05-04 01:36:55109
110// Tests that platform apps have isolated storage by default.
111IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Isolation) {
112 ASSERT_TRUE(StartTestServer());
113
114 // Load a (non-app) page under the "localhost" origin that sets a cookie.
115 GURL set_cookie_url = test_server()->GetURL(
116 "files/extensions/platform_apps/isolation/set_cookie.html");
117 GURL::Replacements replace_host;
118 std::string host_str("localhost"); // Must stay in scope with replace_host.
119 replace_host.SetHostStr(host_str);
120 set_cookie_url = set_cookie_url.ReplaceComponents(replace_host);
121
122 ui_test_utils::NavigateToURLWithDisposition(
123 browser(), set_cookie_url,
124 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
125
126 // Make sure the cookie is set.
127 int cookie_size;
128 std::string cookie_value;
129 automation_util::GetCookies(
130 set_cookie_url,
131 browser()->GetWebContentsAt(0),
132 &cookie_size,
133 &cookie_value);
134 ASSERT_EQ("testCookie=1", cookie_value);
135
136 // Let the platform app request the same URL, and make sure that it doesn't
137 // see the cookie.
138 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_;
139}
[email protected]31bdbfef2012-05-22 19:59:15140
141IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ExtensionWindowingApis) {
142 // Initially there should be just the one browser window visible to the
143 // extensions API.
144 const Extension* extension = LoadExtension(
145 test_data_dir_.AppendASCII("common/background_page"));
146 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
147
148 // And no shell windows.
149 ASSERT_EQ(0U, GetShellWindowCount());
150
151 // Launch a platform app that shows a window.
152 ExtensionTestMessageListener launched_listener("Launched", false);
153 const Extension* platform_app = LoadAndLaunchPlatformApp("minimal");
154 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
155 ASSERT_EQ(1U, GetShellWindowCount());
156 ShellWindowRegistry::ShellWindowSet shell_windows =
157 ShellWindowRegistry::Get(browser()->profile())->shell_windows();
158 int shell_window_id = (*shell_windows.begin())->session_id().id();
159
160 // But it's not visible to the extensions API, it still thinks there's just
161 // one browser window.
162 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
163 // It can't look it up by ID either
164 ASSERT_FALSE(RunGetWindowFunctionForExtension(shell_window_id, extension));
165
166 // The app can also only see one window (its own).
167 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(platform_app));
168 ASSERT_TRUE(RunGetWindowFunctionForExtension(shell_window_id, platform_app));
169
170 // Launch another platform app that also shows a window.
171 ExtensionTestMessageListener launched_listener2("Launched", false);
172 const Extension* platform_app2 = LoadAndLaunchPlatformApp("context_menu");
173 ASSERT_TRUE(launched_listener2.WaitUntilSatisfied());
174
175 // There are two total shell windows, but each app can only see its own.
176 ASSERT_EQ(2U, GetShellWindowCount());
177 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(platform_app));
178 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(platform_app2));
179}
[email protected]12e540452012-05-26 07:09:36180
181// TODO(benwells): fix these tests for ChromeOS.
182#if !defined(OS_CHROMEOS)
183// Tests that command line parameters get passed through to platform apps
184// via launchData correctly when launching with a file.
185IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFile) {
186 SetCommandLineArg( "platform_apps/launch_files/test.txt");
187 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file"))
188 << message_;
189}
190
191// Tests that no launch data is sent through if the platform app provides
192// an intent with the wrong action.
193IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongIntent) {
194 SetCommandLineArg("platform_apps/launch_files/test.txt");
195 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_intent"))
196 << message_;
197}
198
199// Tests that no launch data is sent through if the file is of the wrong MIME
200// type.
201IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongType) {
202 SetCommandLineArg("platform_apps/launch_files/test.txt");
203 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_type"))
204 << message_;
205}
206
207// Tests that no launch data is sent through if the platform app does not
208// provide an intent.
209IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNoIntent) {
210 SetCommandLineArg("platform_apps/launch_files/test.txt");
211 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_no_intent"))
212 << message_;
213}
214
215// Tests that no launch data is sent through if the file MIME type cannot
216// be read.
217IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoType) {
218 SetCommandLineArg("platform_apps/launch_files/test.unknownextension");
219 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
220 << message_;
221}
222
223// Tests that no launch data is sent through if the file does not exist.
224IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoFile) {
225 SetCommandLineArg("platform_apps/launch_files/doesnotexist.txt");
226 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
227 << message_;
228}
229
230// Tests that no launch data is sent through if the argument is a directory.
231IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithDirectory) {
232 SetCommandLineArg("platform_apps/launch_files");
233 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid"))
234 << message_;
235}
236
237// Tests that no launch data is sent through if there are no arguments passed
238// on the command line
239IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNothing) {
240 ClearCommandLineArgs();
241 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_nothing"))
242 << message_;
243}
244#endif // defined(OS_CHROMEOS)