Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: chrome/browser/extensions/platform_app_browsertest.cc

Issue 10407035: Extension/Platform App window isolation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix tests Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/stringprintf.h" 6 #include "base/stringprintf.h"
7 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "base/values.h"
8 #include "chrome/browser/automation/automation_util.h" 9 #include "chrome/browser/automation/automation_util.h"
9 #include "chrome/browser/extensions/extension_apitest.h" 10 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/extensions/extension_browsertest.h" 11 #include "chrome/browser/extensions/extension_browsertest.h"
12 #include "chrome/browser/extensions/extension_function_test_utils.h"
11 #include "chrome/browser/extensions/extension_service.h" 13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_tabs_module.h"
12 #include "chrome/browser/extensions/extension_test_message_listener.h" 15 #include "chrome/browser/extensions/extension_test_message_listener.h"
13 #include "chrome/browser/extensions/shell_window_registry.h" 16 #include "chrome/browser/extensions/shell_window_registry.h"
14 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/tab_contents/render_view_context_menu.h" 18 #include "chrome/browser/tab_contents/render_view_context_menu.h"
16 #include "chrome/browser/ui/browser.h" 19 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_list.h" 20 #include "chrome/browser/ui/browser_list.h"
18 #include "chrome/browser/ui/extensions/shell_window.h" 21 #include "chrome/browser/ui/extensions/shell_window.h"
19 #include "chrome/common/chrome_switches.h" 22 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/extensions/extension_constants.h" 23 #include "chrome/common/extensions/extension_constants.h"
21 #include "chrome/test/base/ui_test_utils.h" 24 #include "chrome/test/base/ui_test_utils.h"
22 #include "content/public/browser/web_contents.h" 25 #include "content/public/browser/web_contents.h"
23 #include "content/public/common/context_menu_params.h" 26 #include "content/public/common/context_menu_params.h"
24 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
25 #include "ui/base/models/menu_model.h" 28 #include "ui/base/models/menu_model.h"
26 29
27 using content::WebContents; 30 using content::WebContents;
28 31
32 namespace utils = extension_function_test_utils;
33
29 namespace { 34 namespace {
30 // Non-abstract RenderViewContextMenu class. 35 // Non-abstract RenderViewContextMenu class.
31 class PlatformAppContextMenu : public RenderViewContextMenu { 36 class PlatformAppContextMenu : public RenderViewContextMenu {
32 public: 37 public:
33 PlatformAppContextMenu(WebContents* web_contents, 38 PlatformAppContextMenu(WebContents* web_contents,
34 const content::ContextMenuParams& params) 39 const content::ContextMenuParams& params)
35 : RenderViewContextMenu(web_contents, params) {} 40 : RenderViewContextMenu(web_contents, params) {}
36 41
37 protected: 42 protected:
38 // These two functions implement pure virtual methods of 43 // These two functions implement pure virtual methods of
(...skipping 10 matching lines...) Expand all
49 54
50 class PlatformAppBrowserTest : public ExtensionApiTest { 55 class PlatformAppBrowserTest : public ExtensionApiTest {
51 public: 56 public:
52 virtual void SetUpCommandLine(CommandLine* command_line) { 57 virtual void SetUpCommandLine(CommandLine* command_line) {
53 ExtensionBrowserTest::SetUpCommandLine(command_line); 58 ExtensionBrowserTest::SetUpCommandLine(command_line);
54 command_line->AppendSwitch(switches::kEnablePlatformApps); 59 command_line->AppendSwitch(switches::kEnablePlatformApps);
55 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); 60 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
56 } 61 }
57 62
58 protected: 63 protected:
59 void LoadAndLaunchPlatformApp(const char* name) { 64 const Extension* LoadAndLaunchPlatformApp(const char* name) {
60 ui_test_utils::WindowedNotificationObserver app_loaded_observer( 65 ui_test_utils::WindowedNotificationObserver app_loaded_observer(
61 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, 66 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
62 content::NotificationService::AllSources()); 67 content::NotificationService::AllSources());
63 68
64 EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps"). 69 const Extension* extension = LoadExtension(
65 AppendASCII(name))); 70 test_data_dir_.AppendASCII("platform_apps").AppendASCII(name));
66
67 ExtensionService* service = browser()->profile()->GetExtensionService();
68 const Extension* extension = service->GetExtensionById(
69 last_loaded_extension_id_, false);
70 EXPECT_TRUE(extension); 71 EXPECT_TRUE(extension);
71 72
72 Browser::OpenApplication( 73 Browser::OpenApplication(
73 browser()->profile(), 74 browser()->profile(),
74 extension, 75 extension,
75 extension_misc::LAUNCH_NONE, 76 extension_misc::LAUNCH_NONE,
76 GURL(), 77 GURL(),
77 NEW_WINDOW); 78 NEW_WINDOW);
78 79
79 app_loaded_observer.Wait(); 80 app_loaded_observer.Wait();
81
82 return extension;
80 } 83 }
81 84
82 // Gets the WebContents associated with the first shell window that is found 85 // 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 86 // (most tests only deal with one platform app window, so this is good
84 // enough). 87 // enough).
85 WebContents* GetFirstShellWindowWebContents() { 88 WebContents* GetFirstShellWindowWebContents() {
86 ShellWindowRegistry* app_registry = 89 ShellWindowRegistry* app_registry =
87 ShellWindowRegistry::Get(browser()->profile()); 90 ShellWindowRegistry::Get(browser()->profile());
88 ShellWindowRegistry::const_iterator iter; 91 ShellWindowRegistry::const_iterator iter;
89 ShellWindowRegistry::ShellWindowSet shell_windows = 92 ShellWindowRegistry::ShellWindowSet shell_windows =
90 app_registry->shell_windows(); 93 app_registry->shell_windows();
91 for (iter = shell_windows.begin(); iter != shell_windows.end(); ++iter) { 94 for (iter = shell_windows.begin(); iter != shell_windows.end(); ++iter) {
92 return (*iter)->web_contents(); 95 return (*iter)->web_contents();
93 } 96 }
94 97
95 return NULL; 98 return NULL;
96 } 99 }
100
101 // Runs chrome.windows.getAll for the given extension and returns the number
102 // of windows that the function returns.
103 size_t RunGetWindowsFunctionForExtension(const Extension* extension) {
104 GetAllWindowsFunction* function = new GetAllWindowsFunction();
105 function->set_extension(extension);
106 scoped_ptr<base::ListValue> result(utils::ToList(
107 utils::RunFunctionAndReturnResult(function, "[]", browser())));
108 return result->GetSize();
109 }
110
111 // Runs chrome.windows.get(|window_id|) for the the given extension and
112 // returns whether or not a window was found.
113 bool RunGetWindowFunctionForExtension(
114 int window_id, const Extension* extension) {
115 GetWindowFunction* function = new GetWindowFunction();
116 function->set_extension(extension);
117 utils::RunFunction(
118 function,
119 base::StringPrintf("[%u]", window_id),
120 browser(),
121 utils::NONE);
122 return function->GetResultValue() != NULL;
123 }
124
125 size_t GetShellWindowCount() {
126 return ShellWindowRegistry::Get(browser()->profile())->
127 shell_windows().size();
128 }
97 }; 129 };
98 130
99 // Tests that platform apps received the "launch" event when launched. 131 // Tests that platform apps received the "launch" event when launched.
100 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) { 132 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) {
101 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_; 133 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_;
102 } 134 }
103 135
104 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) { 136 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
105 ExtensionTestMessageListener launched_listener("Launched", false); 137 ExtensionTestMessageListener launched_listener("Launched", false);
106 LoadAndLaunchPlatformApp("empty_context_menu"); 138 LoadAndLaunchPlatformApp("minimal");
107 139
108 ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); 140 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
109 141
110 // The empty app doesn't add any context menu items, so its menu should 142 // The empty app doesn't add any context menu items, so its menu should
111 // only include the developer tools. 143 // only include the developer tools.
112 WebContents* web_contents = GetFirstShellWindowWebContents(); 144 WebContents* web_contents = GetFirstShellWindowWebContents();
113 ASSERT_TRUE(web_contents); 145 ASSERT_TRUE(web_contents);
114 WebKit::WebContextMenuData data; 146 WebKit::WebContextMenuData data;
115 content::ContextMenuParams params(data); 147 content::ContextMenuParams params(data);
116 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, 148 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 set_cookie_url, 218 set_cookie_url,
187 browser()->GetWebContentsAt(0), 219 browser()->GetWebContentsAt(0),
188 &cookie_size, 220 &cookie_size,
189 &cookie_value); 221 &cookie_value);
190 ASSERT_EQ("testCookie=1", cookie_value); 222 ASSERT_EQ("testCookie=1", cookie_value);
191 223
192 // Let the platform app request the same URL, and make sure that it doesn't 224 // Let the platform app request the same URL, and make sure that it doesn't
193 // see the cookie. 225 // see the cookie.
194 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_; 226 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_;
195 } 227 }
228
229 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ExtensionWindowingApis) {
230 // Initially there should be just the one browser window visible to the
231 // extensions API.
232 const Extension* extension = LoadExtension(
233 test_data_dir_.AppendASCII("common/background_page"));
234 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
235
236 // And no shell windows.
237 ASSERT_EQ(0U, GetShellWindowCount());
238
239 // Launch a platform app that shows a window.
240 ExtensionTestMessageListener launched_listener("Launched", false);
241 const Extension* platform_app = LoadAndLaunchPlatformApp("minimal");
242 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
243 ASSERT_EQ(1U, GetShellWindowCount());
244 ShellWindowRegistry::ShellWindowSet shell_windows =
245 ShellWindowRegistry::Get(browser()->profile())->shell_windows();
246 int shell_window_id = (*shell_windows.begin())->session_id().id();
247
248 // But it's not visible to the extensions API, it still thinks there's just
249 // one browser window.
250 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
251 // It can't look it up by ID either
252 ASSERT_FALSE(RunGetWindowFunctionForExtension(shell_window_id, extension));
253
254 // The app can also only see one window (its own).
255 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(platform_app));
256 ASSERT_TRUE(RunGetWindowFunctionForExtension(shell_window_id, platform_app));
257
258 // Launch another platform app that also shows a window.
259 ExtensionTestMessageListener launched_listener2("Launched", false);
260 const Extension* platform_app2 = LoadAndLaunchPlatformApp("context_menu");
261 ASSERT_TRUE(launched_listener2.WaitUntilSatisfied());
262
263 // There are two total shell windows, but each app can only see its own.
264 ASSERT_EQ(2U, GetShellWindowCount());
265 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(platform_app));
266 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(platform_app2));
267 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698