[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] | ad687a7 | 2012-06-08 06:03:11 | [diff] [blame] | 5 | #include "chrome/app/chrome_command_ids.h" |
[email protected] | 605fb810 | 2012-05-04 01:36:55 | [diff] [blame] | 6 | #include "chrome/browser/automation/automation_util.h" |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 7 | #include "chrome/browser/tab_contents/render_view_context_menu.h" |
[email protected] | 25977110 | 2012-05-31 16:52:20 | [diff] [blame] | 8 | #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] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 11 | #include "chrome/browser/ui/browser.h" |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame^] | 12 | #include "chrome/browser/ui/browser_tabstrip.h" |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 13 | #include "chrome/browser/ui/extensions/shell_window.h" |
[email protected] | 5b1a04b4 | 2012-06-15 00:41:44 | [diff] [blame] | 14 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 15 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 16 | |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 17 | using content::WebContents; |
[email protected] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 18 | using extensions::Extension; |
| 19 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 20 | namespace { |
| 21 | // Non-abstract RenderViewContextMenu class. |
| 22 | class PlatformAppContextMenu : public RenderViewContextMenu { |
| 23 | public: |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 24 | PlatformAppContextMenu(WebContents* web_contents, |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 25 | const content::ContextMenuParams& params) |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 26 | : RenderViewContextMenu(web_contents, params) {} |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 27 | |
[email protected] | ad687a7 | 2012-06-08 06:03:11 | [diff] [blame] | 28 | bool HasCommandWithId(int command_id) { |
| 29 | return menu_model_.GetIndexOfCommandId(command_id) != -1; |
| 30 | } |
| 31 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 32 | 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] | f5d5cd0 | 2012-05-04 13:59:30 | [diff] [blame] | 40 | virtual void PlatformCancel() {} |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | } // namespace |
| 44 | |
[email protected] | eb4bcac | 2012-06-27 01:32:08 | [diff] [blame] | 45 | // 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. |
| 48 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, CreateAndCloseShellWindow) { |
| 49 | const Extension* extension = LoadAndLaunchPlatformApp("minimal"); |
| 50 | ShellWindow* window = CreateShellWindow(extension); |
| 51 | CloseShellWindow(window); |
| 52 | } |
| 53 | |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 54 | // Tests that platform apps received the "launch" event when launched. |
| 55 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) { |
| 56 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_; |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 57 | } |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 58 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 59 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) { |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 60 | ExtensionTestMessageListener launched_listener("Launched", false); |
[email protected] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 61 | LoadAndLaunchPlatformApp("minimal"); |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 62 | |
| 63 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 64 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 65 | // 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] | 66 | // only include the developer tools. |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 67 | WebContents* web_contents = GetFirstShellWindowWebContents(); |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 68 | ASSERT_TRUE(web_contents); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 69 | WebKit::WebContextMenuData data; |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 70 | content::ContextMenuParams params(data); |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 71 | PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 72 | params); |
| 73 | menu->Init(); |
[email protected] | ad687a7 | 2012-06-08 06:03:11 | [diff] [blame] | 74 | 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] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 78 | } |
| 79 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 80 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) { |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 81 | ExtensionTestMessageListener launched_listener("Launched", false); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 82 | LoadAndLaunchPlatformApp("context_menu"); |
| 83 | |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 84 | // 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] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 87 | |
[email protected] | 0cfacd8 | 2012-02-09 01:55:33 | [diff] [blame] | 88 | // 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] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 90 | WebContents* web_contents = GetFirstShellWindowWebContents(); |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 91 | ASSERT_TRUE(web_contents); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 92 | WebKit::WebContextMenuData data; |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 93 | content::ContextMenuParams params(data); |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 94 | PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 95 | params); |
| 96 | menu->Init(); |
[email protected] | ad687a7 | 2012-06-08 06:03:11 | [diff] [blame] | 97 | 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] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 102 | } |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 103 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 104 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) { |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 105 | ASSERT_TRUE(StartTestServer()); |
| 106 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_; |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 107 | } |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 108 | |
[email protected] | fd3238af | 2012-05-22 18:55:30 | [diff] [blame] | 109 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Iframes) { |
| 110 | ASSERT_TRUE(StartTestServer()); |
| 111 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_; |
| 112 | } |
| 113 | |
[email protected] | 2aac7ff | 2012-01-25 18:05:11 | [diff] [blame] | 114 | // Tests that localStorage and WebSQL are disabled for platform apps. |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 115 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) { |
[email protected] | 2aac7ff | 2012-01-25 18:05:11 | [diff] [blame] | 116 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_; |
| 117 | } |
[email protected] | 6a5a2e5 | 2012-03-22 03:21:12 | [diff] [blame] | 118 | |
[email protected] | c0cecd1f | 2012-04-05 16:50:12 | [diff] [blame] | 119 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) { |
| 120 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_; |
| 121 | } |
| 122 | |
[email protected] | eb4bcac | 2012-06-27 01:32:08 | [diff] [blame] | 123 | // Tests that platform apps can use the chrome.appWindow.* API. |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 124 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) { |
[email protected] | 6a5a2e5 | 2012-03-22 03:21:12 | [diff] [blame] | 125 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_; |
| 126 | } |
[email protected] | 605fb810 | 2012-05-04 01:36:55 | [diff] [blame] | 127 | |
| 128 | // Tests that platform apps have isolated storage by default. |
| 129 | IN_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] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame^] | 149 | chrome::GetWebContentsAt(browser(), 0), |
[email protected] | 605fb810 | 2012-05-04 01:36:55 | [diff] [blame] | 150 | &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] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 158 | |
| 159 | IN_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] | ad5bb8a9 | 2012-06-07 03:55:58 | [diff] [blame] | 171 | LoadAndLaunchPlatformApp("minimal"); |
[email protected] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 172 | 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] | ad5bb8a9 | 2012-06-07 03:55:58 | [diff] [blame] | 185 | // 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] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 187 | |
| 188 | // Launch another platform app that also shows a window. |
| 189 | ExtensionTestMessageListener launched_listener2("Launched", false); |
[email protected] | ad5bb8a9 | 2012-06-07 03:55:58 | [diff] [blame] | 190 | LoadAndLaunchPlatformApp("context_menu"); |
[email protected] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 191 | 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] | ad5bb8a9 | 2012-06-07 03:55:58 | [diff] [blame] | 195 | // TODO(jeremya): as above, this requires more extension functions. |
[email protected] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 196 | } |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 197 | |
| 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. |
| 202 | IN_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. |
| 210 | IN_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. |
| 218 | IN_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. |
| 226 | IN_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. |
| 234 | IN_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. |
| 241 | IN_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. |
| 248 | IN_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 |
| 256 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNothing) { |
| 257 | ClearCommandLineArgs(); |
| 258 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_nothing")) |
| 259 | << message_; |
| 260 | } |
[email protected] | ffc7b4d | 2012-06-08 00:05:32 | [diff] [blame] | 261 | |
| 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. |
| 264 | IN_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] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 270 | #endif // defined(OS_CHROMEOS) |
[email protected] | 5b1a04b4 | 2012-06-15 00:41:44 | [diff] [blame] | 271 | |
| 272 | IN_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 | } |