blob: f0354e72d6f7d0a05334a5bb1ec1a7096085224b [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"
[email protected]dc37b002012-04-23 23:02:2618#include "chrome/common/chrome_view_type.h"
[email protected]cfc6cca2011-12-01 02:30:0619#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]35be7ec2012-02-12 20:42:5122#include "content/public/common/context_menu_params.h"
[email protected]cfc6cca2011-12-01 02:30:0623#include "testing/gtest/include/gtest/gtest.h"
[email protected]375003a2011-12-13 02:53:2124#include "ui/base/models/menu_model.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,
[email protected]35be7ec2012-02-12 20:42:5133 const content::ContextMenuParams& params)
[email protected]bb81f382012-01-03 22:45:4434 : 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);
[email protected]dc37b002012-04-23 23:02:2653 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
[email protected]cfc6cca2011-12-01 02:30:0654 }
55
[email protected]a44657202012-01-09 05:48:3156 protected:
[email protected]cfc6cca2011-12-01 02:30:0657 void LoadAndLaunchPlatformApp(const char* name) {
[email protected]dc37b002012-04-23 23:02:2658 size_t platform_app_count = GetPlatformAppCount();
[email protected]939ac752012-01-13 03:00:4759 ui_test_utils::WindowedNotificationObserver app_loaded_observer(
60 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
61 content::NotificationService::AllSources());
62
[email protected]375003a2011-12-13 02:53:2163 EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps").
64 AppendASCII(name)));
[email protected]cfc6cca2011-12-01 02:30:0665
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]cfc6cca2011-12-01 02:30:0671 Browser::OpenApplication(
72 browser()->profile(),
73 extension,
[email protected]dc37b002012-04-23 23:02:2674 extension_misc::LAUNCH_NONE,
[email protected]cfc6cca2011-12-01 02:30:0675 GURL(),
76 NEW_WINDOW);
77
[email protected]939ac752012-01-13 03:00:4778 app_loaded_observer.Wait();
79
[email protected]a44657202012-01-09 05:48:3180 // Now we have a new platform app running.
81 EXPECT_EQ(platform_app_count + 1, GetPlatformAppCount());
82 }
83
[email protected]dc37b002012-04-23 23:02:2684 // Gets the number of platform apps extension hosts that are running.
[email protected]a44657202012-01-09 05:48:3185 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]dc37b002012-04-23 23:02:26101 // 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]a44657202012-01-09 05:48:31104 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]dc37b002012-04-23 23:02:26110 if (host->extension() && host->extension()->is_platform_app() &&
111 host->extension_host_type() == chrome::VIEW_TYPE_APP_SHELL)
[email protected]a44657202012-01-09 05:48:31112 return host->host_contents();
113 }
114
115 return NULL;
[email protected]cfc6cca2011-12-01 02:30:06116 }
117};
118
[email protected]dc37b002012-04-23 23:02:26119// Tests that platform apps received the "launch" event when launched.
120IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) {
121 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_;
[email protected]cfc6cca2011-12-01 02:30:06122}
[email protected]375003a2011-12-13 02:53:21123
[email protected]edd7ed692012-02-08 02:50:03124IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
[email protected]dc37b002012-04-23 23:02:26125 ExtensionTestMessageListener launched_listener("Launched", false);
126 LoadAndLaunchPlatformApp("empty_context_menu");
127
128 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
[email protected]375003a2011-12-13 02:53:21129
[email protected]375003a2011-12-13 02:53:21130 // The empty app doesn't add any context menu items, so its menu should
[email protected]0cfacd82012-02-09 01:55:33131 // only include the developer tools.
[email protected]dc37b002012-04-23 23:02:26132 WebContents* web_contents = GetFirstPlatformAppShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:31133 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:21134 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:51135 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:44136 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:21137 params);
138 menu->Init();
[email protected]40b13002012-03-06 02:23:15139 // 3 including separator
140 ASSERT_EQ(3, menu->menu_model().GetItemCount());
[email protected]375003a2011-12-13 02:53:21141}
142
[email protected]edd7ed692012-02-08 02:50:03143IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) {
[email protected]dc37b002012-04-23 23:02:26144 ExtensionTestMessageListener launched_listener("Launched", false);
[email protected]375003a2011-12-13 02:53:21145 LoadAndLaunchPlatformApp("context_menu");
146
[email protected]dc37b002012-04-23 23:02:26147 // 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]375003a2011-12-13 02:53:21150
[email protected]0cfacd82012-02-09 01:55:33151 // 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]dc37b002012-04-23 23:02:26153 WebContents* web_contents = GetFirstPlatformAppShellWindowWebContents();
[email protected]a44657202012-01-09 05:48:31154 ASSERT_TRUE(web_contents);
[email protected]375003a2011-12-13 02:53:21155 WebKit::WebContextMenuData data;
[email protected]35be7ec2012-02-12 20:42:51156 content::ContextMenuParams params(data);
[email protected]bb81f382012-01-03 22:45:44157 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
[email protected]375003a2011-12-13 02:53:21158 params);
159 menu->Init();
[email protected]40b13002012-03-06 02:23:15160 ASSERT_EQ(4, menu->menu_model().GetItemCount());
[email protected]375003a2011-12-13 02:53:21161}
[email protected]5f000f272012-01-19 05:25:08162
[email protected]edd7ed692012-02-08 02:50:03163IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) {
[email protected]dc37b002012-04-23 23:02:26164 ASSERT_TRUE(StartTestServer());
165 ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_;
[email protected]5f000f272012-01-19 05:25:08166}
[email protected]863e6472012-01-24 19:33:58167
[email protected]2aac7ff2012-01-25 18:05:11168// Tests that localStorage and WebSQL are disabled for platform apps.
[email protected]edd7ed692012-02-08 02:50:03169IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) {
[email protected]2aac7ff2012-01-25 18:05:11170 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_;
171}
[email protected]6a5a2e52012-03-22 03:21:12172
[email protected]c0cecd1f2012-04-05 16:50:12173IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) {
174 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_;
175}
176
[email protected]6a5a2e52012-03-22 03:21:12177// Tests that platform apps can use the chrome.windows.* API.
[email protected]dc37b002012-04-23 23:02:26178IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) {
[email protected]6a5a2e52012-03-22 03:21:12179 ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_;
180}