[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] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 8 | #include "chrome/browser/extensions/extension_apitest.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 9 | #include "chrome/browser/extensions/extension_browsertest.h" |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 10 | #include "chrome/browser/extensions/extension_host.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] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 13 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 14 | #include "chrome/browser/tab_contents/render_view_context_menu.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser.h" |
| 16 | #include "chrome/browser/ui/browser_list.h" |
| 17 | #include "chrome/common/chrome_switches.h" |
| 18 | #include "chrome/common/extensions/extension_constants.h" |
| 19 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 20 | #include "content/public/browser/web_contents.h" |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 21 | #include "content/public/common/context_menu_params.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 22 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 23 | #include "ui/base/models/menu_model.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 24 | |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 25 | using content::WebContents; |
| 26 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 27 | namespace { |
| 28 | // Non-abstract RenderViewContextMenu class. |
| 29 | class PlatformAppContextMenu : public RenderViewContextMenu { |
| 30 | public: |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 31 | PlatformAppContextMenu(WebContents* web_contents, |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 32 | const content::ContextMenuParams& params) |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 33 | : RenderViewContextMenu(web_contents, params) {} |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 34 | |
| 35 | protected: |
| 36 | // These two functions implement pure virtual methods of |
| 37 | // RenderViewContextMenu. |
| 38 | virtual bool GetAcceleratorForCommandId(int command_id, |
| 39 | ui::Accelerator* accelerator) { |
| 40 | return false; |
| 41 | } |
| 42 | virtual void PlatformInit() {} |
| 43 | }; |
| 44 | |
| 45 | } // namespace |
| 46 | |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 47 | class PlatformAppBrowserTest : public ExtensionApiTest { |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 48 | public: |
| 49 | virtual void SetUpCommandLine(CommandLine* command_line) { |
| 50 | ExtensionBrowserTest::SetUpCommandLine(command_line); |
| 51 | command_line->AppendSwitch(switches::kEnablePlatformApps); |
| 52 | } |
| 53 | |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 54 | protected: |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 55 | void LoadAndLaunchPlatformApp(const char* name) { |
[email protected] | 939ac75 | 2012-01-13 03:00:47 | [diff] [blame] | 56 | ui_test_utils::WindowedNotificationObserver app_loaded_observer( |
| 57 | content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 58 | content::NotificationService::AllSources()); |
| 59 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 60 | EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps"). |
| 61 | AppendASCII(name))); |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 62 | |
| 63 | ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 64 | const Extension* extension = service->GetExtensionById( |
| 65 | last_loaded_extension_id_, false); |
| 66 | EXPECT_TRUE(extension); |
| 67 | |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 68 | size_t platform_app_count = GetPlatformAppCount(); |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 69 | |
| 70 | Browser::OpenApplication( |
| 71 | browser()->profile(), |
| 72 | extension, |
| 73 | extension_misc::LAUNCH_SHELL, |
| 74 | GURL(), |
| 75 | NEW_WINDOW); |
| 76 | |
[email protected] | 939ac75 | 2012-01-13 03:00:47 | [diff] [blame] | 77 | app_loaded_observer.Wait(); |
| 78 | |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 79 | // Now we have a new platform app running. |
| 80 | EXPECT_EQ(platform_app_count + 1, GetPlatformAppCount()); |
| 81 | } |
| 82 | |
| 83 | // Gets the number of platform apps that are running. |
| 84 | size_t GetPlatformAppCount() { |
| 85 | int count = 0; |
| 86 | ExtensionProcessManager* process_manager = |
| 87 | browser()->profile()->GetExtensionProcessManager(); |
| 88 | ExtensionProcessManager::const_iterator iter; |
| 89 | for (iter = process_manager->begin(); iter != process_manager->end(); |
| 90 | ++iter) { |
| 91 | ExtensionHost* host = *iter; |
| 92 | if (host->extension() && host->extension()->is_platform_app()) |
| 93 | count++; |
| 94 | } |
| 95 | |
| 96 | return count; |
| 97 | } |
| 98 | |
| 99 | // Gets the WebContents associated with the ExtensionHost of the first |
| 100 | // platform app that is found (most tests only deal with one platform |
| 101 | // app, so this is good enough). |
| 102 | WebContents* GetFirstPlatformAppWebContents() { |
| 103 | ExtensionProcessManager* process_manager = |
| 104 | browser()->profile()->GetExtensionProcessManager(); |
| 105 | ExtensionProcessManager::const_iterator iter; |
| 106 | for (iter = process_manager->begin(); iter != process_manager->end(); |
| 107 | ++iter) { |
| 108 | ExtensionHost* host = *iter; |
| 109 | if (host->extension() && host->extension()->is_platform_app()) |
| 110 | return host->host_contents(); |
| 111 | } |
| 112 | |
| 113 | return NULL; |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 114 | } |
| 115 | }; |
| 116 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 117 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OpenAppInShellContainer) { |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 118 | ASSERT_EQ(0u, GetPlatformAppCount()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 119 | LoadAndLaunchPlatformApp("empty"); |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 120 | ASSERT_EQ(1u, GetPlatformAppCount()); |
[email protected] | a6080c0 | 2012-01-11 23:33:49 | [diff] [blame] | 121 | |
| 122 | UnloadExtension(last_loaded_extension_id_); |
| 123 | ASSERT_EQ(0u, GetPlatformAppCount()); |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 124 | } |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 125 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 126 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) { |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 127 | LoadAndLaunchPlatformApp("empty"); |
| 128 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 129 | // 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] | 130 | // only include the developer tools. |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 131 | WebContents* web_contents = GetFirstPlatformAppWebContents(); |
| 132 | ASSERT_TRUE(web_contents); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 133 | WebKit::WebContextMenuData data; |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 134 | content::ContextMenuParams params(data); |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 135 | PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 136 | params); |
| 137 | menu->Init(); |
[email protected] | 40b1300 | 2012-03-06 02:23:15 | [diff] [blame] | 138 | // 3 including separator |
| 139 | ASSERT_EQ(3, menu->menu_model().GetItemCount()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 142 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) { |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 143 | ExtensionTestMessageListener listener1("created item", false); |
| 144 | LoadAndLaunchPlatformApp("context_menu"); |
| 145 | |
| 146 | // Wait for the extension to tell us it's created an item. |
| 147 | ASSERT_TRUE(listener1.WaitUntilSatisfied()); |
| 148 | |
[email protected] | 0cfacd8 | 2012-02-09 01:55:33 | [diff] [blame] | 149 | // The context_menu app has one context menu item. This, along with a |
| 150 | // separator and the developer tools, is all that should be in the menu. |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 151 | WebContents* web_contents = GetFirstPlatformAppWebContents(); |
| 152 | ASSERT_TRUE(web_contents); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 153 | WebKit::WebContextMenuData data; |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 154 | content::ContextMenuParams params(data); |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 155 | PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 156 | params); |
| 157 | menu->Init(); |
[email protected] | 40b1300 | 2012-03-06 02:23:15 | [diff] [blame] | 158 | ASSERT_EQ(4, menu->menu_model().GetItemCount()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 159 | } |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 160 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 161 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) { |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 162 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 163 | |
| 164 | LoadAndLaunchPlatformApp("navigation"); |
| 165 | WebContents* web_contents = GetFirstPlatformAppWebContents(); |
| 166 | |
| 167 | GURL remote_url = test_server()->GetURL( |
| 168 | "files/extensions/platform_apps/navigation/nav-target.html"); |
| 169 | |
| 170 | std::string script = StringPrintf( |
| 171 | "runTests(\"%s\")", remote_url.spec().c_str()); |
| 172 | bool result = false; |
| 173 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 174 | web_contents->GetRenderViewHost(), L"", |
| 175 | UTF8ToWide(script), &result)); |
| 176 | EXPECT_TRUE(result); |
| 177 | } |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 178 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 179 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowModalDialogs) { |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 180 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/modal_dialogs")) << message_; |
| 181 | } |
[email protected] | 2aac7ff | 2012-01-25 18:05:11 | [diff] [blame] | 182 | |
| 183 | // Tests that localStorage and WebSQL are disabled for platform apps. |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 184 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) { |
[email protected] | 2aac7ff | 2012-01-25 18:05:11 | [diff] [blame] | 185 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_; |
| 186 | } |
[email protected] | 6a5a2e5 | 2012-03-22 03:21:12 | [diff] [blame^] | 187 | |
| 188 | // Tests that platform apps can use the chrome.windows.* API. |
| 189 | #if defined(USE_AURA) |
| 190 | // On Aura, this currently fails because the window width is returned as 256 |
| 191 | // instead of 250. See http://crbug.com/119410. |
| 192 | #define MAYBE_WindowsApi FAILS_WindowsApi |
| 193 | #else |
| 194 | #define MAYBE_WindowsApi WindowsApi |
| 195 | #endif |
| 196 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_WindowsApi) { |
| 197 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_; |
| 198 | } |