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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/extensions/extension_browsertest.h" | 12 #include "chrome/browser/extensions/extension_browsertest.h" |
13 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_tabstrip.h" | 16 #include "chrome/browser/ui/browser_tabstrip.h" |
17 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
19 #include "chrome/common/extensions/manifest.h" | 19 #include "chrome/common/extensions/manifest.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/test/browser_test_utils.h" | 22 #include "content/public/test/browser_test_utils.h" |
23 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
24 #include "net/base/mock_host_resolver.h" | 24 #include "net/base/mock_host_resolver.h" |
25 | 25 |
26 using extensions::Extension; | 26 using extensions::Extension; |
27 | 27 |
28 class ChromeAppAPITest : public ExtensionBrowserTest { | 28 class ChromeAppAPITest : public ExtensionBrowserTest { |
29 protected: | 29 protected: |
30 bool IsAppInstalled() { return IsAppInstalled(L""); } | 30 bool IsAppInstalled() { return IsAppInstalled(""); } |
31 bool IsAppInstalled(const std::wstring& frame_xpath) { | 31 bool IsAppInstalled(const char* frame_xpath) { |
32 std::wstring get_app_is_installed = | 32 const char kGetAppIsInstalled[] = |
33 L"window.domAutomationController.send(window.chrome.app.isInstalled);"; | 33 "window.domAutomationController.send(window.chrome.app.isInstalled);"; |
34 bool result; | 34 bool result; |
35 CHECK( | 35 CHECK( |
36 content::ExecuteJavaScriptAndExtractBool( | 36 content::ExecuteJavaScriptAndExtractBool( |
37 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 37 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
38 frame_xpath, get_app_is_installed, &result)); | 38 frame_xpath, kGetAppIsInstalled, &result)); |
39 return result; | 39 return result; |
40 } | 40 } |
41 | 41 |
42 std::string InstallState() { return InstallState(L""); } | 42 std::string InstallState() { return InstallState(""); } |
43 std::string InstallState(const std::wstring& frame_xpath) { | 43 std::string InstallState(const char* frame_xpath) { |
44 std::wstring get_app_install_state = | 44 const char kGetAppInstallState[] = |
45 L"window.chrome.app.installState(" | 45 "window.chrome.app.installState(" |
46 L"function(s) { window.domAutomationController.send(s); });"; | 46 " function(s) { window.domAutomationController.send(s); });"; |
47 std::string result; | 47 std::string result; |
48 CHECK( | 48 CHECK( |
49 content::ExecuteJavaScriptAndExtractString( | 49 content::ExecuteJavaScriptAndExtractString( |
50 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 50 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
51 frame_xpath, get_app_install_state, &result)); | 51 frame_xpath, kGetAppInstallState, &result)); |
52 return result; | 52 return result; |
53 } | 53 } |
54 | 54 |
55 std::string RunningState() { return RunningState(L""); } | 55 std::string RunningState() { return RunningState(""); } |
56 std::string RunningState(const std::wstring& frame_xpath) { | 56 std::string RunningState(const char* frame_xpath) { |
57 std::wstring get_app_install_state = | 57 const char kGetAppRunningState[] = |
58 L"window.domAutomationController.send(" | 58 "window.domAutomationController.send(" |
59 L"window.chrome.app.runningState());"; | 59 " window.chrome.app.runningState());"; |
60 std::string result; | 60 std::string result; |
61 CHECK( | 61 CHECK( |
62 content::ExecuteJavaScriptAndExtractString( | 62 content::ExecuteJavaScriptAndExtractString( |
63 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 63 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
64 frame_xpath, get_app_install_state, &result)); | 64 frame_xpath, kGetAppRunningState, &result)); |
65 return result; | 65 return result; |
66 } | 66 } |
67 | 67 |
68 private: | 68 private: |
69 virtual void SetUpCommandLine(CommandLine* command_line) { | 69 virtual void SetUpCommandLine(CommandLine* command_line) { |
70 ExtensionBrowserTest::SetUpCommandLine(command_line); | 70 ExtensionBrowserTest::SetUpCommandLine(command_line); |
71 command_line->AppendSwitchASCII(switches::kAppsCheckoutURL, | 71 command_line->AppendSwitchASCII(switches::kAppsCheckoutURL, |
72 "http://checkout.com:"); | 72 "http://checkout.com:"); |
73 } | 73 } |
74 }; | 74 }; |
(...skipping 26 matching lines...) Expand all Loading... |
101 | 101 |
102 // Even after the app is installed, the existing app.com tab is not in an | 102 // Even after the app is installed, the existing app.com tab is not in an |
103 // app process, so chrome.app.isInstalled should return false. | 103 // app process, so chrome.app.isInstalled should return false. |
104 EXPECT_FALSE(IsAppInstalled()); | 104 EXPECT_FALSE(IsAppInstalled()); |
105 | 105 |
106 // Test that a non-app page has chrome.app.isInstalled = false. | 106 // Test that a non-app page has chrome.app.isInstalled = false. |
107 ui_test_utils::NavigateToURL(browser(), non_app_url); | 107 ui_test_utils::NavigateToURL(browser(), non_app_url); |
108 EXPECT_FALSE(IsAppInstalled()); | 108 EXPECT_FALSE(IsAppInstalled()); |
109 | 109 |
110 // Test that a non-app page returns null for chrome.app.getDetails(). | 110 // Test that a non-app page returns null for chrome.app.getDetails(). |
111 std::wstring get_app_details = | 111 const char kGetAppDetails[] = |
112 L"window.domAutomationController.send(" | 112 "window.domAutomationController.send(" |
113 L" JSON.stringify(window.chrome.app.getDetails()));"; | 113 " JSON.stringify(window.chrome.app.getDetails()));"; |
114 std::string result; | 114 std::string result; |
115 ASSERT_TRUE( | 115 ASSERT_TRUE( |
116 content::ExecuteJavaScriptAndExtractString( | 116 content::ExecuteJavaScriptAndExtractString( |
117 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 117 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
118 L"", get_app_details, &result)); | 118 "", |
| 119 kGetAppDetails, |
| 120 &result)); |
119 EXPECT_EQ("null", result); | 121 EXPECT_EQ("null", result); |
120 | 122 |
121 // Check that an app page has chrome.app.isInstalled = true. | 123 // Check that an app page has chrome.app.isInstalled = true. |
122 ui_test_utils::NavigateToURL(browser(), app_url); | 124 ui_test_utils::NavigateToURL(browser(), app_url); |
123 EXPECT_TRUE(IsAppInstalled()); | 125 EXPECT_TRUE(IsAppInstalled()); |
124 | 126 |
125 // Check that an app page returns the correct result for | 127 // Check that an app page returns the correct result for |
126 // chrome.app.getDetails(). | 128 // chrome.app.getDetails(). |
127 ui_test_utils::NavigateToURL(browser(), app_url); | 129 ui_test_utils::NavigateToURL(browser(), app_url); |
128 ASSERT_TRUE( | 130 ASSERT_TRUE( |
129 content::ExecuteJavaScriptAndExtractString( | 131 content::ExecuteJavaScriptAndExtractString( |
130 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 132 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
131 L"", get_app_details, &result)); | 133 "", |
| 134 kGetAppDetails, |
| 135 &result)); |
132 scoped_ptr<DictionaryValue> app_details( | 136 scoped_ptr<DictionaryValue> app_details( |
133 static_cast<DictionaryValue*>(base::JSONReader::Read(result))); | 137 static_cast<DictionaryValue*>(base::JSONReader::Read(result))); |
134 // extension->manifest() does not contain the id. | 138 // extension->manifest() does not contain the id. |
135 app_details->Remove("id", NULL); | 139 app_details->Remove("id", NULL); |
136 EXPECT_TRUE(app_details.get()); | 140 EXPECT_TRUE(app_details.get()); |
137 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); | 141 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); |
138 | 142 |
139 // Try to change app.isInstalled. Should silently fail, so | 143 // Try to change app.isInstalled. Should silently fail, so |
140 // that isInstalled should have the initial value. | 144 // that isInstalled should have the initial value. |
141 ASSERT_TRUE( | 145 ASSERT_TRUE( |
142 content::ExecuteJavaScriptAndExtractString( | 146 content::ExecuteJavaScriptAndExtractString( |
143 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 147 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
144 L"", | 148 "", |
145 L"window.domAutomationController.send(" | 149 "window.domAutomationController.send(" |
146 L" function() {" | 150 " function() {" |
147 L" var value = window.chrome.app.isInstalled;" | 151 " var value = window.chrome.app.isInstalled;" |
148 L" window.chrome.app.isInstalled = !value;" | 152 " window.chrome.app.isInstalled = !value;" |
149 L" if (window.chrome.app.isInstalled == value) {" | 153 " if (window.chrome.app.isInstalled == value) {" |
150 L" return 'true';" | 154 " return 'true';" |
151 L" } else {" | 155 " } else {" |
152 L" return 'false';" | 156 " return 'false';" |
153 L" }" | 157 " }" |
154 L" }()" | 158 " }()" |
155 L");", | 159 ");", |
156 &result)); | 160 &result)); |
157 | 161 |
158 // Should not be able to alter window.chrome.app.isInstalled from javascript"; | 162 // Should not be able to alter window.chrome.app.isInstalled from javascript"; |
159 EXPECT_EQ("true", result); | 163 EXPECT_EQ("true", result); |
160 } | 164 } |
161 | 165 |
162 IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, GetDetailsForFrame) { | 166 IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, GetDetailsForFrame) { |
163 std::string app_host("app.com"); | 167 std::string app_host("app.com"); |
164 std::string nonapp_host("nonapp.com"); | 168 std::string nonapp_host("nonapp.com"); |
165 std::string checkout_host("checkout.com"); | 169 std::string checkout_host("checkout.com"); |
166 | 170 |
(...skipping 12 matching lines...) Expand all Loading... |
179 replace_host.SetHostStr(app_host); | 183 replace_host.SetHostStr(app_host); |
180 GURL app_url(test_file_url.ReplaceComponents(replace_host)); | 184 GURL app_url(test_file_url.ReplaceComponents(replace_host)); |
181 | 185 |
182 // Load an app which includes app.com in its extent. | 186 // Load an app which includes app.com in its extent. |
183 const Extension* extension = LoadExtension( | 187 const Extension* extension = LoadExtension( |
184 test_data_dir_.AppendASCII("app_dot_com_app")); | 188 test_data_dir_.AppendASCII("app_dot_com_app")); |
185 ASSERT_TRUE(extension); | 189 ASSERT_TRUE(extension); |
186 | 190 |
187 // Test that normal pages (even apps) cannot use getDetailsForFrame(). | 191 // Test that normal pages (even apps) cannot use getDetailsForFrame(). |
188 ui_test_utils::NavigateToURL(browser(), app_url); | 192 ui_test_utils::NavigateToURL(browser(), app_url); |
189 std::wstring test_unsuccessful_access = | 193 const char kTestUnsuccessfulAccess[] = |
190 L"window.domAutomationController.send(window.testUnsuccessfulAccess())"; | 194 "window.domAutomationController.send(window.testUnsuccessfulAccess())"; |
191 bool result = false; | 195 bool result = false; |
192 ASSERT_TRUE( | 196 ASSERT_TRUE( |
193 content::ExecuteJavaScriptAndExtractBool( | 197 content::ExecuteJavaScriptAndExtractBool( |
194 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 198 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
195 L"", test_unsuccessful_access, &result)); | 199 "", |
| 200 kTestUnsuccessfulAccess, |
| 201 &result)); |
196 EXPECT_TRUE(result); | 202 EXPECT_TRUE(result); |
197 | 203 |
198 // Test that checkout can use getDetailsForFrame() and that it works | 204 // Test that checkout can use getDetailsForFrame() and that it works |
199 // correctly. | 205 // correctly. |
200 ui_test_utils::NavigateToURL(browser(), checkout_url); | 206 ui_test_utils::NavigateToURL(browser(), checkout_url); |
201 std::wstring get_details_for_frame = | 207 const char kGetDetailsForFrame[] = |
202 L"window.domAutomationController.send(" | 208 "window.domAutomationController.send(" |
203 L" JSON.stringify(chrome.app.getDetailsForFrame(frames[0])))"; | 209 " JSON.stringify(chrome.app.getDetailsForFrame(frames[0])))"; |
204 std::string json; | 210 std::string json; |
205 ASSERT_TRUE( | 211 ASSERT_TRUE( |
206 content::ExecuteJavaScriptAndExtractString( | 212 content::ExecuteJavaScriptAndExtractString( |
207 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 213 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
208 L"", get_details_for_frame, &json)); | 214 "", |
| 215 kGetDetailsForFrame, |
| 216 &json)); |
209 | 217 |
210 scoped_ptr<DictionaryValue> app_details( | 218 scoped_ptr<DictionaryValue> app_details( |
211 static_cast<DictionaryValue*>(base::JSONReader::Read(json))); | 219 static_cast<DictionaryValue*>(base::JSONReader::Read(json))); |
212 // extension->manifest() does not contain the id. | 220 // extension->manifest() does not contain the id. |
213 app_details->Remove("id", NULL); | 221 app_details->Remove("id", NULL); |
214 EXPECT_TRUE(app_details.get()); | 222 EXPECT_TRUE(app_details.get()); |
215 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); | 223 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); |
216 } | 224 } |
217 | 225 |
218 | 226 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 EXPECT_EQ("ready_to_run", RunningState()); | 277 EXPECT_EQ("ready_to_run", RunningState()); |
270 EXPECT_FALSE(IsAppInstalled()); | 278 EXPECT_FALSE(IsAppInstalled()); |
271 | 279 |
272 // The non-app URL should still not be installed or running. | 280 // The non-app URL should still not be installed or running. |
273 ui_test_utils::NavigateToURL(browser(), non_app_url); | 281 ui_test_utils::NavigateToURL(browser(), non_app_url); |
274 | 282 |
275 EXPECT_EQ("not_installed", InstallState()); | 283 EXPECT_EQ("not_installed", InstallState()); |
276 EXPECT_EQ("cannot_run", RunningState()); | 284 EXPECT_EQ("cannot_run", RunningState()); |
277 EXPECT_FALSE(IsAppInstalled()); | 285 EXPECT_FALSE(IsAppInstalled()); |
278 | 286 |
279 EXPECT_EQ("installed", InstallState(L"//html/iframe[1]")); | 287 EXPECT_EQ("installed", InstallState("//html/iframe[1]")); |
280 EXPECT_EQ("cannot_run", RunningState(L"//html/iframe[1]")); | 288 EXPECT_EQ("cannot_run", RunningState("//html/iframe[1]")); |
281 EXPECT_FALSE(IsAppInstalled(L"//html/iframe[1]")); | 289 EXPECT_FALSE(IsAppInstalled("//html/iframe[1]")); |
282 | 290 |
283 } | 291 } |
284 | 292 |
285 IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, InstallAndRunningStateFrame) { | 293 IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, InstallAndRunningStateFrame) { |
286 std::string app_host("app.com"); | 294 std::string app_host("app.com"); |
287 std::string non_app_host("nonapp.com"); | 295 std::string non_app_host("nonapp.com"); |
288 | 296 |
289 host_resolver()->AddRule(app_host, "127.0.0.1"); | 297 host_resolver()->AddRule(app_host, "127.0.0.1"); |
290 host_resolver()->AddRule(non_app_host, "127.0.0.1"); | 298 host_resolver()->AddRule(non_app_host, "127.0.0.1"); |
291 ASSERT_TRUE(test_server()->Start()); | 299 ASSERT_TRUE(test_server()->Start()); |
292 | 300 |
293 GURL test_file_url(test_server()->GetURL( | 301 GURL test_file_url(test_server()->GetURL( |
294 "files/extensions/get_app_details_for_frame_reversed.html")); | 302 "files/extensions/get_app_details_for_frame_reversed.html")); |
295 GURL::Replacements replace_host; | 303 GURL::Replacements replace_host; |
296 | 304 |
297 replace_host.SetHostStr(app_host); | 305 replace_host.SetHostStr(app_host); |
298 GURL app_url(test_file_url.ReplaceComponents(replace_host)); | 306 GURL app_url(test_file_url.ReplaceComponents(replace_host)); |
299 | 307 |
300 replace_host.SetHostStr(non_app_host); | 308 replace_host.SetHostStr(non_app_host); |
301 GURL non_app_url(test_file_url.ReplaceComponents(replace_host)); | 309 GURL non_app_url(test_file_url.ReplaceComponents(replace_host)); |
302 | 310 |
303 // Check the install and running state of a non-app iframe running | 311 // Check the install and running state of a non-app iframe running |
304 // within an app. | 312 // within an app. |
305 ui_test_utils::NavigateToURL(browser(), app_url); | 313 ui_test_utils::NavigateToURL(browser(), app_url); |
306 | 314 |
307 EXPECT_EQ("not_installed", InstallState(L"//html/iframe[1]")); | 315 EXPECT_EQ("not_installed", InstallState("//html/iframe[1]")); |
308 EXPECT_EQ("cannot_run", RunningState(L"//html/iframe[1]")); | 316 EXPECT_EQ("cannot_run", RunningState("//html/iframe[1]")); |
309 EXPECT_FALSE(IsAppInstalled(L"//html/iframe[1]")); | 317 EXPECT_FALSE(IsAppInstalled("//html/iframe[1]")); |
310 } | 318 } |
OLD | NEW |