[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] | a6db612 | 2012-09-03 06:00:23 | [diff] [blame] | 5 | #include "base/bind.h" |
[email protected] | 939532d0 | 2012-08-31 23:37:33 | [diff] [blame] | 6 | #include "base/test/test_timeouts.h" |
| 7 | #include "base/threading/platform_thread.h" |
[email protected] | b3c019b | 2012-08-28 02:43:48 | [diff] [blame] | 8 | #include "base/utf_string_conversions.h" |
[email protected] | ad687a7 | 2012-06-08 06:03:11 | [diff] [blame] | 9 | #include "chrome/app/chrome_command_ids.h" |
[email protected] | 605fb810 | 2012-05-04 01:36:55 | [diff] [blame] | 10 | #include "chrome/browser/automation/automation_util.h" |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 11 | #include "chrome/browser/debugger/devtools_window.h" |
[email protected] | ddf4216 | 2012-11-06 21:14:55 | [diff] [blame] | 12 | #include "chrome/browser/extensions/api/permissions/permissions_api.h" |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 13 | #include "chrome/browser/extensions/app_restore_service_factory.h" |
| 14 | #include "chrome/browser/extensions/app_restore_service.h" |
[email protected] | a6db612 | 2012-09-03 06:00:23 | [diff] [blame] | 15 | #include "chrome/browser/extensions/extension_browsertest.h" |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 16 | #include "chrome/browser/extensions/extension_prefs.h" |
| 17 | #include "chrome/browser/extensions/extension_service.h" |
| 18 | #include "chrome/browser/extensions/extension_system.h" |
[email protected] | 25977110 | 2012-05-31 16:52:20 | [diff] [blame] | 19 | #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 20 | #include "chrome/browser/extensions/platform_app_browsertest_util.h" |
[email protected] | a6db612 | 2012-09-03 06:00:23 | [diff] [blame] | 21 | #include "chrome/browser/extensions/platform_app_launcher.h" |
[email protected] | 25977110 | 2012-05-31 16:52:20 | [diff] [blame] | 22 | #include "chrome/browser/extensions/shell_window_registry.h" |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 23 | #include "chrome/browser/tab_contents/render_view_context_menu.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 24 | #include "chrome/browser/ui/browser.h" |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 25 | #include "chrome/browser/ui/browser_tabstrip.h" |
[email protected] | ddf4216 | 2012-11-06 21:14:55 | [diff] [blame] | 26 | #include "chrome/browser/ui/constrained_window_tab_helper.h" |
[email protected] | a5a0be0 | 2012-07-18 05:51:54 | [diff] [blame] | 27 | #include "chrome/browser/ui/extensions/application_launch.h" |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 28 | #include "chrome/browser/ui/extensions/shell_window.h" |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 29 | #include "chrome/browser/ui/tab_contents/tab_contents.h" |
[email protected] | 5b1a04b4 | 2012-06-15 00:41:44 | [diff] [blame] | 30 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | 4d007b31 | 2012-10-17 03:00:48 | [diff] [blame] | 31 | #include "chrome/common/url_constants.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 32 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 33 | #include "content/public/browser/devtools_agent_host_registry.h" |
[email protected] | fb29e6cf | 2012-07-12 21:27:20 | [diff] [blame] | 34 | #include "content/public/browser/render_process_host.h" |
[email protected] | a6db612 | 2012-09-03 06:00:23 | [diff] [blame] | 35 | #include "content/public/browser/web_intents_dispatcher.h" |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 36 | #include "content/public/test/test_utils.h" |
[email protected] | a6db612 | 2012-09-03 06:00:23 | [diff] [blame] | 37 | #include "googleurl/src/gurl.h" |
| 38 | #include "webkit/glue/web_intent_data.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 39 | |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 40 | using content::WebContents; |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 41 | |
| 42 | namespace extensions { |
[email protected] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 43 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 44 | namespace { |
[email protected] | 85d3d5e | 2012-08-31 21:19:17 | [diff] [blame] | 45 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 46 | // Non-abstract RenderViewContextMenu class. |
| 47 | class PlatformAppContextMenu : public RenderViewContextMenu { |
| 48 | public: |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 49 | PlatformAppContextMenu(WebContents* web_contents, |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 50 | const content::ContextMenuParams& params) |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 51 | : RenderViewContextMenu(web_contents, params) {} |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 52 | |
[email protected] | ad687a7 | 2012-06-08 06:03:11 | [diff] [blame] | 53 | bool HasCommandWithId(int command_id) { |
| 54 | return menu_model_.GetIndexOfCommandId(command_id) != -1; |
| 55 | } |
| 56 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 57 | protected: |
[email protected] | 85d3d5e | 2012-08-31 21:19:17 | [diff] [blame] | 58 | // RenderViewContextMenu implementation. |
| 59 | virtual bool GetAcceleratorForCommandId( |
| 60 | int command_id, |
| 61 | ui::Accelerator* accelerator) OVERRIDE { |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 62 | return false; |
| 63 | } |
[email protected] | 85d3d5e | 2012-08-31 21:19:17 | [diff] [blame] | 64 | virtual void PlatformInit() OVERRIDE {} |
| 65 | virtual void PlatformCancel() OVERRIDE {} |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 66 | }; |
| 67 | |
[email protected] | a6db612 | 2012-09-03 06:00:23 | [diff] [blame] | 68 | // State holder for the LaunchReply test. This provides an WebIntentsDispatcher |
| 69 | // that will, when used to launch a Web Intent, will return its reply via this |
| 70 | // class. The result may then be waited on via WaitUntilReply(). |
| 71 | class LaunchReplyHandler { |
| 72 | public: |
| 73 | explicit LaunchReplyHandler(webkit_glue::WebIntentData& data) |
| 74 | : data_(data), |
| 75 | replied_(false), |
| 76 | weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 77 | intents_dispatcher_ = content::WebIntentsDispatcher::Create(data); |
| 78 | intents_dispatcher_->RegisterReplyNotification(base::Bind( |
| 79 | &LaunchReplyHandler::OnReply, weak_ptr_factory_.GetWeakPtr())); |
| 80 | } |
| 81 | |
| 82 | content::WebIntentsDispatcher* intents_dispatcher() { |
| 83 | return intents_dispatcher_; |
| 84 | } |
| 85 | |
| 86 | // Waits until a reply to this Web Intent is provided via the |
| 87 | // WebIntentsDispatcher. |
| 88 | bool WaitUntilReply() { |
| 89 | if (replied_) |
| 90 | return true; |
| 91 | waiting_ = true; |
| 92 | content::RunMessageLoop(); |
| 93 | waiting_ = false; |
| 94 | return replied_; |
| 95 | } |
| 96 | |
| 97 | private: |
| 98 | void OnReply(webkit_glue::WebIntentReplyType reply) { |
| 99 | // Note that the ReplyNotification registered on WebIntentsDispatcher does |
| 100 | // not include the result data: this is reserved for the source page (which |
| 101 | // we don't care about). |
| 102 | replied_ = true; |
| 103 | if (waiting_) |
| 104 | MessageLoopForUI::current()->Quit(); |
| 105 | } |
| 106 | |
| 107 | webkit_glue::WebIntentData data_; |
| 108 | bool replied_; |
| 109 | bool waiting_; |
| 110 | content::WebIntentsDispatcher* intents_dispatcher_; |
| 111 | base::WeakPtrFactory<LaunchReplyHandler> weak_ptr_factory_; |
| 112 | }; |
| 113 | |
[email protected] | 4d007b31 | 2012-10-17 03:00:48 | [diff] [blame] | 114 | // This class keeps track of tabs as they are added to the browser. It will be |
| 115 | // "done" (i.e. won't block on Wait()) once |observations| tabs have been added. |
| 116 | class TabsAddedNotificationObserver |
| 117 | : public content::WindowedNotificationObserver { |
| 118 | public: |
| 119 | explicit TabsAddedNotificationObserver(size_t observations) |
| 120 | : content::WindowedNotificationObserver( |
| 121 | chrome::NOTIFICATION_TAB_ADDED, |
| 122 | content::NotificationService::AllSources()), |
| 123 | observations_(observations) { |
| 124 | } |
| 125 | |
| 126 | virtual void Observe(int type, |
| 127 | const content::NotificationSource& source, |
| 128 | const content::NotificationDetails& details) OVERRIDE { |
| 129 | observed_tabs_.push_back( |
| 130 | content::Details<WebContents>(details).ptr()); |
| 131 | if (observed_tabs_.size() == observations_) |
| 132 | content::WindowedNotificationObserver::Observe(type, source, details); |
| 133 | } |
| 134 | |
| 135 | const std::vector<content::WebContents*>& tabs() { return observed_tabs_; } |
| 136 | |
| 137 | private: |
| 138 | size_t observations_; |
| 139 | std::vector<content::WebContents*> observed_tabs_; |
| 140 | |
| 141 | DISALLOW_COPY_AND_ASSIGN(TabsAddedNotificationObserver); |
| 142 | }; |
| 143 | |
[email protected] | 85d3d5e | 2012-08-31 21:19:17 | [diff] [blame] | 144 | const char kTestFilePath[] = "platform_apps/launch_files/test.txt"; |
| 145 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 146 | } // namespace |
| 147 | |
[email protected] | eb4bcac | 2012-06-27 01:32:08 | [diff] [blame] | 148 | // Tests that CreateShellWindow doesn't crash if you close it straight away. |
| 149 | // LauncherPlatformAppBrowserTest relies on this behaviour, but is only run for |
| 150 | // ash, so we test that it works here. |
| 151 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, CreateAndCloseShellWindow) { |
| 152 | const Extension* extension = LoadAndLaunchPlatformApp("minimal"); |
| 153 | ShellWindow* window = CreateShellWindow(extension); |
| 154 | CloseShellWindow(window); |
| 155 | } |
| 156 | |
[email protected] | 688a173 | 2012-10-18 20:19:31 | [diff] [blame] | 157 | // Tests that platform apps can be launched in incognito mode without crashing. |
[email protected] | 50a04d3 | 2012-11-05 11:44:22 | [diff] [blame] | 158 | // Times out on ChromeOS: http://crbug.com/159392 |
| 159 | #if defined(OS_CHROMEOS) |
| 160 | #define MAYBE_LaunchAppIncognito DISABLED_LaunchAppIncognito |
| 161 | #else |
| 162 | #define MAYBE_LaunchAppIncognito LaunchAppIncognito |
| 163 | #endif |
| 164 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_LaunchAppIncognito) { |
[email protected] | 688a173 | 2012-10-18 20:19:31 | [diff] [blame] | 165 | Browser* browser_incognito = ui_test_utils::OpenURLOffTheRecord( |
| 166 | browser()->profile(), GURL("about:blank")); |
| 167 | |
| 168 | ExtensionTestMessageListener launched_listener("Launched", false); |
| 169 | |
| 170 | const Extension* extension = LoadExtensionIncognito( |
| 171 | test_data_dir_.AppendASCII("platform_apps").AppendASCII("minimal")); |
| 172 | EXPECT_TRUE(extension); |
| 173 | |
| 174 | application_launch::OpenApplication(application_launch::LaunchParams( |
| 175 | browser_incognito->profile(), extension, extension_misc::LAUNCH_NONE, |
| 176 | NEW_WINDOW)); |
| 177 | |
| 178 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
| 179 | } |
| 180 | |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 181 | // Tests that platform apps received the "launch" event when launched. |
| 182 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) { |
| 183 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_; |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 184 | } |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 185 | |
[email protected] | a6db612 | 2012-09-03 06:00:23 | [diff] [blame] | 186 | // Tests that platform apps can reply to "launch" events that contain a Web |
| 187 | // Intent. This test does not test the mechanics of invoking a Web Intent |
| 188 | // from a source page, and short-circuits to LaunchPlatformAppWithWebIntent. |
| 189 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchReply) { |
| 190 | FilePath path = test_data_dir_.AppendASCII("platform_apps/launch_reply"); |
| 191 | const extensions::Extension* extension = LoadExtension(path); |
| 192 | ASSERT_TRUE(extension) << "Failed to load extension."; |
| 193 | |
| 194 | webkit_glue::WebIntentData data( |
| 195 | UTF8ToUTF16("http://webintents.org/view"), |
| 196 | UTF8ToUTF16("text/plain"), |
| 197 | UTF8ToUTF16("irrelevant unserialized string data")); |
| 198 | LaunchReplyHandler handler(data); |
| 199 | |
| 200 | // Navigate to a boring page: we don't care what it is, but we require some |
| 201 | // source WebContents to launch the Web Intent "from". |
| 202 | ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); |
| 203 | WebContents* web_contents = chrome::GetActiveWebContents(browser()); |
| 204 | ASSERT_TRUE(web_contents); |
| 205 | |
| 206 | extensions::LaunchPlatformAppWithWebIntent( |
| 207 | browser()->profile(), |
| 208 | extension, |
| 209 | handler.intents_dispatcher(), |
| 210 | web_contents); |
| 211 | |
| 212 | ASSERT_TRUE(handler.WaitUntilReply()); |
| 213 | } |
| 214 | |
[email protected] | 567230b | 2012-08-21 21:11:44 | [diff] [blame] | 215 | // Tests that platform apps cannot use certain disabled window properties, but |
| 216 | // can override them and then use them. |
| 217 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisabledWindowProperties) { |
| 218 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/disabled_window_properties")) |
| 219 | << message_; |
| 220 | } |
| 221 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 222 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) { |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 223 | ExtensionTestMessageListener launched_listener("Launched", false); |
[email protected] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 224 | LoadAndLaunchPlatformApp("minimal"); |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 225 | |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 226 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
| 227 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 228 | // 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] | 229 | // only include the developer tools. |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 230 | WebContents* web_contents = GetFirstShellWindowWebContents(); |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 231 | ASSERT_TRUE(web_contents); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 232 | WebKit::WebContextMenuData data; |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 233 | content::ContextMenuParams params(data); |
[email protected] | 85d3d5e | 2012-08-31 21:19:17 | [diff] [blame] | 234 | scoped_ptr<PlatformAppContextMenu> menu; |
| 235 | menu.reset(new PlatformAppContextMenu(web_contents, params)); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 236 | menu->Init(); |
[email protected] | ad687a7 | 2012-06-08 06:03:11 | [diff] [blame] | 237 | ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT)); |
[email protected] | 65992d5 | 2012-09-19 23:05:31 | [diff] [blame] | 238 | ASSERT_TRUE( |
| 239 | menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE)); |
[email protected] | 671c970 | 2012-09-27 07:51:19 | [diff] [blame] | 240 | ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP)); |
[email protected] | ad687a7 | 2012-06-08 06:03:11 | [diff] [blame] | 241 | ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK)); |
| 242 | ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE)); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 243 | } |
| 244 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 245 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) { |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 246 | ExtensionTestMessageListener launched_listener("Launched", false); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 247 | LoadAndLaunchPlatformApp("context_menu"); |
| 248 | |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 249 | // Wait for the extension to tell us it's initialized its context menus and |
| 250 | // launched a window. |
| 251 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
| 252 | |
[email protected] | 271d01c | 2012-08-27 23:48:05 | [diff] [blame] | 253 | // The context_menu app has two context menu items. These, along with a |
[email protected] | 0cfacd8 | 2012-02-09 01:55:33 | [diff] [blame] | 254 | // separator and the developer tools, is all that should be in the menu. |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 255 | WebContents* web_contents = GetFirstShellWindowWebContents(); |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 256 | ASSERT_TRUE(web_contents); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 257 | WebKit::WebContextMenuData data; |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 258 | content::ContextMenuParams params(data); |
[email protected] | 85d3d5e | 2012-08-31 21:19:17 | [diff] [blame] | 259 | scoped_ptr<PlatformAppContextMenu> menu; |
| 260 | menu.reset(new PlatformAppContextMenu(web_contents, params)); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 261 | menu->Init(); |
[email protected] | ad687a7 | 2012-06-08 06:03:11 | [diff] [blame] | 262 | ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST)); |
[email protected] | 271d01c | 2012-08-27 23:48:05 | [diff] [blame] | 263 | ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST + 1)); |
[email protected] | ad687a7 | 2012-06-08 06:03:11 | [diff] [blame] | 264 | ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT)); |
[email protected] | 65992d5 | 2012-09-19 23:05:31 | [diff] [blame] | 265 | ASSERT_TRUE( |
| 266 | menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE)); |
[email protected] | 671c970 | 2012-09-27 07:51:19 | [diff] [blame] | 267 | ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP)); |
[email protected] | ad687a7 | 2012-06-08 06:03:11 | [diff] [blame] | 268 | ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK)); |
| 269 | ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE)); |
[email protected] | b3c019b | 2012-08-28 02:43:48 | [diff] [blame] | 270 | ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO)); |
| 271 | } |
| 272 | |
[email protected] | 65992d5 | 2012-09-19 23:05:31 | [diff] [blame] | 273 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, InstalledAppWithContextMenu) { |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 274 | ExtensionTestMessageListener launched_listener("Launched", false); |
[email protected] | 65992d5 | 2012-09-19 23:05:31 | [diff] [blame] | 275 | InstallAndLaunchPlatformApp("context_menu"); |
| 276 | |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 277 | // Wait for the extension to tell us it's initialized its context menus and |
| 278 | // launched a window. |
| 279 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
| 280 | |
[email protected] | 65992d5 | 2012-09-19 23:05:31 | [diff] [blame] | 281 | // The context_menu app has two context menu items. For an installed app |
| 282 | // these are all that should be in the menu. |
| 283 | WebContents* web_contents = GetFirstShellWindowWebContents(); |
| 284 | ASSERT_TRUE(web_contents); |
| 285 | WebKit::WebContextMenuData data; |
| 286 | content::ContextMenuParams params(data); |
| 287 | scoped_ptr<PlatformAppContextMenu> menu; |
| 288 | menu.reset(new PlatformAppContextMenu(web_contents, params)); |
| 289 | menu->Init(); |
| 290 | ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST)); |
| 291 | ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST + 1)); |
| 292 | ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT)); |
| 293 | ASSERT_FALSE( |
| 294 | menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE)); |
[email protected] | 671c970 | 2012-09-27 07:51:19 | [diff] [blame] | 295 | ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP)); |
[email protected] | 65992d5 | 2012-09-19 23:05:31 | [diff] [blame] | 296 | ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK)); |
| 297 | ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE)); |
| 298 | ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO)); |
| 299 | } |
| 300 | |
[email protected] | b3c019b | 2012-08-28 02:43:48 | [diff] [blame] | 301 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuTextField) { |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 302 | ExtensionTestMessageListener launched_listener("Launched", false); |
[email protected] | b3c019b | 2012-08-28 02:43:48 | [diff] [blame] | 303 | LoadAndLaunchPlatformApp("context_menu"); |
| 304 | |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 305 | // Wait for the extension to tell us it's initialized its context menus and |
| 306 | // launched a window. |
| 307 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
| 308 | |
[email protected] | b3c019b | 2012-08-28 02:43:48 | [diff] [blame] | 309 | // The context_menu app has one context menu item. This, along with a |
| 310 | // separator and the developer tools, is all that should be in the menu. |
| 311 | WebContents* web_contents = GetFirstShellWindowWebContents(); |
| 312 | ASSERT_TRUE(web_contents); |
| 313 | WebKit::WebContextMenuData data; |
| 314 | content::ContextMenuParams params(data); |
| 315 | params.is_editable = true; |
[email protected] | 85d3d5e | 2012-08-31 21:19:17 | [diff] [blame] | 316 | scoped_ptr<PlatformAppContextMenu> menu; |
| 317 | menu.reset(new PlatformAppContextMenu(web_contents, params)); |
[email protected] | b3c019b | 2012-08-28 02:43:48 | [diff] [blame] | 318 | menu->Init(); |
| 319 | ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST)); |
| 320 | ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT)); |
[email protected] | 65992d5 | 2012-09-19 23:05:31 | [diff] [blame] | 321 | ASSERT_TRUE( |
| 322 | menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE)); |
[email protected] | 671c970 | 2012-09-27 07:51:19 | [diff] [blame] | 323 | ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP)); |
[email protected] | b3c019b | 2012-08-28 02:43:48 | [diff] [blame] | 324 | ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO)); |
| 325 | ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY)); |
| 326 | ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK)); |
| 327 | ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE)); |
| 328 | } |
| 329 | |
| 330 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuSelection) { |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 331 | ExtensionTestMessageListener launched_listener("Launched", false); |
[email protected] | b3c019b | 2012-08-28 02:43:48 | [diff] [blame] | 332 | LoadAndLaunchPlatformApp("context_menu"); |
| 333 | |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 334 | // Wait for the extension to tell us it's initialized its context menus and |
| 335 | // launched a window. |
| 336 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
| 337 | |
[email protected] | b3c019b | 2012-08-28 02:43:48 | [diff] [blame] | 338 | // The context_menu app has one context menu item. This, along with a |
| 339 | // separator and the developer tools, is all that should be in the menu. |
| 340 | WebContents* web_contents = GetFirstShellWindowWebContents(); |
| 341 | ASSERT_TRUE(web_contents); |
| 342 | WebKit::WebContextMenuData data; |
| 343 | content::ContextMenuParams params(data); |
| 344 | params.selection_text = ASCIIToUTF16("Hello World"); |
[email protected] | 85d3d5e | 2012-08-31 21:19:17 | [diff] [blame] | 345 | scoped_ptr<PlatformAppContextMenu> menu; |
| 346 | menu.reset(new PlatformAppContextMenu(web_contents, params)); |
[email protected] | b3c019b | 2012-08-28 02:43:48 | [diff] [blame] | 347 | menu->Init(); |
| 348 | ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST)); |
| 349 | ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTELEMENT)); |
[email protected] | 65992d5 | 2012-09-19 23:05:31 | [diff] [blame] | 350 | ASSERT_TRUE( |
| 351 | menu->HasCommandWithId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE)); |
[email protected] | 671c970 | 2012-09-27 07:51:19 | [diff] [blame] | 352 | ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP)); |
[email protected] | b3c019b | 2012-08-28 02:43:48 | [diff] [blame] | 353 | ASSERT_FALSE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_UNDO)); |
| 354 | ASSERT_TRUE(menu->HasCommandWithId(IDC_CONTENT_CONTEXT_COPY)); |
| 355 | ASSERT_FALSE(menu->HasCommandWithId(IDC_BACK)); |
| 356 | ASSERT_FALSE(menu->HasCommandWithId(IDC_SAVE_PAGE)); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 357 | } |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 358 | |
[email protected] | dc83c58 | 2012-08-17 02:18:14 | [diff] [blame] | 359 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenuClicked) { |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 360 | ExtensionTestMessageListener launched_listener("Launched", false); |
[email protected] | dc83c58 | 2012-08-17 02:18:14 | [diff] [blame] | 361 | LoadAndLaunchPlatformApp("context_menu_click"); |
| 362 | |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 363 | // Wait for the extension to tell us it's initialized its context menus and |
| 364 | // launched a window. |
| 365 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
| 366 | |
[email protected] | dc83c58 | 2012-08-17 02:18:14 | [diff] [blame] | 367 | // Test that the menu item shows up |
| 368 | WebContents* web_contents = GetFirstShellWindowWebContents(); |
| 369 | ASSERT_TRUE(web_contents); |
| 370 | WebKit::WebContextMenuData data; |
| 371 | content::ContextMenuParams params(data); |
| 372 | params.page_url = GURL("http://foo.bar"); |
[email protected] | 85d3d5e | 2012-08-31 21:19:17 | [diff] [blame] | 373 | scoped_ptr<PlatformAppContextMenu> menu; |
| 374 | menu.reset(new PlatformAppContextMenu(web_contents, params)); |
[email protected] | dc83c58 | 2012-08-17 02:18:14 | [diff] [blame] | 375 | menu->Init(); |
| 376 | ASSERT_TRUE(menu->HasCommandWithId(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST)); |
| 377 | |
| 378 | // Execute the menu item |
| 379 | ExtensionTestMessageListener onclicked_listener("onClicked fired for id1", |
| 380 | false); |
| 381 | menu->ExecuteCommand(IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST); |
| 382 | |
| 383 | ASSERT_TRUE(onclicked_listener.WaitUntilSatisfied()); |
| 384 | } |
| 385 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 386 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) { |
[email protected] | 4d007b31 | 2012-10-17 03:00:48 | [diff] [blame] | 387 | TabsAddedNotificationObserver observer(2); |
| 388 | |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 389 | ASSERT_TRUE(StartTestServer()); |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 390 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_; |
[email protected] | 4d007b31 | 2012-10-17 03:00:48 | [diff] [blame] | 391 | |
| 392 | observer.Wait(); |
| 393 | ASSERT_EQ(2U, observer.tabs().size()); |
| 394 | EXPECT_EQ(std::string(chrome::kExtensionInvalidRequestURL), |
| 395 | observer.tabs()[0]->GetURL().spec()); |
| 396 | EXPECT_EQ("http://chromium.org/", |
| 397 | observer.tabs()[1]->GetURL().spec()); |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 398 | } |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 399 | |
[email protected] | 40eefd5 | 2012-10-04 22:54:22 | [diff] [blame] | 400 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Iframes) { |
[email protected] | fd3238af | 2012-05-22 18:55:30 | [diff] [blame] | 401 | ASSERT_TRUE(StartTestServer()); |
| 402 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_; |
| 403 | } |
| 404 | |
[email protected] | 2aac7ff | 2012-01-25 18:05:11 | [diff] [blame] | 405 | // Tests that localStorage and WebSQL are disabled for platform apps. |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 406 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) { |
[email protected] | 2aac7ff | 2012-01-25 18:05:11 | [diff] [blame] | 407 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_; |
| 408 | } |
[email protected] | 6a5a2e5 | 2012-03-22 03:21:12 | [diff] [blame] | 409 | |
[email protected] | c0cecd1f | 2012-04-05 16:50:12 | [diff] [blame] | 410 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) { |
| 411 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_; |
| 412 | } |
| 413 | |
[email protected] | f0233ff | 2012-07-20 20:14:50 | [diff] [blame] | 414 | // Tests that platform apps can use the chrome.app.window.* API. |
[email protected] | 5177f3a | 2012-11-08 17:51:29 | [diff] [blame] | 415 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) { |
[email protected] | 6a5a2e5 | 2012-03-22 03:21:12 | [diff] [blame] | 416 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_; |
| 417 | } |
[email protected] | 605fb810 | 2012-05-04 01:36:55 | [diff] [blame] | 418 | |
[email protected] | 1df2204 | 2012-08-30 19:48:55 | [diff] [blame] | 419 | // Tests that extensions can't use platform-app-only APIs. |
| 420 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, PlatformAppsOnly) { |
| 421 | ASSERT_TRUE(RunExtensionTestIgnoreManifestWarnings( |
| 422 | "platform_apps/apps_only")) << message_; |
| 423 | } |
| 424 | |
[email protected] | 605fb810 | 2012-05-04 01:36:55 | [diff] [blame] | 425 | // Tests that platform apps have isolated storage by default. |
| 426 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Isolation) { |
| 427 | ASSERT_TRUE(StartTestServer()); |
| 428 | |
| 429 | // Load a (non-app) page under the "localhost" origin that sets a cookie. |
| 430 | GURL set_cookie_url = test_server()->GetURL( |
| 431 | "files/extensions/platform_apps/isolation/set_cookie.html"); |
| 432 | GURL::Replacements replace_host; |
| 433 | std::string host_str("localhost"); // Must stay in scope with replace_host. |
| 434 | replace_host.SetHostStr(host_str); |
| 435 | set_cookie_url = set_cookie_url.ReplaceComponents(replace_host); |
| 436 | |
| 437 | ui_test_utils::NavigateToURLWithDisposition( |
| 438 | browser(), set_cookie_url, |
| 439 | CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 440 | |
| 441 | // Make sure the cookie is set. |
| 442 | int cookie_size; |
| 443 | std::string cookie_value; |
| 444 | automation_util::GetCookies( |
| 445 | set_cookie_url, |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 446 | chrome::GetWebContentsAt(browser(), 0), |
[email protected] | 605fb810 | 2012-05-04 01:36:55 | [diff] [blame] | 447 | &cookie_size, |
| 448 | &cookie_value); |
| 449 | ASSERT_EQ("testCookie=1", cookie_value); |
| 450 | |
| 451 | // Let the platform app request the same URL, and make sure that it doesn't |
| 452 | // see the cookie. |
| 453 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_; |
| 454 | } |
[email protected] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 455 | |
| 456 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ExtensionWindowingApis) { |
| 457 | // Initially there should be just the one browser window visible to the |
| 458 | // extensions API. |
| 459 | const Extension* extension = LoadExtension( |
| 460 | test_data_dir_.AppendASCII("common/background_page")); |
| 461 | ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension)); |
| 462 | |
| 463 | // And no shell windows. |
| 464 | ASSERT_EQ(0U, GetShellWindowCount()); |
| 465 | |
| 466 | // Launch a platform app that shows a window. |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 467 | ExtensionTestMessageListener launched_listener("Launched", false); |
[email protected] | ad5bb8a9 | 2012-06-07 03:55:58 | [diff] [blame] | 468 | LoadAndLaunchPlatformApp("minimal"); |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 469 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
[email protected] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 470 | ASSERT_EQ(1U, GetShellWindowCount()); |
| 471 | ShellWindowRegistry::ShellWindowSet shell_windows = |
| 472 | ShellWindowRegistry::Get(browser()->profile())->shell_windows(); |
| 473 | int shell_window_id = (*shell_windows.begin())->session_id().id(); |
| 474 | |
| 475 | // But it's not visible to the extensions API, it still thinks there's just |
| 476 | // one browser window. |
| 477 | ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension)); |
| 478 | // It can't look it up by ID either |
| 479 | ASSERT_FALSE(RunGetWindowFunctionForExtension(shell_window_id, extension)); |
| 480 | |
| 481 | // The app can also only see one window (its own). |
[email protected] | ad5bb8a9 | 2012-06-07 03:55:58 | [diff] [blame] | 482 | // TODO(jeremya): add an extension function to get a shell window by ID, and |
| 483 | // 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] | 484 | |
| 485 | // Launch another platform app that also shows a window. |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 486 | ExtensionTestMessageListener launched_listener2("Launched", false); |
[email protected] | ad5bb8a9 | 2012-06-07 03:55:58 | [diff] [blame] | 487 | LoadAndLaunchPlatformApp("context_menu"); |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 488 | ASSERT_TRUE(launched_listener2.WaitUntilSatisfied()); |
[email protected] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 489 | |
| 490 | // There are two total shell windows, but each app can only see its own. |
| 491 | ASSERT_EQ(2U, GetShellWindowCount()); |
[email protected] | ad5bb8a9 | 2012-06-07 03:55:58 | [diff] [blame] | 492 | // TODO(jeremya): as above, this requires more extension functions. |
[email protected] | 31bdbfef | 2012-05-22 19:59:15 | [diff] [blame] | 493 | } |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 494 | |
[email protected] | a2ece52 | 2012-10-16 03:41:10 | [diff] [blame] | 495 | // ChromeOS does not support passing arguments on the command line, so the tests |
| 496 | // that rely on this functionality are disabled. |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 497 | #if !defined(OS_CHROMEOS) |
| 498 | // Tests that command line parameters get passed through to platform apps |
| 499 | // via launchData correctly when launching with a file. |
[email protected] | 7d0cbb6 | 2012-11-22 04:41:07 | [diff] [blame^] | 500 | // TODO(benwells/jeremya): tests need a way to specify a handler ID. |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 501 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithFile) { |
[email protected] | 85d3d5e | 2012-08-31 21:19:17 | [diff] [blame] | 502 | SetCommandLineArg(kTestFilePath); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 503 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_file")) |
| 504 | << message_; |
| 505 | } |
| 506 | |
[email protected] | a5a0be0 | 2012-07-18 05:51:54 | [diff] [blame] | 507 | // Tests that relative paths can be passed through to the platform app. |
| 508 | // This test doesn't use the normal test infrastructure as it needs to open |
| 509 | // the application differently to all other platform app tests, by setting |
| 510 | // the application_launch::LaunchParams.current_directory field. |
| 511 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithRelativeFile) { |
| 512 | // Setup the command line |
| 513 | ClearCommandLineArgs(); |
| 514 | CommandLine* command_line = CommandLine::ForCurrentProcess(); |
[email protected] | 85d3d5e | 2012-08-31 21:19:17 | [diff] [blame] | 515 | FilePath relative_test_doc = FilePath::FromUTF8Unsafe(kTestFilePath); |
[email protected] | a5a0be0 | 2012-07-18 05:51:54 | [diff] [blame] | 516 | relative_test_doc = relative_test_doc.NormalizePathSeparators(); |
| 517 | command_line->AppendArgPath(relative_test_doc); |
| 518 | |
| 519 | // Load the extension |
| 520 | ResultCatcher catcher; |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 521 | const Extension* extension = LoadExtension( |
[email protected] | a5a0be0 | 2012-07-18 05:51:54 | [diff] [blame] | 522 | test_data_dir_.AppendASCII("platform_apps/launch_file")); |
| 523 | ASSERT_TRUE(extension); |
| 524 | |
| 525 | // Run the test |
| 526 | application_launch::LaunchParams params(browser()->profile(), extension, |
| 527 | extension_misc::LAUNCH_NONE, |
| 528 | NEW_WINDOW); |
| 529 | params.command_line = CommandLine::ForCurrentProcess(); |
| 530 | params.current_directory = test_data_dir_; |
| 531 | application_launch::OpenApplication(params); |
| 532 | |
| 533 | if (!catcher.GetNextResult()) { |
| 534 | message_ = catcher.message(); |
| 535 | ASSERT_TRUE(0); |
| 536 | } |
| 537 | } |
| 538 | |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 539 | // Tests that no launch data is sent through if the file is of the wrong MIME |
| 540 | // type. |
| 541 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithWrongType) { |
[email protected] | 85d3d5e | 2012-08-31 21:19:17 | [diff] [blame] | 542 | SetCommandLineArg(kTestFilePath); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 543 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_wrong_type")) |
| 544 | << message_; |
| 545 | } |
| 546 | |
| 547 | // Tests that no launch data is sent through if the platform app does not |
| 548 | // provide an intent. |
| 549 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNoIntent) { |
[email protected] | 85d3d5e | 2012-08-31 21:19:17 | [diff] [blame] | 550 | SetCommandLineArg(kTestFilePath); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 551 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_no_intent")) |
| 552 | << message_; |
| 553 | } |
| 554 | |
| 555 | // Tests that no launch data is sent through if the file MIME type cannot |
| 556 | // be read. |
| 557 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoType) { |
| 558 | SetCommandLineArg("platform_apps/launch_files/test.unknownextension"); |
| 559 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid")) |
| 560 | << message_; |
| 561 | } |
| 562 | |
| 563 | // Tests that no launch data is sent through if the file does not exist. |
| 564 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchNoFile) { |
| 565 | SetCommandLineArg("platform_apps/launch_files/doesnotexist.txt"); |
| 566 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid")) |
| 567 | << message_; |
| 568 | } |
| 569 | |
| 570 | // Tests that no launch data is sent through if the argument is a directory. |
| 571 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithDirectory) { |
| 572 | SetCommandLineArg("platform_apps/launch_files"); |
| 573 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_invalid")) |
| 574 | << message_; |
| 575 | } |
| 576 | |
| 577 | // Tests that no launch data is sent through if there are no arguments passed |
| 578 | // on the command line |
| 579 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, LaunchWithNothing) { |
| 580 | ClearCommandLineArgs(); |
| 581 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch_nothing")) |
| 582 | << message_; |
| 583 | } |
[email protected] | ffc7b4d | 2012-06-08 00:05:32 | [diff] [blame] | 584 | |
| 585 | // Test that platform apps can use the chrome.fileSystem.getDisplayPath |
| 586 | // function to get the native file system path of a file they are launched with. |
| 587 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, GetDisplayPath) { |
[email protected] | 85d3d5e | 2012-08-31 21:19:17 | [diff] [blame] | 588 | SetCommandLineArg(kTestFilePath); |
[email protected] | ffc7b4d | 2012-06-08 00:05:32 | [diff] [blame] | 589 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/get_display_path")) |
| 590 | << message_; |
| 591 | } |
| 592 | |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 593 | #endif // defined(OS_CHROMEOS) |
[email protected] | 5b1a04b4 | 2012-06-15 00:41:44 | [diff] [blame] | 594 | |
| 595 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OpenLink) { |
| 596 | ASSERT_TRUE(StartTestServer()); |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 597 | content::WindowedNotificationObserver observer( |
[email protected] | 5b1a04b4 | 2012-06-15 00:41:44 | [diff] [blame] | 598 | chrome::NOTIFICATION_TAB_ADDED, |
| 599 | content::Source<content::WebContentsDelegate>(browser())); |
| 600 | LoadAndLaunchPlatformApp("open_link"); |
| 601 | observer.Wait(); |
| 602 | ASSERT_EQ(2, browser()->tab_count()); |
| 603 | } |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 604 | |
[email protected] | 3a8e6194 | 2012-08-23 01:46:45 | [diff] [blame] | 605 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MutationEventsDisabled) { |
| 606 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/mutation_events")) << message_; |
| 607 | } |
| 608 | |
[email protected] | 939532d0 | 2012-08-31 23:37:33 | [diff] [blame] | 609 | // Test that windows created with an id will remember and restore their |
| 610 | // geometry when opening new windows. |
[email protected] | 6908083 | 2012-11-20 20:31:47 | [diff] [blame] | 611 | // Originally disabled due to flakiness (see http://crbug.com/155459) |
| 612 | // but now because a regression breaks the test (http://crbug.com/160343). |
[email protected] | 329c4e9 | 2012-11-09 06:04:24 | [diff] [blame] | 613 | #define MAYBE_ShellWindowRestorePosition DISABLED_ShellWindowRestorePosition |
[email protected] | 9d53c1c | 2012-10-18 00:49:54 | [diff] [blame] | 614 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, |
[email protected] | 329c4e9 | 2012-11-09 06:04:24 | [diff] [blame] | 615 | MAYBE_ShellWindowRestorePosition) { |
[email protected] | 939532d0 | 2012-08-31 23:37:33 | [diff] [blame] | 616 | ExtensionTestMessageListener page2_listener("WaitForPage2", true); |
| 617 | ExtensionTestMessageListener page3_listener("WaitForPage3", true); |
| 618 | ExtensionTestMessageListener done_listener("Done1", false); |
| 619 | ExtensionTestMessageListener done2_listener("Done2", false); |
| 620 | ExtensionTestMessageListener done3_listener("Done3", false); |
| 621 | |
| 622 | ASSERT_TRUE(LoadAndLaunchPlatformApp("geometry")); |
| 623 | |
| 624 | // Wait for the app to be launched (although this is mostly to have a |
| 625 | // message to reply to to let the script know it should create its second |
| 626 | // window. |
| 627 | ASSERT_TRUE(page2_listener.WaitUntilSatisfied()); |
| 628 | |
| 629 | // Wait for the first window to verify its geometry was correctly set |
| 630 | // from the default* attributes passed to the create function. |
| 631 | ASSERT_TRUE(done_listener.WaitUntilSatisfied()); |
| 632 | |
| 633 | // Programatically move and resize the window. |
| 634 | ShellWindow* window = GetFirstShellWindow(); |
| 635 | ASSERT_TRUE(window); |
| 636 | gfx::Rect bounds(137, 143, 203, 187); |
| 637 | window->GetBaseWindow()->SetBounds(bounds); |
| 638 | |
| 639 | #if defined(TOOLKIT_GTK) |
| 640 | // TODO(mek): On GTK we have to wait for a roundtrip to the X server before |
| 641 | // a resize actually happens: |
| 642 | // "if you call gtk_window_resize() then immediately call |
| 643 | // gtk_window_get_size(), the size won't have taken effect yet. After the |
| 644 | // window manager processes the resize request, GTK+ receives notification |
| 645 | // that the size has changed via a configure event, and the size of the |
| 646 | // window gets updated." |
| 647 | // Because of this we have to wait for an unknown time for the resize to |
| 648 | // actually take effect. So wait some time or until the resize got |
| 649 | // handled. |
| 650 | base::TimeTicks end_time = base::TimeTicks::Now() + |
| 651 | TestTimeouts::action_timeout(); |
| 652 | while (base::TimeTicks::Now() < end_time && |
| 653 | bounds != window->GetBaseWindow()->GetBounds()) { |
| 654 | content::RunAllPendingInMessageLoop(); |
| 655 | } |
| 656 | |
| 657 | // In the GTK ShellWindow implementation there also is a delay between |
| 658 | // getting the correct bounds and it calling SaveWindowPosition, so call that |
| 659 | // method explicitly to make sure the value was stored. |
| 660 | window->SaveWindowPosition(); |
| 661 | #endif // defined(TOOLKIT_GTK) |
| 662 | |
| 663 | // Make sure the window was properly moved&resized. |
| 664 | ASSERT_EQ(bounds, window->GetBaseWindow()->GetBounds()); |
| 665 | |
| 666 | // Tell javascript to open a second window. |
| 667 | page2_listener.Reply("continue"); |
| 668 | |
| 669 | // Wait for javascript to verify that the second window got the updated |
| 670 | // coordinates, ignoring the default coordinates passed to the create method. |
| 671 | ASSERT_TRUE(done2_listener.WaitUntilSatisfied()); |
| 672 | |
| 673 | // Tell javascript to open a third window. |
| 674 | page3_listener.Reply("continue"); |
| 675 | |
| 676 | // Wait for javascript to verify that the third window got the restored size |
| 677 | // and explicitly specified coordinates. |
| 678 | ASSERT_TRUE(done3_listener.WaitUntilSatisfied()); |
| 679 | } |
[email protected] | 939532d0 | 2012-08-31 23:37:33 | [diff] [blame] | 680 | |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 681 | // Tests that a running app is recorded in the preferences as such. |
| 682 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, RunningAppsAreRecorded) { |
| 683 | content::WindowedNotificationObserver extension_suspended( |
| 684 | chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 685 | content::NotificationService::AllSources()); |
| 686 | |
| 687 | const Extension* extension = LoadExtension( |
| 688 | test_data_dir_.AppendASCII("platform_apps/restart_test")); |
| 689 | ASSERT_TRUE(extension); |
| 690 | ExtensionService* extension_service = |
| 691 | ExtensionSystem::Get(browser()->profile())->extension_service(); |
| 692 | ExtensionPrefs* extension_prefs = extension_service->extension_prefs(); |
| 693 | |
| 694 | // App is running. |
| 695 | ASSERT_TRUE(extension_prefs->IsExtensionRunning(extension->id())); |
| 696 | |
| 697 | // Wait for the extension to get suspended. |
| 698 | extension_suspended.Wait(); |
| 699 | |
| 700 | // App isn't running because it got suspended. |
| 701 | ASSERT_FALSE(extension_prefs->IsExtensionRunning(extension->id())); |
| 702 | |
| 703 | // Pretend that the app is supposed to be running. |
| 704 | extension_prefs->SetExtensionRunning(extension->id(), true); |
| 705 | |
| 706 | ExtensionTestMessageListener restart_listener("onRestarted", false); |
[email protected] | 11945462 | 2012-10-18 09:48:32 | [diff] [blame] | 707 | AppRestoreServiceFactory::GetForProfile(browser()->profile())-> |
| 708 | HandleStartup(true); |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 709 | restart_listener.WaitUntilSatisfied(); |
| 710 | } |
| 711 | |
[email protected] | 545b531 | 2012-11-07 12:18:21 | [diff] [blame] | 712 | namespace { |
| 713 | |
| 714 | class PlatformAppDevToolsBrowserTest : public PlatformAppBrowserTest { |
| 715 | protected: |
| 716 | enum TestFlags { |
| 717 | RELAUNCH = 0x1, |
| 718 | HAS_ID = 0x2, |
| 719 | }; |
| 720 | // Runs a test inside a harness that opens DevTools on a shell window. |
| 721 | void RunTestWithDevTools(const char* name, int test_flags); |
| 722 | }; |
| 723 | |
| 724 | void PlatformAppDevToolsBrowserTest::RunTestWithDevTools( |
| 725 | const char* name, int test_flags) { |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 726 | using content::DevToolsAgentHostRegistry; |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 727 | ExtensionTestMessageListener launched_listener("Launched", false); |
[email protected] | 545b531 | 2012-11-07 12:18:21 | [diff] [blame] | 728 | const Extension* extension = LoadAndLaunchPlatformApp(name); |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 729 | ASSERT_TRUE(extension); |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 730 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 731 | ShellWindow* window = GetFirstShellWindow(); |
| 732 | ASSERT_TRUE(window); |
[email protected] | 545b531 | 2012-11-07 12:18:21 | [diff] [blame] | 733 | ASSERT_EQ(window->window_key().empty(), (test_flags & HAS_ID) == 0); |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 734 | content::RenderViewHost* rvh = window->web_contents()->GetRenderViewHost(); |
| 735 | ASSERT_TRUE(rvh); |
| 736 | |
| 737 | // Ensure no DevTools open for the ShellWindow, then open one. |
| 738 | ASSERT_FALSE(DevToolsAgentHostRegistry::HasDevToolsAgentHost(rvh)); |
| 739 | DevToolsWindow* devtools_window = DevToolsWindow::OpenDevToolsWindow(rvh); |
| 740 | content::WindowedNotificationObserver loaded_observer( |
| 741 | content::NOTIFICATION_LOAD_STOP, |
| 742 | content::Source<content::NavigationController>( |
| 743 | &devtools_window->tab_contents()->web_contents()->GetController())); |
| 744 | loaded_observer.Wait(); |
| 745 | ASSERT_TRUE(DevToolsAgentHostRegistry::HasDevToolsAgentHost(rvh)); |
| 746 | |
[email protected] | 545b531 | 2012-11-07 12:18:21 | [diff] [blame] | 747 | if (test_flags & RELAUNCH) { |
| 748 | // Close the ShellWindow, and ensure it is gone. |
| 749 | CloseShellWindow(window); |
| 750 | ASSERT_FALSE(GetFirstShellWindow()); |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 751 | |
[email protected] | 545b531 | 2012-11-07 12:18:21 | [diff] [blame] | 752 | // Relaunch the app and get a new ShellWindow. |
| 753 | content::WindowedNotificationObserver app_loaded_observer( |
| 754 | content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 755 | content::NotificationService::AllSources()); |
| 756 | application_launch::OpenApplication(application_launch::LaunchParams( |
| 757 | browser()->profile(), extension, extension_misc::LAUNCH_NONE, |
| 758 | NEW_WINDOW)); |
| 759 | app_loaded_observer.Wait(); |
| 760 | window = GetFirstShellWindow(); |
| 761 | ASSERT_TRUE(window); |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 762 | |
[email protected] | 545b531 | 2012-11-07 12:18:21 | [diff] [blame] | 763 | // DevTools should have reopened with the relaunch. |
| 764 | rvh = window->web_contents()->GetRenderViewHost(); |
| 765 | ASSERT_TRUE(rvh); |
| 766 | ASSERT_TRUE(DevToolsAgentHostRegistry::HasDevToolsAgentHost(rvh)); |
| 767 | } |
[email protected] | e85cc64 | 2012-10-24 06:14:23 | [diff] [blame] | 768 | } |
| 769 | |
[email protected] | 545b531 | 2012-11-07 12:18:21 | [diff] [blame] | 770 | } // namespace |
| 771 | |
| 772 | IN_PROC_BROWSER_TEST_F(PlatformAppDevToolsBrowserTest, ReOpenedWithID) { |
| 773 | RunTestWithDevTools("minimal_id", RELAUNCH | HAS_ID); |
| 774 | } |
| 775 | |
| 776 | IN_PROC_BROWSER_TEST_F(PlatformAppDevToolsBrowserTest, ReOpenedWithURL) { |
| 777 | RunTestWithDevTools("minimal", RELAUNCH); |
| 778 | } |
| 779 | |
[email protected] | ddf4216 | 2012-11-06 21:14:55 | [diff] [blame] | 780 | // Test that showing a permission request as a constrained window works and is |
| 781 | // correctly parented. |
| 782 | #if defined(OS_MACOSX) |
[email protected] | cd20266 | 2012-11-07 19:11:41 | [diff] [blame] | 783 | #define MAYBE_ConstrainedWindowRequest DISABLED_ConstrainedWindowRequest |
[email protected] | ddf4216 | 2012-11-06 21:14:55 | [diff] [blame] | 784 | #else |
| 785 | // TODO(sail): Enable this on other platforms once http://crbug.com/95455 is |
| 786 | // fixed. |
| 787 | #define MAYBE_ConstrainedWindowRequest DISABLED_ConstrainedWindowRequest |
| 788 | #endif |
| 789 | |
| 790 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_ConstrainedWindowRequest) { |
| 791 | RequestPermissionsFunction::SetIgnoreUserGestureForTests(true); |
| 792 | const Extension* extension = |
| 793 | LoadAndLaunchPlatformApp("optional_permission_request"); |
| 794 | ASSERT_TRUE(extension) << "Failed to load extension."; |
| 795 | |
| 796 | WebContents* web_contents = GetFirstShellWindowWebContents(); |
| 797 | ASSERT_TRUE(web_contents); |
| 798 | |
| 799 | // Verify that the shell window has a constrained window attached. |
| 800 | ConstrainedWindowTabHelper* constrained_window_tab_helper = |
| 801 | ConstrainedWindowTabHelper::FromWebContents(web_contents); |
| 802 | EXPECT_EQ(1u, constrained_window_tab_helper->constrained_window_count()); |
| 803 | |
| 804 | // Close the constrained window and wait for the reply to the permission |
| 805 | // request. |
| 806 | ExtensionTestMessageListener listener("PermissionRequestDone", false); |
| 807 | constrained_window_tab_helper->CloseConstrainedWindows(); |
| 808 | ASSERT_TRUE(listener.WaitUntilSatisfied()); |
[email protected] | ddf4216 | 2012-11-06 21:14:55 | [diff] [blame] | 809 | } |
| 810 | |
[email protected] | bf1be4e | 2012-11-07 20:32:27 | [diff] [blame] | 811 | // Tests that an app calling chrome.runtime.reload will reload the app and |
| 812 | // relaunch it if it was running. |
| 813 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ReloadRelaunches) { |
| 814 | ExtensionTestMessageListener launched_listener("Launched", true); |
[email protected] | e92a361 | 2012-11-08 18:44:58 | [diff] [blame] | 815 | const Extension* extension = LoadAndLaunchPlatformApp("reload"); |
[email protected] | bf1be4e | 2012-11-07 20:32:27 | [diff] [blame] | 816 | ASSERT_TRUE(extension); |
[email protected] | bf1be4e | 2012-11-07 20:32:27 | [diff] [blame] | 817 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
| 818 | ASSERT_TRUE(GetFirstShellWindow()); |
| 819 | |
| 820 | // Now tell the app to reload itself |
| 821 | ExtensionTestMessageListener launched_listener2("Launched", false); |
| 822 | launched_listener.Reply("reload"); |
| 823 | ASSERT_TRUE(launched_listener2.WaitUntilSatisfied()); |
| 824 | ASSERT_TRUE(GetFirstShellWindow()); |
[email protected] | bf1be4e | 2012-11-07 20:32:27 | [diff] [blame] | 825 | } |
| 826 | |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 827 | } // namespace extensions |