blob: 3aeac39a9f1de56ada32255e07dcb067dd0f81eb [file] [log] [blame]
[email protected]a44657202012-01-09 05:48:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]cfc6cca2011-12-01 02:30:062// 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]5f000f272012-01-19 05:25:086#include "base/stringprintf.h"
7#include "base/utf_string_conversions.h"
[email protected]863e6472012-01-24 19:33:588#include "chrome/browser/extensions/extension_apitest.h"
[email protected]cfc6cca2011-12-01 02:30:069#include "chrome/browser/extensions/extension_browsertest.h"
[email protected]a44657202012-01-09 05:48:3110#include "chrome/browser/extensions/extension_host.h"
[email protected]cfc6cca2011-12-01 02:30:0611#include "chrome/browser/extensions/extension_service.h"
[email protected]375003a2011-12-13 02:53:2112#include "chrome/browser/extensions/extension_test_message_listener.h"
[email protected]cfc6cca2011-12-01 02:30:0613#include "chrome/browser/profiles/profile.h"
[email protected]375003a2011-12-13 02:53:2114#include "chrome/browser/tab_contents/render_view_context_menu.h"
[email protected]cfc6cca2011-12-01 02:30:0615#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]5f000f272012-01-19 05:25:0820#include "content/public/browser/web_contents.h"
[email protected]35be7ec2012-02-12 20:42:5121#include "content/public/common/context_menu_params.h"
[email protected]cfc6cca2011-12-01 02:30:0622#include "testing/gtest/include/gtest/gtest.h"
[email protected]375003a2011-12-13 02:53:2123#include "ui/base/models/menu_model.h"
[email protected]cfc6cca2011-12-01 02:30:0624
[email protected]bb81f382012-01-03 22:45:4425using content::WebContents;
26
[email protected]375003a2011-12-13 02:53:2127namespace {
28// Non-abstract RenderViewContextMenu class.
29class PlatformAppContextMenu : public RenderViewContextMenu {
30 public:
[email protected]bb81f382012-01-03 22:45:4431 PlatformAppContextMenu(WebContents* web_contents,
[email protected]35be7ec2012-02-12 20:42:5132 const content::ContextMenuParams& params)
[email protected]bb81f382012-01-03 22:45:4433 : RenderViewContextMenu(web_contents, params) {}
[email protected]375003a2011-12-13 02:53:2134
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]863e6472012-01-24 19:33:5847class PlatformAppBrowserTest : public ExtensionApiTest {
[email protected]cfc6cca2011-12-01 02:30:0648 public:
49 virtual void SetUpCommandLine(CommandLine* command_line) {
50 ExtensionBrowserTest::SetUpCommandLine(command_line);
51 command_line->AppendSwitch(switches::kEnablePlatformApps);
52 }
53
[email protected]a44657202012-01-09 05:48:3154 protected:
[email protected]cfc6cca2011-12-01 02:30:0655 void LoadAndLaunchPlatformApp(const char* name) {
[email protected]939ac752012-01-13 03:00:4756 ui_test_utils::WindowedNotificationObserver app_loaded_observer(
57 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
58 content::NotificationService::AllSources());
59
[email protected]375003a2011-12-13 02:53:2160 EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps").
61 AppendASCII(name)));
[email protected]cfc6cca2011-12-01 02:30:0662
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]a44657202012-01-09 05:48:3168 size_t platform_app_count = GetPlatformAppCount();
[email protected]cfc6cca2011-12-01 02:30:0669
70 Browser::OpenApplication(
71 browser()->profile(),
72 extension,
73 extension_misc::LAUNCH_SHELL,
74 GURL(),
75 NEW_WINDOW);
76
[email protected]939ac752012-01-13 03:00:4777 app_loaded_observer.Wait();
78
[email protected]a44657202012-01-09 05:48:3179 // 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]cfc6cca2011-12-01 02:30:06114 }
115};
116
[email protected]edd7ed692012-02-08 02:50:03117IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OpenAppInShellContainer) {
[email protected]a44657202012-01-09 05:48:31118 ASSERT_EQ(0u, GetPlatformAppCount());
[email protected]375003a2011-12-13 02:53:21119 LoadAndLaunchPlatformApp("empty");
[email protected]a44657202012-01-09 05:48:31120 ASSERT_EQ(1u, GetPlatformAppCount());
[email protected]a6080c02012-01-11 23:33:49121
122 UnloadExtension(last_loaded_extension_id_);
123 ASSERT_EQ(0u, GetPlatformAppCount());
[email protected]cfc6cca2011-12-01 02:30:06124}
[email protected]375003a2011-12-13 02:53:21125
[email protected]edd7ed692012-02-08 02:50:03126IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
[email protected]375003a2011-12-13 02:53:21127 LoadAndLaunchPlatformApp("empty");
128
[email protected]375003a2011-12-13 02:53:21129 // The empty app doesn't add any context menu items, so its menu should
[email protected]0cfacd82012-02-09 01:55:33130 // only include the developer tools.
[email protected]a44657202012-01-09 05:48:31131 WebContents* web_contents = GetFirstPlatformAppWebContents();
132 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:21133 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:51134 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:44135 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:21136 params);
137 menu->Init();
[email protected]40b13002012-03-06 02:23:15138 // 3 including separator
139 ASSERT_EQ(3, menu->menu_model().GetItemCount());
[email protected]375003a2011-12-13 02:53:21140}
141
[email protected]edd7ed692012-02-08 02:50:03142IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) {
[email protected]375003a2011-12-13 02:53:21143 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]0cfacd82012-02-09 01:55:33149 // 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]a44657202012-01-09 05:48:31151 WebContents* web_contents = GetFirstPlatformAppWebContents();
152 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:21153 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:51154 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:44155 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:21156 params);
157 menu->Init();
[email protected]40b13002012-03-06 02:23:15158 ASSERT_EQ(4, menu->menu_model().GetItemCount());
[email protected]375003a2011-12-13 02:53:21159}
[email protected]5f000f272012-01-19 05:25:08160
[email protected]edd7ed692012-02-08 02:50:03161IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) {
[email protected]863e6472012-01-24 19:33:58162 ASSERT_TRUE(test_server()->Start());
[email protected]5f000f272012-01-19 05:25:08163
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]863e6472012-01-24 19:33:58178
[email protected]edd7ed692012-02-08 02:50:03179IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowModalDialogs) {
[email protected]863e6472012-01-24 19:33:58180 ASSERT_TRUE(RunPlatformAppTest("platform_apps/modal_dialogs")) << message_;
181}
[email protected]2aac7ff2012-01-25 18:05:11182
183// Tests that localStorage and WebSQL are disabled for platform apps.
[email protected]edd7ed692012-02-08 02:50:03184IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) {
[email protected]2aac7ff2012-01-25 18:05:11185 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_;
186}
[email protected]6a5a2e52012-03-22 03:21:12187
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
196IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_WindowsApi) {
197 ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_;
198}