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

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: Add "ForFunction" 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;
31 using extensions::Extension;
32
33 namespace utils = extension_function_test_utils;
28 34
29 namespace { 35 namespace {
30 // Non-abstract RenderViewContextMenu class. 36 // Non-abstract RenderViewContextMenu class.
31 class PlatformAppContextMenu : public RenderViewContextMenu { 37 class PlatformAppContextMenu : public RenderViewContextMenu {
32 public: 38 public:
33 PlatformAppContextMenu(WebContents* web_contents, 39 PlatformAppContextMenu(WebContents* web_contents,
34 const content::ContextMenuParams& params) 40 const content::ContextMenuParams& params)
35 : RenderViewContextMenu(web_contents, params) {} 41 : RenderViewContextMenu(web_contents, params) {}
36 42
37 protected: 43 protected:
(...skipping 11 matching lines...) Expand all
49 55
50 class PlatformAppBrowserTest : public ExtensionApiTest { 56 class PlatformAppBrowserTest : public ExtensionApiTest {
51 public: 57 public:
52 virtual void SetUpCommandLine(CommandLine* command_line) { 58 virtual void SetUpCommandLine(CommandLine* command_line) {
53 ExtensionBrowserTest::SetUpCommandLine(command_line); 59 ExtensionBrowserTest::SetUpCommandLine(command_line);
54 command_line->AppendSwitch(switches::kEnablePlatformApps); 60 command_line->AppendSwitch(switches::kEnablePlatformApps);
55 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); 61 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
56 } 62 }
57 63
58 protected: 64 protected:
59 void LoadAndLaunchPlatformApp(const char* name) { 65 const Extension* LoadAndLaunchPlatformApp(const char* name) {
60 ui_test_utils::WindowedNotificationObserver app_loaded_observer( 66 ui_test_utils::WindowedNotificationObserver app_loaded_observer(
61 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, 67 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
62 content::NotificationService::AllSources()); 68 content::NotificationService::AllSources());
63 69
64 EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps"). 70 const Extension* extension = LoadExtension(
65 AppendASCII(name))); 71 test_data_dir_.AppendASCII("platform_apps").AppendASCII(name));
66
67 ExtensionService* service = browser()->profile()->GetExtensionService();
68 const extensions::Extension* extension = service->GetExtensionById(
69 last_loaded_extension_id_, false);
70 EXPECT_TRUE(extension); 72 EXPECT_TRUE(extension);
71 73
72 Browser::OpenApplication( 74 Browser::OpenApplication(
73 browser()->profile(), 75 browser()->profile(),
74 extension, 76 extension,
75 extension_misc::LAUNCH_NONE, 77 extension_misc::LAUNCH_NONE,
76 GURL(), 78 GURL(),
77 NEW_WINDOW); 79 NEW_WINDOW);
78 80
79 app_loaded_observer.Wait(); 81 app_loaded_observer.Wait();
82
83 return extension;
80 } 84 }
81 85
82 // Gets the WebContents associated with the first shell window that is found 86 // 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 87 // (most tests only deal with one platform app window, so this is good
84 // enough). 88 // enough).
85 WebContents* GetFirstShellWindowWebContents() { 89 WebContents* GetFirstShellWindowWebContents() {
86 ShellWindowRegistry* app_registry = 90 ShellWindowRegistry* app_registry =
87 ShellWindowRegistry::Get(browser()->profile()); 91 ShellWindowRegistry::Get(browser()->profile());
88 ShellWindowRegistry::const_iterator iter; 92 ShellWindowRegistry::const_iterator iter;
89 ShellWindowRegistry::ShellWindowSet shell_windows = 93 ShellWindowRegistry::ShellWindowSet shell_windows =
90 app_registry->shell_windows(); 94 app_registry->shell_windows();
91 for (iter = shell_windows.begin(); iter != shell_windows.end(); ++iter) { 95 for (iter = shell_windows.begin(); iter != shell_windows.end(); ++iter) {
92 return (*iter)->web_contents(); 96 return (*iter)->web_contents();
93 } 97 }
94 98
95 return NULL; 99 return NULL;
96 } 100 }
101
102 // Runs chrome.windows.getAll for the given extension and returns the number
103 // of windows that the function returns.
104 size_t RunGetWindowsFunctionForExtension(const Extension* extension) {
105 GetAllWindowsFunction* function = new GetAllWindowsFunction();
106 function->set_extension(extension);
107 scoped_ptr<base::ListValue> result(utils::ToList(
108 utils::RunFunctionAndReturnResult(function, "[]", browser())));
109 return result->GetSize();
110 }
111
112 // Runs chrome.windows.get(|window_id|) for the the given extension and
113 // returns whether or not a window was found.
114 bool RunGetWindowFunctionForExtension(
115 int window_id, const Extension* extension) {
116 GetWindowFunction* function = new GetWindowFunction();
117 function->set_extension(extension);
118 utils::RunFunction(
119 function,
120 base::StringPrintf("[%u]", window_id),
121 browser(),
122 utils::NONE);
123 return function->GetResultValue() != NULL;
124 }
125
126 size_t GetShellWindowCount() {
127 return ShellWindowRegistry::Get(browser()->profile())->
128 shell_windows().size();
129 }
97 }; 130 };
98 131
99 // Tests that platform apps received the "launch" event when launched. 132 // Tests that platform apps received the "launch" event when launched.
100 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) { 133 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) {
101 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_; 134 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_;
102 } 135 }
103 136
104 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) { 137 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) {
105 ExtensionTestMessageListener launched_listener("Launched", false); 138 ExtensionTestMessageListener launched_listener("Launched", false);
106 LoadAndLaunchPlatformApp("empty_context_menu"); 139 LoadAndLaunchPlatformApp("minimal");
107 140
108 ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); 141 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
109 142
110 // The empty app doesn't add any context menu items, so its menu should 143 // The empty app doesn't add any context menu items, so its menu should
111 // only include the developer tools. 144 // only include the developer tools.
112 WebContents* web_contents = GetFirstShellWindowWebContents(); 145 WebContents* web_contents = GetFirstShellWindowWebContents();
113 ASSERT_TRUE(web_contents); 146 ASSERT_TRUE(web_contents);
114 WebKit::WebContextMenuData data; 147 WebKit::WebContextMenuData data;
115 content::ContextMenuParams params(data); 148 content::ContextMenuParams params(data);
116 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, 149 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents,
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 set_cookie_url, 219 set_cookie_url,
187 browser()->GetWebContentsAt(0), 220 browser()->GetWebContentsAt(0),
188 &cookie_size, 221 &cookie_size,
189 &cookie_value); 222 &cookie_value);
190 ASSERT_EQ("testCookie=1", cookie_value); 223 ASSERT_EQ("testCookie=1", cookie_value);
191 224
192 // Let the platform app request the same URL, and make sure that it doesn't 225 // Let the platform app request the same URL, and make sure that it doesn't
193 // see the cookie. 226 // see the cookie.
194 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_; 227 ASSERT_TRUE(RunPlatformAppTest("platform_apps/isolation")) << message_;
195 } 228 }
229
230 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ExtensionWindowingApis) {
231 // Initially there should be just the one browser window visible to the
232 // extensions API.
233 const Extension* extension = LoadExtension(
234 test_data_dir_.AppendASCII("common/background_page"));
235 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
236
237 // And no shell windows.
238 ASSERT_EQ(0U, GetShellWindowCount());
239
240 // Launch a platform app that shows a window.
241 ExtensionTestMessageListener launched_listener("Launched", false);
242 const Extension* platform_app = LoadAndLaunchPlatformApp("minimal");
243 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
244 ASSERT_EQ(1U, GetShellWindowCount());
245 ShellWindowRegistry::ShellWindowSet shell_windows =
246 ShellWindowRegistry::Get(browser()->profile())->shell_windows();
247 int shell_window_id = (*shell_windows.begin())->session_id().id();
248
249 // But it's not visible to the extensions API, it still thinks there's just
250 // one browser window.
251 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(extension));
252 // It can't look it up by ID either
253 ASSERT_FALSE(RunGetWindowFunctionForExtension(shell_window_id, extension));
254
255 // The app can also only see one window (its own).
256 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(platform_app));
257 ASSERT_TRUE(RunGetWindowFunctionForExtension(shell_window_id, platform_app));
258
259 // Launch another platform app that also shows a window.
260 ExtensionTestMessageListener launched_listener2("Launched", false);
261 const Extension* platform_app2 = LoadAndLaunchPlatformApp("context_menu");
262 ASSERT_TRUE(launched_listener2.WaitUntilSatisfied());
263
264 // There are two total shell windows, but each app can only see its own.
265 ASSERT_EQ(2U, GetShellWindowCount());
266 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(platform_app));
267 ASSERT_EQ(1U, RunGetWindowsFunctionForExtension(platform_app2));
268 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_window_list.cc ('k') | chrome/browser/ui/extensions/shell_window.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698