OLD | NEW |
---|---|
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 "chrome/browser/extensions/extension_apitest.h" | 8 #include "chrome/browser/extensions/extension_apitest.h" |
9 #include "chrome/browser/extensions/extension_browsertest.h" | 9 #include "chrome/browser/extensions/extension_browsertest.h" |
10 #include "chrome/browser/extensions/extension_host.h" | |
11 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
12 #include "chrome/browser/extensions/extension_test_message_listener.h" | 11 #include "chrome/browser/extensions/extension_test_message_listener.h" |
12 #include "chrome/browser/extensions/shell_window_registry.h" | |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 14 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_list.h" | 16 #include "chrome/browser/ui/browser_list.h" |
17 #include "chrome/browser/ui/extensions/shell_window.h" | |
17 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/chrome_view_type.h" | |
19 #include "chrome/common/extensions/extension_constants.h" | 19 #include "chrome/common/extensions/extension_constants.h" |
20 #include "chrome/test/base/ui_test_utils.h" | 20 #include "chrome/test/base/ui_test_utils.h" |
21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
22 #include "content/public/common/context_menu_params.h" | 22 #include "content/public/common/context_menu_params.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "ui/base/models/menu_model.h" | 24 #include "ui/base/models/menu_model.h" |
25 | 25 |
26 using content::WebContents; | 26 using content::WebContents; |
27 | 27 |
28 namespace { | 28 namespace { |
(...skipping 19 matching lines...) Expand all Loading... | |
48 class PlatformAppBrowserTest : public ExtensionApiTest { | 48 class PlatformAppBrowserTest : public ExtensionApiTest { |
49 public: | 49 public: |
50 virtual void SetUpCommandLine(CommandLine* command_line) { | 50 virtual void SetUpCommandLine(CommandLine* command_line) { |
51 ExtensionBrowserTest::SetUpCommandLine(command_line); | 51 ExtensionBrowserTest::SetUpCommandLine(command_line); |
52 command_line->AppendSwitch(switches::kEnablePlatformApps); | 52 command_line->AppendSwitch(switches::kEnablePlatformApps); |
53 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | 53 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |
54 } | 54 } |
55 | 55 |
56 protected: | 56 protected: |
57 void LoadAndLaunchPlatformApp(const char* name) { | 57 void LoadAndLaunchPlatformApp(const char* name) { |
58 size_t platform_app_count = GetPlatformAppCount(); | |
59 ui_test_utils::WindowedNotificationObserver app_loaded_observer( | 58 ui_test_utils::WindowedNotificationObserver app_loaded_observer( |
60 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, | 59 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
61 content::NotificationService::AllSources()); | 60 content::NotificationService::AllSources()); |
62 | 61 |
63 EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps"). | 62 EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII("platform_apps"). |
64 AppendASCII(name))); | 63 AppendASCII(name))); |
65 | 64 |
66 ExtensionService* service = browser()->profile()->GetExtensionService(); | 65 ExtensionService* service = browser()->profile()->GetExtensionService(); |
67 const Extension* extension = service->GetExtensionById( | 66 const Extension* extension = service->GetExtensionById( |
68 last_loaded_extension_id_, false); | 67 last_loaded_extension_id_, false); |
69 EXPECT_TRUE(extension); | 68 EXPECT_TRUE(extension); |
70 | 69 |
71 Browser::OpenApplication( | 70 Browser::OpenApplication( |
72 browser()->profile(), | 71 browser()->profile(), |
73 extension, | 72 extension, |
74 extension_misc::LAUNCH_NONE, | 73 extension_misc::LAUNCH_NONE, |
75 GURL(), | 74 GURL(), |
76 NEW_WINDOW); | 75 NEW_WINDOW); |
77 | 76 |
78 app_loaded_observer.Wait(); | 77 app_loaded_observer.Wait(); |
79 | |
80 // Now we have a new platform app running. | |
81 EXPECT_EQ(platform_app_count + 1, GetPlatformAppCount()); | |
82 } | 78 } |
83 | 79 |
84 // Gets the number of platform apps extension hosts that are running. | 80 // Gets the WebContents associated with the first shell window that is found |
85 size_t GetPlatformAppCount() { | 81 // (most tests only deal with one platform app window, so this is good |
86 int count = 0; | 82 // enough). |
87 ExtensionProcessManager* process_manager = | 83 WebContents* GetFirstShellWindowWebContents() { |
88 browser()->profile()->GetExtensionProcessManager(); | 84 ShellWindowRegistry* app_registry = |
89 ExtensionProcessManager::const_iterator iter; | 85 ShellWindowRegistry::Get(browser()->profile()); |
90 ExtensionProcessManager::ExtensionHostSet platform_app_hosts = | 86 ShellWindowRegistry::const_iterator iter; |
91 process_manager->platform_app_hosts(); | 87 ShellWindowRegistry::ShellWindowSet shell_windows = |
92 for (iter = platform_app_hosts.begin(); iter != platform_app_hosts.end(); | 88 app_registry->shell_windows(); |
93 ++iter) { | 89 for (iter = shell_windows.begin(); iter != shell_windows.end(); ++iter) { |
94 if ((*iter)->extension()) | 90 return (*iter)->web_contents(); |
95 count++; | |
96 } | |
97 | |
98 return count; | |
99 } | |
100 | |
101 // Gets the WebContents associated with the ExtensionHost of the first | |
102 // platform app shell window that is found (most tests only deal with one | |
103 // platform app window, so this is good enough). | |
104 WebContents* GetFirstPlatformAppShellWindowWebContents() { | |
105 ExtensionProcessManager* process_manager = | |
106 browser()->profile()->GetExtensionProcessManager(); | |
107 ExtensionProcessManager::const_iterator iter; | |
108 ExtensionProcessManager::ExtensionHostSet platform_app_hosts = | |
109 process_manager->platform_app_hosts(); | |
110 for (iter = platform_app_hosts.begin(); iter != platform_app_hosts.end(); | |
111 ++iter) { | |
112 if ((*iter)->extension_host_type() == chrome::VIEW_TYPE_APP_SHELL) | |
113 return (*iter)->host_contents(); | |
114 } | 91 } |
115 | 92 |
116 return NULL; | 93 return NULL; |
117 } | 94 } |
118 }; | 95 }; |
119 | 96 |
120 // Tests that platform apps received the "launch" event when launched. | 97 // Tests that platform apps received the "launch" event when launched. |
121 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) { | 98 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, OnLaunchedEvent) { |
122 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_; | 99 ASSERT_TRUE(RunPlatformAppTest("platform_apps/launch")) << message_; |
123 } | 100 } |
124 | 101 |
125 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) { | 102 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, EmptyContextMenu) { |
126 ExtensionTestMessageListener launched_listener("Launched", false); | 103 ExtensionTestMessageListener launched_listener("Launched", false); |
127 LoadAndLaunchPlatformApp("empty_context_menu"); | 104 LoadAndLaunchPlatformApp("empty_context_menu"); |
128 | 105 |
129 ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); | 106 ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
130 | 107 |
131 // The empty app doesn't add any context menu items, so its menu should | 108 // The empty app doesn't add any context menu items, so its menu should |
132 // only include the developer tools. | 109 // only include the developer tools. |
133 WebContents* web_contents = GetFirstPlatformAppShellWindowWebContents(); | 110 WebContents* web_contents = GetFirstShellWindowWebContents(); |
134 ASSERT_TRUE(web_contents); | 111 ASSERT_TRUE(web_contents); |
135 WebKit::WebContextMenuData data; | 112 WebKit::WebContextMenuData data; |
136 content::ContextMenuParams params(data); | 113 content::ContextMenuParams params(data); |
137 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, | 114 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |
138 params); | 115 params); |
139 menu->Init(); | 116 menu->Init(); |
117 // TODO(benwells): Remove the constant below. Instead of checking the | |
118 // number of menu items check certain item's absense and presence. | |
140 // 3 including separator | 119 // 3 including separator |
141 ASSERT_EQ(3, menu->menu_model().GetItemCount()); | 120 ASSERT_EQ(3, menu->menu_model().GetItemCount()); |
142 } | 121 } |
jeremya
2012/05/07 00:54:38
I wish we could write a unit test for this, instea
| |
143 | 122 |
144 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) { | 123 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, AppWithContextMenu) { |
145 ExtensionTestMessageListener launched_listener("Launched", false); | 124 ExtensionTestMessageListener launched_listener("Launched", false); |
146 LoadAndLaunchPlatformApp("context_menu"); | 125 LoadAndLaunchPlatformApp("context_menu"); |
147 | 126 |
148 // Wait for the extension to tell us it's initialized its context menus and | 127 // Wait for the extension to tell us it's initialized its context menus and |
149 // launched a window. | 128 // launched a window. |
150 ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); | 129 ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); |
151 | 130 |
152 // The context_menu app has one context menu item. This, along with a | 131 // The context_menu app has one context menu item. This, along with a |
153 // separator and the developer tools, is all that should be in the menu. | 132 // separator and the developer tools, is all that should be in the menu. |
154 WebContents* web_contents = GetFirstPlatformAppShellWindowWebContents(); | 133 WebContents* web_contents = GetFirstShellWindowWebContents(); |
155 ASSERT_TRUE(web_contents); | 134 ASSERT_TRUE(web_contents); |
156 WebKit::WebContextMenuData data; | 135 WebKit::WebContextMenuData data; |
157 content::ContextMenuParams params(data); | 136 content::ContextMenuParams params(data); |
158 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, | 137 PlatformAppContextMenu* menu = new PlatformAppContextMenu(web_contents, |
159 params); | 138 params); |
160 menu->Init(); | 139 menu->Init(); |
140 // TODO(benwells): Remove the constant below. Instead of checking the | |
141 // number of menu items check certain item's absense and presence. | |
161 ASSERT_EQ(4, menu->menu_model().GetItemCount()); | 142 ASSERT_EQ(4, menu->menu_model().GetItemCount()); |
162 } | 143 } |
163 | 144 |
164 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) { | 145 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowNavigation) { |
165 ASSERT_TRUE(StartTestServer()); | 146 ASSERT_TRUE(StartTestServer()); |
166 ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_; | 147 ASSERT_TRUE(RunPlatformAppTest("platform_apps/navigation")) << message_; |
167 } | 148 } |
168 | 149 |
169 // Tests that localStorage and WebSQL are disabled for platform apps. | 150 // Tests that localStorage and WebSQL are disabled for platform apps. |
170 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) { | 151 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, DisallowStorage) { |
171 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_; | 152 ASSERT_TRUE(RunPlatformAppTest("platform_apps/storage")) << message_; |
172 } | 153 } |
173 | 154 |
174 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) { | 155 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, Restrictions) { |
175 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_; | 156 ASSERT_TRUE(RunPlatformAppTest("platform_apps/restrictions")) << message_; |
176 } | 157 } |
177 | 158 |
178 // Tests that platform apps can use the chrome.windows.* API. | 159 // Tests that platform apps can use the chrome.windows.* API. |
179 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) { | 160 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, WindowsApi) { |
180 ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_; | 161 ASSERT_TRUE(RunPlatformAppTest("platform_apps/windows_api")) << message_; |
181 } | 162 } |
OLD | NEW |