blob: abde2eedc0e880594e1b4738ff8b590bbd7cf26c [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"
[email protected]8588026d2011-12-14 15:59:0217#include "chrome/browser/web_applications/web_app.h"
[email protected]cfc6cca2011-12-01 02:30:0618#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]5f000f272012-01-19 05:25:0821#include "content/public/browser/web_contents.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"
24#include "webkit/glue/context_menu.h"
[email protected]cfc6cca2011-12-01 02:30:0625
[email protected]bb81f382012-01-03 22:45:4426using content::WebContents;
27
[email protected]375003a2011-12-13 02:53:2128namespace {
29// Non-abstract RenderViewContextMenu class.
30class PlatformAppContextMenu : public RenderViewContextMenu {
31 public:
[email protected]bb81f382012-01-03 22:45:4432 PlatformAppContextMenu(WebContents* web_contents,
33 const ContextMenuParams& params)
34 : RenderViewContextMenu(web_contents, params) {}
[email protected]375003a2011-12-13 02:53:2135
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]863e6472012-01-24 19:33:5848class PlatformAppBrowserTest : public ExtensionApiTest {
[email protected]cfc6cca2011-12-01 02:30:0649 public:
50 virtual void SetUpCommandLine(CommandLine* command_line) {
51 ExtensionBrowserTest::SetUpCommandLine(command_line);
52 command_line->AppendSwitch(switches::kEnablePlatformApps);
53 }
54
[email protected]a44657202012-01-09 05:48:3155 protected:
[email protected]cfc6cca2011-12-01 02:30:0656 void LoadAndLaunchPlatformApp(const char* name) {
[email protected]939ac752012-01-13 03:00:4757 ui_test_utils::WindowedNotificationObserver app_loaded_observer(
58 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
59 content::NotificationService::AllSources());
60
[email protected]8588026d2011-12-14 15:59:0261 web_app::SetDisableShortcutCreationForTests(true);
[email protected]375003a2011-12-13 02:53:2162 EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps").
63 AppendASCII(name)));
[email protected]cfc6cca2011-12-01 02:30:0664
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]a44657202012-01-09 05:48:3170 size_t platform_app_count = GetPlatformAppCount();
[email protected]cfc6cca2011-12-01 02:30:0671
72 Browser::OpenApplication(
73 browser()->profile(),
74 extension,
75 extension_misc::LAUNCH_SHELL,
76 GURL(),
77 NEW_WINDOW);
78
[email protected]939ac752012-01-13 03:00:4779 app_loaded_observer.Wait();
80
[email protected]a44657202012-01-09 05:48:3181 // 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]cfc6cca2011-12-01 02:30:06116 }
117};
118
[email protected]939ac752012-01-13 03:00:47119// Disabled until shell windows are implemented for non-GTK, non-Views toolkits.
120#if defined(TOOLKIT_GTK) || defined(TOOLKIT_VIEWS)
[email protected]a44657202012-01-09 05:48:31121#define MAYBE_OpenAppInShellContainer OpenAppInShellContainer
122#else
123#define MAYBE_OpenAppInShellContainer DISABLED_OpenAppInShellContainer
124#endif
125IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_OpenAppInShellContainer) {
126 ASSERT_EQ(0u, GetPlatformAppCount());
[email protected]375003a2011-12-13 02:53:21127 LoadAndLaunchPlatformApp("empty");
[email protected]a44657202012-01-09 05:48:31128 ASSERT_EQ(1u, GetPlatformAppCount());
[email protected]a6080c02012-01-11 23:33:49129
130 UnloadExtension(last_loaded_extension_id_);
131 ASSERT_EQ(0u, GetPlatformAppCount());
[email protected]cfc6cca2011-12-01 02:30:06132}
[email protected]375003a2011-12-13 02:53:21133
[email protected]939ac752012-01-13 03:00:47134// Disabled until shell windows are implemented for non-GTK, non-Views toolkits.
135#if defined(TOOLKIT_GTK) || defined(TOOLKIT_VIEWS)
[email protected]a6080c02012-01-11 23:33:49136#define MAYBE_EmptyContextMenu EmptyContextMenu
[email protected]a44657202012-01-09 05:48:31137#else
138#define MAYBE_EmptyContextMenu DISABLED_EmptyContextMenu
139#endif
140IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_EmptyContextMenu) {
[email protected]375003a2011-12-13 02:53:21141 LoadAndLaunchPlatformApp("empty");
142
[email protected]375003a2011-12-13 02:53:21143 // The empty app doesn't add any context menu items, so its menu should
144 // be empty.
[email protected]a44657202012-01-09 05:48:31145 WebContents* web_contents = GetFirstPlatformAppWebContents();
146 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:21147 WebKit::WebContextMenuData data;
148 ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:44149 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:21150 params);
151 menu->Init();
152 ASSERT_FALSE(menu->menu_model().GetItemCount());
153}
154
[email protected]939ac752012-01-13 03:00:47155// Disabled until shell windows are implemented for non-GTK, non-Views toolkits.
156#if defined(TOOLKIT_GTK) || defined(TOOLKIT_VIEWS)
[email protected]a44657202012-01-09 05:48:31157#define MAYBE_AppWithContextMenu AppWithContextMenu
158#else
159#define MAYBE_AppWithContextMenu DISABLED_AppWithContextMenu
160#endif
161IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_AppWithContextMenu) {
[email protected]375003a2011-12-13 02:53:21162 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]375003a2011-12-13 02:53:21168 // 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]a44657202012-01-09 05:48:31170 WebContents* web_contents = GetFirstPlatformAppWebContents();
171 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:21172 WebKit::WebContextMenuData data;
173 ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:44174 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:21175 params);
176 menu->Init();
177 ASSERT_EQ(1, menu->menu_model().GetItemCount());
178}
[email protected]5f000f272012-01-19 05:25:08179
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
186IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_DisallowNavigation) {
[email protected]863e6472012-01-24 19:33:58187 ASSERT_TRUE(test_server()->Start());
[email protected]5f000f272012-01-19 05:25:08188
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]863e6472012-01-24 19:33:58203
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
210IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_DisallowModalDialogs) {
211 ASSERT_TRUE(RunPlatformAppTest("platform_apps/modal_dialogs")) << message_;
212}
[email protected]2aac7ff2012-01-25 18:05:11213
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
221IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_DisallowStorage) {
222 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_;
223}