[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" |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame^] | 18 | #include "chrome/common/chrome_view_type.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 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] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 22 | #include "content/public/common/context_menu_params.h" |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 23 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 24 | #include "ui/base/models/menu_model.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, |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 33 | const content::ContextMenuParams& params) |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 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); |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame^] | 53 | command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 56 | protected: |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 57 | void LoadAndLaunchPlatformApp(const char* name) { |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame^] | 58 | size_t platform_app_count = GetPlatformAppCount(); |
[email protected] | 939ac75 | 2012-01-13 03:00:47 | [diff] [blame] | 59 | ui_test_utils::WindowedNotificationObserver app_loaded_observer( |
| 60 | content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 61 | content::NotificationService::AllSources()); |
| 62 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 63 | EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps"). |
| 64 | AppendASCII(name))); |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 65 | |
| 66 | ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 67 | const Extension* extension = service->GetExtensionById( |
| 68 | last_loaded_extension_id_, false); |
| 69 | EXPECT_TRUE(extension); |
| 70 | |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 71 | Browser::OpenApplication( |
| 72 | browser()->profile(), |
| 73 | extension, |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame^] | 74 | extension_misc::LAUNCH_NONE, |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 75 | GURL(), |
| 76 | NEW_WINDOW); |
| 77 | |
[email protected] | 939ac75 | 2012-01-13 03:00:47 | [diff] [blame] | 78 | app_loaded_observer.Wait(); |
| 79 | |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 80 | // Now we have a new platform app running. |
| 81 | EXPECT_EQ(platform_app_count + 1, GetPlatformAppCount()); |
| 82 | } |
| 83 | |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame^] | 84 | // Gets the number of platform apps extension hosts that are running. |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 85 | size_t GetPlatformAppCount() { |
| 86 | int count = 0; |
| 87 | ExtensionProcessManager* process_manager = |
| 88 | browser()->profile()->GetExtensionProcessManager(); |
| 89 | ExtensionProcessManager::const_iterator iter; |
| 90 | for (iter = process_manager->begin(); iter != process_manager->end(); |
| 91 | ++iter) { |
| 92 | ExtensionHost* host = *iter; |
| 93 | if (host->extension() && host->extension()->is_platform_app()) |
| 94 | count++; |
| 95 | } |
| 96 | |
| 97 | return count; |
| 98 | } |
| 99 | |
| 100 | // Gets the WebContents associated with the ExtensionHost of the first |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame^] | 101 | // platform app shell window that is found (most tests only deal with one |
| 102 | // platform app window, so this is good enough). |
| 103 | WebContents* GetFirstPlatformAppShellWindowWebContents() { |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 104 | ExtensionProcessManager* process_manager = |
| 105 | browser()->profile()->GetExtensionProcessManager(); |
| 106 | ExtensionProcessManager::const_iterator iter; |
| 107 | for (iter = process_manager->begin(); iter != process_manager->end(); |
| 108 | ++iter) { |
| 109 | ExtensionHost* host = *iter; |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame^] | 110 | if (host->extension() && host->extension()->is_platform_app() && |
| 111 | host->extension_host_type() == chrome::VIEW_TYPE_APP_SHELL) |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 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] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame^] | 119 | // Tests that platform apps received the "launch" event when launched. |
| 120 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) { |
| 121 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_; |
[email protected] | cfc6cca | 2011-12-01 02:30:06 | [diff] [blame] | 122 | } |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 123 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 124 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) { |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame^] | 125 | ExtensionTestMessageListener launched_listener("Launched", false); |
| 126 | LoadAndLaunchPlatformApp("empty_context_menu"); |
| 127 | |
| 128 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 129 | |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 130 | // 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] | 131 | // only include the developer tools. |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame^] | 132 | WebContents* web_contents = GetFirstPlatformAppShellWindowWebContents(); |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 133 | ASSERT_TRUE(web_contents); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 134 | WebKit::WebContextMenuData data; |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 135 | content::ContextMenuParams params(data); |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 136 | PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 137 | params); |
| 138 | menu->Init(); |
[email protected] | 40b1300 | 2012-03-06 02:23:15 | [diff] [blame] | 139 | // 3 including separator |
| 140 | ASSERT_EQ(3, menu->menu_model().GetItemCount()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 143 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) { |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame^] | 144 | ExtensionTestMessageListener launched_listener("Launched", false); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 145 | LoadAndLaunchPlatformApp("context_menu"); |
| 146 | |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame^] | 147 | // Wait for the extension to tell us it's initialized its context menus and |
| 148 | // launched a window. |
| 149 | ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 150 | |
[email protected] | 0cfacd8 | 2012-02-09 01:55:33 | [diff] [blame] | 151 | // The context_menu app has one context menu item. This, along with a |
| 152 | // separator and the developer tools, is all that should be in the menu. |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame^] | 153 | WebContents* web_contents = GetFirstPlatformAppShellWindowWebContents(); |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 154 | ASSERT_TRUE(web_contents); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 155 | WebKit::WebContextMenuData data; |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 156 | content::ContextMenuParams params(data); |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 157 | PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 158 | params); |
| 159 | menu->Init(); |
[email protected] | 40b1300 | 2012-03-06 02:23:15 | [diff] [blame] | 160 | ASSERT_EQ(4, menu->menu_model().GetItemCount()); |
[email protected] | 375003a | 2011-12-13 02:53:21 | [diff] [blame] | 161 | } |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 162 | |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 163 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) { |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame^] | 164 | ASSERT_TRUE(StartTestServer()); |
| 165 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_; |
[email protected] | 5f000f27 | 2012-01-19 05:25:08 | [diff] [blame] | 166 | } |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 167 | |
[email protected] | 2aac7ff | 2012-01-25 18:05:11 | [diff] [blame] | 168 | // Tests that localStorage and WebSQL are disabled for platform apps. |
[email protected] | edd7ed69 | 2012-02-08 02:50:03 | [diff] [blame] | 169 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) { |
[email protected] | 2aac7ff | 2012-01-25 18:05:11 | [diff] [blame] | 170 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_; |
| 171 | } |
[email protected] | 6a5a2e5 | 2012-03-22 03:21:12 | [diff] [blame] | 172 | |
[email protected] | c0cecd1f | 2012-04-05 16:50:12 | [diff] [blame] | 173 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) { |
| 174 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_; |
| 175 | } |
| 176 | |
[email protected] | 6a5a2e5 | 2012-03-22 03:21:12 | [diff] [blame] | 177 | // Tests that platform apps can use the chrome.windows.* API. |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame^] | 178 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) { |
[email protected] | 6a5a2e5 | 2012-03-22 03:21:12 | [diff] [blame] | 179 | ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_; |
| 180 | } |