[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" |
[email protected] | 8588026d | 2011-12-14 15:59:02 | [diff] [blame] | 17 | #include "chrome/browser/web_applications/web_app.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 18 | #include "chrome/common/chrome_switches.h" |
| 19 | #include "chrome/common/extensions/extension_constants.h" |
| 20 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 21 | #include "content/public/browser/web_contents.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" |
| 24 | #include "webkit/glue/context_menu.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 25 | |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 26 | using content::WebContents; |
| 27 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 28 | namespace { |
| 29 | // Non-abstract RenderViewContextMenu class. |
| 30 | class PlatformAppContextMenu : public RenderViewContextMenu { |
| 31 | public: |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 32 | PlatformAppContextMenu(WebContents* web_contents, |
| 33 | const ContextMenuParams& params) |
| 34 | : RenderViewContextMenu(web_contents, params) {} |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 35 | |
| 36 | protected: |
| 37 | // These two functions implement pure virtual methods of |
| 38 | // RenderViewContextMenu. |
| 39 | virtual bool GetAcceleratorForCommandId(int command_id, |
| 40 | ui::Accelerator* accelerator) { |
| 41 | return false; |
| 42 | } |
| 43 | virtual void PlatformInit() {} |
| 44 | }; |
| 45 | |
| 46 | } // namespace |
| 47 | |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 48 | class PlatformAppBrowserTest : public ExtensionApiTest { |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 49 | public: |
| 50 | virtual void SetUpCommandLine(CommandLine* command_line) { |
| 51 | ExtensionBrowserTest::SetUpCommandLine(command_line); |
| 52 | command_line->AppendSwitch(switches::kEnablePlatformApps); |
| 53 | } |
| 54 | |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 55 | protected: |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 56 | void LoadAndLaunchPlatformApp(const char* name) { |
[email protected] | 939ac75 | 2012-01-13 03:00:47 | [diff] [blame] | 57 | ui_test_utils::WindowedNotificationObserver app_loaded_observer( |
| 58 | content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 59 | content::NotificationService::AllSources()); |
| 60 | |
[email protected] | 8588026d | 2011-12-14 15:59:02 | [diff] [blame] | 61 | web_app::SetDisableShortcutCreationForTests(true); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 62 | EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps"). |
| 63 | AppendASCII(name))); |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 64 | |
| 65 | ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 66 | const Extension* extension = service->GetExtensionById( |
| 67 | last_loaded_extension_id_, false); |
| 68 | EXPECT_TRUE(extension); |
| 69 | |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 70 | size_t platform_app_count = GetPlatformAppCount(); |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 71 | |
| 72 | Browser::OpenApplication( |
| 73 | browser()->profile(), |
| 74 | extension, |
| 75 | extension_misc::LAUNCH_SHELL, |
| 76 | GURL(), |
| 77 | NEW_WINDOW); |
| 78 | |
[email protected] | 939ac75 | 2012-01-13 03:00:47 | [diff] [blame] | 79 | app_loaded_observer.Wait(); |
| 80 | |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 81 | // Now we have a new platform app running. |
| 82 | EXPECT_EQ(platform_app_count + 1, GetPlatformAppCount()); |
| 83 | } |
| 84 | |
| 85 | // Gets the number of platform apps that are running. |
| 86 | size_t GetPlatformAppCount() { |
| 87 | int count = 0; |
| 88 | ExtensionProcessManager* process_manager = |
| 89 | browser()->profile()->GetExtensionProcessManager(); |
| 90 | ExtensionProcessManager::const_iterator iter; |
| 91 | for (iter = process_manager->begin(); iter != process_manager->end(); |
| 92 | ++iter) { |
| 93 | ExtensionHost* host = *iter; |
| 94 | if (host->extension() && host->extension()->is_platform_app()) |
| 95 | count++; |
| 96 | } |
| 97 | |
| 98 | return count; |
| 99 | } |
| 100 | |
| 101 | // Gets the WebContents associated with the ExtensionHost of the first |
| 102 | // platform app that is found (most tests only deal with one platform |
| 103 | // app, so this is good enough). |
| 104 | WebContents* GetFirstPlatformAppWebContents() { |
| 105 | ExtensionProcessManager* process_manager = |
| 106 | browser()->profile()->GetExtensionProcessManager(); |
| 107 | ExtensionProcessManager::const_iterator iter; |
| 108 | for (iter = process_manager->begin(); iter != process_manager->end(); |
| 109 | ++iter) { |
| 110 | ExtensionHost* host = *iter; |
| 111 | if (host->extension() && host->extension()->is_platform_app()) |
| 112 | return host->host_contents(); |
| 113 | } |
| 114 | |
| 115 | return NULL; |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 116 | } |
| 117 | }; |
| 118 | |
[email protected] | 939ac75 | 2012-01-13 03:00:47 | [diff] [blame] | 119 | // Disabled until shell windows are implemented for non-GTK, non-Views toolkits. |
| 120 | #if defined(TOOLKIT_GTK) || defined(TOOLKIT_VIEWS) |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 121 | #define MAYBE_OpenAppInShellContainer OpenAppInShellContainer |
| 122 | #else |
| 123 | #define MAYBE_OpenAppInShellContainer DISABLED_OpenAppInShellContainer |
| 124 | #endif |
| 125 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_OpenAppInShellContainer) { |
| 126 | ASSERT_EQ(0u, GetPlatformAppCount()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 127 | LoadAndLaunchPlatformApp("empty"); |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 128 | ASSERT_EQ(1u, GetPlatformAppCount()); |
[email protected] | a6080c0 | 2012-01-11 23:33:49 | [diff] [blame] | 129 | |
| 130 | UnloadExtension(last_loaded_extension_id_); |
| 131 | ASSERT_EQ(0u, GetPlatformAppCount()); |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 132 | } |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 133 | |
[email protected] | 939ac75 | 2012-01-13 03:00:47 | [diff] [blame] | 134 | // Disabled until shell windows are implemented for non-GTK, non-Views toolkits. |
| 135 | #if defined(TOOLKIT_GTK) || defined(TOOLKIT_VIEWS) |
[email protected] | a6080c0 | 2012-01-11 23:33:49 | [diff] [blame] | 136 | #define MAYBE_EmptyContextMenu EmptyContextMenu |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 137 | #else |
| 138 | #define MAYBE_EmptyContextMenu DISABLED_EmptyContextMenu |
| 139 | #endif |
| 140 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_EmptyContextMenu) { |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 141 | LoadAndLaunchPlatformApp("empty"); |
| 142 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 143 | // The empty app doesn't add any context menu items, so its menu should |
| 144 | // be empty. |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 145 | WebContents* web_contents = GetFirstPlatformAppWebContents(); |
| 146 | ASSERT_TRUE(web_contents); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 147 | WebKit::WebContextMenuData data; |
| 148 | ContextMenuParams params(data); |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 149 | PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 150 | params); |
| 151 | menu->Init(); |
| 152 | ASSERT_FALSE(menu->menu_model().GetItemCount()); |
| 153 | } |
| 154 | |
[email protected] | 939ac75 | 2012-01-13 03:00:47 | [diff] [blame] | 155 | // Disabled until shell windows are implemented for non-GTK, non-Views toolkits. |
| 156 | #if defined(TOOLKIT_GTK) || defined(TOOLKIT_VIEWS) |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 157 | #define MAYBE_AppWithContextMenu AppWithContextMenu |
| 158 | #else |
| 159 | #define MAYBE_AppWithContextMenu DISABLED_AppWithContextMenu |
| 160 | #endif |
| 161 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_AppWithContextMenu) { |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 162 | ExtensionTestMessageListener listener1("created item", false); |
| 163 | LoadAndLaunchPlatformApp("context_menu"); |
| 164 | |
| 165 | // Wait for the extension to tell us it's created an item. |
| 166 | ASSERT_TRUE(listener1.WaitUntilSatisfied()); |
| 167 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 168 | // The context_menu app has one context menu item. This is all that should |
| 169 | // be in the menu, there should be no seperator. |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 170 | WebContents* web_contents = GetFirstPlatformAppWebContents(); |
| 171 | ASSERT_TRUE(web_contents); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 172 | WebKit::WebContextMenuData data; |
| 173 | ContextMenuParams params(data); |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 174 | PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 175 | params); |
| 176 | menu->Init(); |
| 177 | ASSERT_EQ(1, menu->menu_model().GetItemCount()); |
| 178 | } |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 179 | |
| 180 | // Disabled until shell windows are implemented for non-GTK, non-Views toolkits. |
| 181 | #if defined(TOOLKIT_GTK) || defined(TOOLKIT_VIEWS) |
| 182 | #define MAYBE_DisallowNavigation DisallowNavigation |
| 183 | #else |
| 184 | #define MAYBE_DisallowNavigation DISABLED_DisallowNavigation |
| 185 | #endif |
| 186 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_DisallowNavigation) { |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 187 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 188 | |
| 189 | LoadAndLaunchPlatformApp("navigation"); |
| 190 | WebContents* web_contents = GetFirstPlatformAppWebContents(); |
| 191 | |
| 192 | GURL remote_url = test_server()->GetURL( |
| 193 | "files/extensions/platform_apps/navigation/nav-target.html"); |
| 194 | |
| 195 | std::string script = StringPrintf( |
| 196 | "runTests(\"%s\")", remote_url.spec().c_str()); |
| 197 | bool result = false; |
| 198 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 199 | web_contents->GetRenderViewHost(), L"", |
| 200 | UTF8ToWide(script), &result)); |
| 201 | EXPECT_TRUE(result); |
| 202 | } |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 203 | |
| 204 | // Disabled until shell windows are implemented for non-GTK, non-Views toolkits. |
| 205 | #if defined(TOOLKIT_GTK) || defined(TOOLKIT_VIEWS) |
| 206 | #define MAYBE_DisallowModalDialogs DisallowModalDialogs |
| 207 | #else |
| 208 | #define MAYBE_DisallowModalDialogs DISABLED_DisallowModalDialogs |
| 209 | #endif |
| 210 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_DisallowModalDialogs) { |
| 211 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/modal_dialogs")) << message_; |
| 212 | } |
[email protected] | 2aac7ff | 2012-01-25 18:05:11 | [diff] [blame^] | 213 | |
| 214 | // Tests that localStorage and WebSQL are disabled for platform apps. |
| 215 | // Disabled until shell windows are implemented for non-GTK, non-Views toolkits. |
| 216 | #if defined(TOOLKIT_GTK) || defined(TOOLKIT_VIEWS) |
| 217 | #define MAYBE_DisallowStorage DisallowStorage |
| 218 | #else |
| 219 | #define MAYBE_DisallowStorage DISABLED_DisallowStorage |
| 220 | #endif |
| 221 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_DisallowStorage) { |
| 222 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_; |
| 223 | } |