[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 605fb810 | 2012-05-04 01:36:55 | [diff] [blame] | 5 | #include "chrome/browser/automation/automation_util.h" |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 6 | #include "chrome/browser/tab_contents/render_view_context_menu.h" |
[email protected] | 25977110 | 2012-05-31 16:52:20 | [diff] [blame^] | 7 | #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] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 10 | #include "chrome/browser/ui/browser.h" |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 11 | #include "chrome/browser/ui/extensions/shell_window.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 12 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 13 | |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 14 | using content::WebContents; |
[email protected] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 15 | using extensions::Extension; |
| 16 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 17 | namespace { |
| 18 | // Non-abstract RenderViewContextMenu class. |
| 19 | class PlatformAppContextMenu : public RenderViewContextMenu { |
| 20 | public: |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 21 | PlatformAppContextMenu(WebContents* web_contents, |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 22 | const content::ContextMenuParams& params) |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 23 | : RenderViewContextMenu(web_contents, params) {} |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 24 | |
| 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] | f5d5cd0 | 2012-05-04 13:59:30 | [diff] [blame] | 33 | virtual void PlatformCancel() {} |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | } // namespace |
| 37 | |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 38 | // Tests that platform apps received the "launch" event when launched. |
| 39 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) { |
| 40 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_; |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 41 | } |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 42 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 43 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) { |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 44 | ExtensionTestMessageListener launched_listener("Launched", false); |
[email protected] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 45 | LoadAndLaunchPlatformApp("minimal"); |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 46 | |
| 47 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 48 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 49 | // The empty app doesn't add any context menu items, so its menu should |
[email protected] | 0cfacd8 | 2012-02-09 01:55:33 | [diff] [blame] | 50 | // only include the developer tools. |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 51 | WebContents* web_contents = GetFirstShellWindowWebContents(); |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 52 | ASSERT_TRUE(web_contents); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 53 | WebKit::WebContextMenuData data; |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 54 | content::ContextMenuParams params(data); |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 55 | PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 56 | params); |
| 57 | menu->Init(); |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 58 | // TODO(benwells): Remove the constant below. Instead of checking the |
| 59 | // number of menu items check certain item's absense and presence. |
[email protected] | 40b1300 | 2012-03-06 02:23:15 | [diff] [blame] | 60 | // 3 including separator |
| 61 | ASSERT_EQ(3, menu->menu_model().GetItemCount()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 64 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) { |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 65 | ExtensionTestMessageListener launched_listener("Launched", false); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 66 | LoadAndLaunchPlatformApp("context_menu"); |
| 67 | |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 68 | // 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] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 71 | |
[email protected] | 0cfacd8 | 2012-02-09 01:55:33 | [diff] [blame] | 72 | // 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] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 74 | WebContents* web_contents = GetFirstShellWindowWebContents(); |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 75 | ASSERT_TRUE(web_contents); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 76 | WebKit::WebContextMenuData data; |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 77 | content::ContextMenuParams params(data); |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 78 | PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 79 | params); |
| 80 | menu->Init(); |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 81 | // TODO(benwells): Remove the constant below. Instead of checking the |
| 82 | // number of menu items check certain item's absense and presence. |
[email protected] | 40b1300 | 2012-03-06 02:23:15 | [diff] [blame] | 83 | ASSERT_EQ(4, menu->menu_model().GetItemCount()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 84 | } |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 85 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 86 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) { |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 87 | ASSERT_TRUE(StartTestServer()); |
| 88 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_; |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 89 | } |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 90 | |
[email protected] | fd3238af | 2012-05-22 18:55:30 | [diff] [blame] | 91 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Iframes) { |
| 92 | ASSERT_TRUE(StartTestServer()); |
| 93 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_; |
| 94 | } |
| 95 | |
[email protected] | 2aac7ff | 2012-01-25 18:05:11 | [diff] [blame] | 96 | // Tests that localStorage and WebSQL are disabled for platform apps. |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 97 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) { |
[email protected] | 2aac7ff | 2012-01-25 18:05:11 | [diff] [blame] | 98 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_; |
| 99 | } |
[email protected] | 6a5a2e5 | 2012-03-22 03:21:12 | [diff] [blame] | 100 | |
[email protected] | c0cecd1f | 2012-04-05 16:50:12 | [diff] [blame] | 101 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) { |
| 102 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_; |
| 103 | } |
| 104 | |
[email protected] | 6a5a2e5 | 2012-03-22 03:21:12 | [diff] [blame] | 105 | // Tests that platform apps can use the chrome.windows.* API. |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 106 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) { |
[email protected] | 6a5a2e5 | 2012-03-22 03:21:12 | [diff] [blame] | 107 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_; |
| 108 | } |
[email protected] | 605fb810 | 2012-05-04 01:36:55 | [diff] [blame] | 109 | |
| 110 | // Tests that platform apps have isolated storage by default. |
| 111 | IN_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] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 140 | |
| 141 | IN_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] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 180 | |
| 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. |
| 185 | IN_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. |
| 193 | IN_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. |
| 201 | IN_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. |
| 209 | IN_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. |
| 217 | IN_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. |
| 224 | IN_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. |
| 231 | IN_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 |
| 239 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNothing) { |
| 240 | ClearCommandLineArgs(); |
| 241 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_nothing")) |
| 242 | << message_; |
| 243 | } |
| 244 | #endif // defined(OS_CHROMEOS) |