[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 | |
| 5 | #include "base/command_line.h" |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 6 | #include "base/stringprintf.h" |
| 7 | #include "base/utf_string_conversions.h" |
[email protected] | 605fb810 | 2012-05-04 01:36:55 | [diff] [blame] | 8 | #include "chrome/browser/automation/automation_util.h" |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 9 | #include "chrome/browser/extensions/extension_apitest.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 10 | #include "chrome/browser/extensions/extension_browsertest.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 12 | #include "chrome/browser/extensions/extension_test_message_listener.h" |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 13 | #include "chrome/browser/extensions/shell_window_registry.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 14 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 15 | #include "chrome/browser/tab_contents/render_view_context_menu.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 16 | #include "chrome/browser/ui/browser.h" |
| 17 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 18 | #include "chrome/browser/ui/extensions/shell_window.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 19 | #include "chrome/common/chrome_switches.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 20 | #include "chrome/common/extensions/extension_constants.h" |
| 21 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 22 | #include "content/public/browser/web_contents.h" |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 23 | #include "content/public/common/context_menu_params.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 24 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 25 | #include "ui/base/models/menu_model.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 26 | |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 27 | using content::WebContents; |
| 28 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 29 | namespace { |
| 30 | // Non-abstract RenderViewContextMenu class. |
| 31 | class PlatformAppContextMenu : public RenderViewContextMenu { |
| 32 | public: |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 33 | PlatformAppContextMenu(WebContents* web_contents, |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 34 | const content::ContextMenuParams& params) |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 35 | : RenderViewContextMenu(web_contents, params) {} |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 36 | |
| 37 | protected: |
| 38 | // These two functions implement pure virtual methods of |
| 39 | // RenderViewContextMenu. |
| 40 | virtual bool GetAcceleratorForCommandId(int command_id, |
| 41 | ui::Accelerator* accelerator) { |
| 42 | return false; |
| 43 | } |
| 44 | virtual void PlatformInit() {} |
[email protected] | f5d5cd0 | 2012-05-04 13:59:30 | [diff] [blame] | 45 | virtual void PlatformCancel() {} |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | } // namespace |
| 49 | |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 50 | class PlatformAppBrowserTest : public ExtensionApiTest { |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 51 | public: |
| 52 | virtual void SetUpCommandLine(CommandLine* command_line) { |
| 53 | ExtensionBrowserTest::SetUpCommandLine(command_line); |
| 54 | command_line->AppendSwitch(switches::kEnablePlatformApps); |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 55 | command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 56 | } |
| 57 | |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 58 | protected: |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 59 | void LoadAndLaunchPlatformApp(const char* name) { |
[email protected] | 939ac75 | 2012-01-13 03:00:47 | [diff] [blame] | 60 | ui_test_utils::WindowedNotificationObserver app_loaded_observer( |
| 61 | content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 62 | content::NotificationService::AllSources()); |
| 63 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 64 | EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps"). |
| 65 | AppendASCII(name))); |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 66 | |
| 67 | ExtensionService* service = browser()->profile()->GetExtensionService(); |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 68 | const extensions::Extension* extension = service->GetExtensionById( |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 69 | last_loaded_extension_id_, false); |
| 70 | EXPECT_TRUE(extension); |
| 71 | |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 72 | Browser::OpenApplication( |
| 73 | browser()->profile(), |
| 74 | extension, |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 75 | extension_misc::LAUNCH_NONE, |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 76 | GURL(), |
| 77 | NEW_WINDOW); |
| 78 | |
[email protected] | 939ac75 | 2012-01-13 03:00:47 | [diff] [blame] | 79 | app_loaded_observer.Wait(); |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 80 | } |
| 81 | |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 82 | // Gets the WebContents associated with the first shell window that is found |
| 83 | // (most tests only deal with one platform app window, so this is good |
| 84 | // enough). |
| 85 | WebContents* GetFirstShellWindowWebContents() { |
| 86 | ShellWindowRegistry* app_registry = |
| 87 | ShellWindowRegistry::Get(browser()->profile()); |
| 88 | ShellWindowRegistry::const_iterator iter; |
| 89 | ShellWindowRegistry::ShellWindowSet shell_windows = |
| 90 | app_registry->shell_windows(); |
| 91 | for (iter = shell_windows.begin(); iter != shell_windows.end(); ++iter) { |
| 92 | return (*iter)->web_contents(); |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return NULL; |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 96 | } |
| 97 | }; |
| 98 | |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 99 | // Tests that platform apps received the "launch" event when launched. |
| 100 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) { |
| 101 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_; |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 102 | } |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 103 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 104 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) { |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 105 | ExtensionTestMessageListener launched_listener("Launched", false); |
| 106 | LoadAndLaunchPlatformApp("empty_context_menu"); |
| 107 | |
| 108 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 109 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 110 | // 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] | 111 | // only include the developer tools. |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 112 | WebContents* web_contents = GetFirstShellWindowWebContents(); |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 113 | ASSERT_TRUE(web_contents); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 114 | WebKit::WebContextMenuData data; |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 115 | content::ContextMenuParams params(data); |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 116 | PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 117 | params); |
| 118 | menu->Init(); |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 119 | // TODO(benwells): Remove the constant below. Instead of checking the |
| 120 | // number of menu items check certain item's absense and presence. |
[email protected] | 40b1300 | 2012-03-06 02:23:15 | [diff] [blame] | 121 | // 3 including separator |
| 122 | ASSERT_EQ(3, menu->menu_model().GetItemCount()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 123 | } |
| 124 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 125 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) { |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 126 | ExtensionTestMessageListener launched_listener("Launched", false); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 127 | LoadAndLaunchPlatformApp("context_menu"); |
| 128 | |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 129 | // Wait for the extension to tell us it's initialized its context menus and |
| 130 | // launched a window. |
| 131 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 132 | |
[email protected] | 0cfacd8 | 2012-02-09 01:55:33 | [diff] [blame] | 133 | // The context_menu app has one context menu item. This, along with a |
| 134 | // separator and the developer tools, is all that should be in the menu. |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 135 | WebContents* web_contents = GetFirstShellWindowWebContents(); |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 136 | ASSERT_TRUE(web_contents); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 137 | WebKit::WebContextMenuData data; |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 138 | content::ContextMenuParams params(data); |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 139 | PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 140 | params); |
| 141 | menu->Init(); |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 142 | // TODO(benwells): Remove the constant below. Instead of checking the |
| 143 | // number of menu items check certain item's absense and presence. |
[email protected] | 40b1300 | 2012-03-06 02:23:15 | [diff] [blame] | 144 | ASSERT_EQ(4, menu->menu_model().GetItemCount()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 145 | } |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 146 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 147 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) { |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 148 | ASSERT_TRUE(StartTestServer()); |
| 149 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_; |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 150 | } |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 151 | |
[email protected] | fd3238af | 2012-05-22 18:55:30 | [diff] [blame^] | 152 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Iframes) { |
| 153 | ASSERT_TRUE(StartTestServer()); |
| 154 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_; |
| 155 | } |
| 156 | |
[email protected] | 2aac7ff | 2012-01-25 18:05:11 | [diff] [blame] | 157 | // Tests that localStorage and WebSQL are disabled for platform apps. |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 158 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) { |
[email protected] | 2aac7ff | 2012-01-25 18:05:11 | [diff] [blame] | 159 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_; |
| 160 | } |
[email protected] | 6a5a2e5 | 2012-03-22 03:21:12 | [diff] [blame] | 161 | |
[email protected] | c0cecd1f | 2012-04-05 16:50:12 | [diff] [blame] | 162 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) { |
| 163 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_; |
| 164 | } |
| 165 | |
[email protected] | 6a5a2e5 | 2012-03-22 03:21:12 | [diff] [blame] | 166 | // Tests that platform apps can use the chrome.windows.* API. |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 167 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) { |
[email protected] | 6a5a2e5 | 2012-03-22 03:21:12 | [diff] [blame] | 168 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_; |
| 169 | } |
[email protected] | 605fb810 | 2012-05-04 01:36:55 | [diff] [blame] | 170 | |
| 171 | // Tests that platform apps have isolated storage by default. |
| 172 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Isolation) { |
| 173 | ASSERT_TRUE(StartTestServer()); |
| 174 | |
| 175 | // Load a (non-app) page under the "localhost" origin that sets a cookie. |
| 176 | GURL set_cookie_url = test_server()->GetURL( |
| 177 | "files/extensions/platform_apps/isolation/set_cookie.html"); |
| 178 | GURL::Replacements replace_host; |
| 179 | std::string host_str("localhost"); // Must stay in scope with replace_host. |
| 180 | replace_host.SetHostStr(host_str); |
| 181 | set_cookie_url = set_cookie_url.ReplaceComponents(replace_host); |
| 182 | |
| 183 | ui_test_utils::NavigateToURLWithDisposition( |
| 184 | browser(), set_cookie_url, |
| 185 | CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 186 | |
| 187 | // Make sure the cookie is set. |
| 188 | int cookie_size; |
| 189 | std::string cookie_value; |
| 190 | automation_util::GetCookies( |
| 191 | set_cookie_url, |
| 192 | browser()->GetWebContentsAt(0), |
| 193 | &cookie_size, |
| 194 | &cookie_value); |
| 195 | ASSERT_EQ("testCookie=1", cookie_value); |
| 196 | |
| 197 | // Let the platform app request the same URL, and make sure that it doesn't |
| 198 | // see the cookie. |
| 199 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_; |
| 200 | } |