blob: aab74f883da89e4eb15fb8053be5d3adb01992d7 [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]605fb8102012-05-04 01:36:558#include "chrome/browser/automation/automation_util.h"
[email protected]863e6472012-01-24 19:33:589#include "chrome/browser/extensions/extension_apitest.h"
[email protected]cfc6cca2011-12-01 02:30:0610#include "chrome/browser/extensions/extension_browsertest.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]d72d3a62012-05-10 03:45:0813#include "chrome/browser/extensions/shell_window_registry.h"
[email protected]cfc6cca2011-12-01 02:30:0614#include "chrome/browser/profiles/profile.h"
[email protected]375003a2011-12-13 02:53:2115#include "chrome/browser/tab_contents/render_view_context_menu.h"
[email protected]cfc6cca2011-12-01 02:30:0616#include "chrome/browser/ui/browser.h"
17#include "chrome/browser/ui/browser_list.h"
[email protected]d72d3a62012-05-10 03:45:0818#include "chrome/browser/ui/extensions/shell_window.h"
[email protected]cfc6cca2011-12-01 02:30:0619#include "chrome/common/chrome_switches.h"
[email protected]cfc6cca2011-12-01 02:30:0620#include "chrome/common/extensions/extension_constants.h"
21#include "chrome/test/base/ui_test_utils.h"
[email protected]5f000f272012-01-19 05:25:0822#include "content/public/browser/web_contents.h"
[email protected]35be7ec2012-02-12 20:42:5123#include "content/public/common/context_menu_params.h"
[email protected]cfc6cca2011-12-01 02:30:0624#include "testing/gtest/include/gtest/gtest.h"
[email protected]375003a2011-12-13 02:53:2125#include "ui/base/models/menu_model.h"
[email protected]cfc6cca2011-12-01 02:30:0626
[email protected]bb81f382012-01-03 22:45:4427using content::WebContents;
28
[email protected]375003a2011-12-13 02:53:2129namespace {
30// Non-abstract RenderViewContextMenu class.
31class PlatformAppContextMenu : public RenderViewContextMenu {
32 public:
[email protected]bb81f382012-01-03 22:45:4433 PlatformAppContextMenu(WebContents* web_contents,
[email protected]35be7ec2012-02-12 20:42:5134 const content::ContextMenuParams& params)
[email protected]bb81f382012-01-03 22:45:4435 : RenderViewContextMenu(web_contents, params) {}
[email protected]375003a2011-12-13 02:53:2136
37 protected:
38 // These two functions implement pure virtual methods of
39 // RenderViewContextMenu.
40 virtual bool GetAcceleratorForCommandId(int command_id,
41 ui::Accelerator* accelerator) {
42 return false;
43 }
44 virtual void PlatformInit() {}
[email protected]f5d5cd02012-05-04 13:59:3045 virtual void PlatformCancel() {}
[email protected]375003a2011-12-13 02:53:2146};
47
48} // namespace
49
[email protected]863e6472012-01-24 19:33:5850class PlatformAppBrowserTest : public ExtensionApiTest {
[email protected]cfc6cca2011-12-01 02:30:0651 public:
52 virtual void SetUpCommandLine(CommandLine* command_line) {
53 ExtensionBrowserTest::SetUpCommandLine(command_line);
54 command_line->AppendSwitch(switches::kEnablePlatformApps);
[email protected]dc37b002012-04-23 23:02:2655 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
[email protected]cfc6cca2011-12-01 02:30:0656 }
57
[email protected]a44657202012-01-09 05:48:3158 protected:
[email protected]cfc6cca2011-12-01 02:30:0659 void LoadAndLaunchPlatformApp(const char* name) {
[email protected]939ac752012-01-13 03:00:4760 ui_test_utils::WindowedNotificationObserver app_loaded_observer(
61 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
62 content::NotificationService::AllSources());
63
[email protected]375003a2011-12-13 02:53:2164 EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps").
65 AppendASCII(name)));
[email protected]cfc6cca2011-12-01 02:30:0666
67 ExtensionService* service = browser()->profile()->GetExtensionService();
[email protected]1c321ee2012-05-21 03:02:3468 const extensions::Extension* extension = service->GetExtensionById(
[email protected]cfc6cca2011-12-01 02:30:0669 last_loaded_extension_id_, false);
70 EXPECT_TRUE(extension);
71
[email protected]cfc6cca2011-12-01 02:30:0672 Browser::OpenApplication(
73 browser()->profile(),
74 extension,
[email protected]dc37b002012-04-23 23:02:2675 extension_misc::LAUNCH_NONE,
[email protected]cfc6cca2011-12-01 02:30:0676 GURL(),
77 NEW_WINDOW);
78
[email protected]939ac752012-01-13 03:00:4779 app_loaded_observer.Wait();
[email protected]a44657202012-01-09 05:48:3180 }
81
[email protected]d72d3a62012-05-10 03:45:0882 // Gets the WebContents associated with the first shell window that is found
83 // (most tests only deal with one platform app window, so this is good
84 // enough).
85 WebContents* GetFirstShellWindowWebContents() {
86 ShellWindowRegistry* app_registry =
87 ShellWindowRegistry::Get(browser()->profile());
88 ShellWindowRegistry::const_iterator iter;
89 ShellWindowRegistry::ShellWindowSet shell_windows =
90 app_registry->shell_windows();
91 for (iter = shell_windows.begin(); iter != shell_windows.end(); ++iter) {
92 return (*iter)->web_contents();
[email protected]a44657202012-01-09 05:48:3193 }
94
95 return NULL;
[email protected]cfc6cca2011-12-01 02:30:0696 }
97};
98
[email protected]dc37b002012-04-23 23:02:2699// Tests that platform apps received the "launch" event when launched.
100IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) {
101 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_;
[email protected]cfc6cca2011-12-01 02:30:06102}
[email protected]375003a2011-12-13 02:53:21103
[email protected]edd7ed692012-02-08 02:50:03104IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
[email protected]dc37b002012-04-23 23:02:26105 ExtensionTestMessageListener launched_listener("Launched", false);
106 LoadAndLaunchPlatformApp("empty_context_menu");
107
108 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:21109
[email protected]375003a2011-12-13 02:53:21110 // The empty app doesn't add any context menu items, so its menu should
[email protected]0cfacd82012-02-09 01:55:33111 // only include the developer tools.
[email protected]d72d3a62012-05-10 03:45:08112 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:31113 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:21114 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:51115 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:44116 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:21117 params);
118 menu->Init();
[email protected]d72d3a62012-05-10 03:45:08119 // TODO(benwells): Remove the constant below. Instead of checking the
120 // number of menu items check certain item's absense and presence.
[email protected]40b13002012-03-06 02:23:15121 // 3 including separator
122 ASSERT_EQ(3, menu->menu_model().GetItemCount());
[email protected]375003a2011-12-13 02:53:21123}
124
[email protected]edd7ed692012-02-08 02:50:03125IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) {
[email protected]dc37b002012-04-23 23:02:26126 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]375003a2011-12-13 02:53:21127 LoadAndLaunchPlatformApp("context_menu");
128
[email protected]dc37b002012-04-23 23:02:26129 // Wait for the extension to tell us it's initialized its context menus and
130 // launched a window.
131 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:21132
[email protected]0cfacd82012-02-09 01:55:33133 // The context_menu app has one context menu item. This, along with a
134 // separator and the developer tools, is all that should be in the menu.
[email protected]d72d3a62012-05-10 03:45:08135 WebContents* web_contents = GetFirstShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:31136 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:21137 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:51138 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:44139 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:21140 params);
141 menu->Init();
[email protected]d72d3a62012-05-10 03:45:08142 // TODO(benwells): Remove the constant below. Instead of checking the
143 // number of menu items check certain item's absense and presence.
[email protected]40b13002012-03-06 02:23:15144 ASSERT_EQ(4, menu->menu_model().GetItemCount());
[email protected]375003a2011-12-13 02:53:21145}
[email protected]5f000f272012-01-19 05:25:08146
[email protected]edd7ed692012-02-08 02:50:03147IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) {
[email protected]dc37b002012-04-23 23:02:26148 ASSERT_TRUE(StartTestServer());
149 ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_;
[email protected]5f000f272012-01-19 05:25:08150}
[email protected]863e6472012-01-24 19:33:58151
[email protected]fd3238af2012-05-22 18:55:30152IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Iframes) {
153 ASSERT_TRUE(StartTestServer());
154 ASSERT_TRUE(RunPlatformAppTest("platform_apps/iframes")) << message_;
155}
156
[email protected]2aac7ff2012-01-25 18:05:11157// Tests that localStorage and WebSQL are disabled for platform apps.
[email protected]edd7ed692012-02-08 02:50:03158IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) {
[email protected]2aac7ff2012-01-25 18:05:11159 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_;
160}
[email protected]6a5a2e52012-03-22 03:21:12161
[email protected]c0cecd1f2012-04-05 16:50:12162IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) {
163 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_;
164}
165
[email protected]6a5a2e52012-03-22 03:21:12166// Tests that platform apps can use the chrome.windows.* API.
[email protected]dc37b002012-04-23 23:02:26167IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) {
[email protected]6a5a2e52012-03-22 03:21:12168 ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_;
169}
[email protected]605fb8102012-05-04 01:36:55170
171// Tests that platform apps have isolated storage by default.
172IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Isolation) {
173 ASSERT_TRUE(StartTestServer());
174
175 // Load a (non-app) page under the "localhost" origin that sets a cookie.
176 GURL set_cookie_url = test_server()->GetURL(
177 "files/extensions/platform_apps/isolation/set_cookie.html");
178 GURL::Replacements replace_host;
179 std::string host_str("localhost"); // Must stay in scope with replace_host.
180 replace_host.SetHostStr(host_str);
181 set_cookie_url = set_cookie_url.ReplaceComponents(replace_host);
182
183 ui_test_utils::NavigateToURLWithDisposition(
184 browser(), set_cookie_url,
185 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
186
187 // Make sure the cookie is set.
188 int cookie_size;
189 std::string cookie_value;
190 automation_util::GetCookies(
191 set_cookie_url,
192 browser()->GetWebContentsAt(0),
193 &cookie_size,
194 &cookie_value);
195 ASSERT_EQ("testCookie=1", cookie_value);
196
197 // Let the platform app request the same URL, and make sure that it doesn't
198 // see the cookie.
199 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_;
200}