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" |
(...skipping 15 matching lines...) Expand all Loading... |
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(""); } | 30 bool IsAppInstalled() { return IsAppInstalled(""); } |
31 bool IsAppInstalled(const char* frame_xpath) { | 31 bool IsAppInstalled(const char* frame_xpath) { |
32 const char kGetAppIsInstalled[] = | 32 const char kGetAppIsInstalled[] = |
33 "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::ExecuteScriptInFrameAndExtractBool( |
37 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 37 chrome::GetActiveWebContents(browser()), |
38 frame_xpath, kGetAppIsInstalled, &result)); | 38 frame_xpath, |
| 39 kGetAppIsInstalled, |
| 40 &result)); |
39 return result; | 41 return result; |
40 } | 42 } |
41 | 43 |
42 std::string InstallState() { return InstallState(""); } | 44 std::string InstallState() { return InstallState(""); } |
43 std::string InstallState(const char* frame_xpath) { | 45 std::string InstallState(const char* frame_xpath) { |
44 const char kGetAppInstallState[] = | 46 const char kGetAppInstallState[] = |
45 "window.chrome.app.installState(" | 47 "window.chrome.app.installState(" |
46 " function(s) { window.domAutomationController.send(s); });"; | 48 " function(s) { window.domAutomationController.send(s); });"; |
47 std::string result; | 49 std::string result; |
48 CHECK( | 50 CHECK( |
49 content::ExecuteJavaScriptAndExtractString( | 51 content::ExecuteScriptInFrameAndExtractString( |
50 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 52 chrome::GetActiveWebContents(browser()), |
51 frame_xpath, kGetAppInstallState, &result)); | 53 frame_xpath, |
| 54 kGetAppInstallState, |
| 55 &result)); |
52 return result; | 56 return result; |
53 } | 57 } |
54 | 58 |
55 std::string RunningState() { return RunningState(""); } | 59 std::string RunningState() { return RunningState(""); } |
56 std::string RunningState(const char* frame_xpath) { | 60 std::string RunningState(const char* frame_xpath) { |
57 const char kGetAppRunningState[] = | 61 const char kGetAppRunningState[] = |
58 "window.domAutomationController.send(" | 62 "window.domAutomationController.send(" |
59 " window.chrome.app.runningState());"; | 63 " window.chrome.app.runningState());"; |
60 std::string result; | 64 std::string result; |
61 CHECK( | 65 CHECK( |
62 content::ExecuteJavaScriptAndExtractString( | 66 content::ExecuteScriptInFrameAndExtractString( |
63 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 67 chrome::GetActiveWebContents(browser()), |
64 frame_xpath, kGetAppRunningState, &result)); | 68 frame_xpath, |
| 69 kGetAppRunningState, |
| 70 &result)); |
65 return result; | 71 return result; |
66 } | 72 } |
67 | 73 |
68 private: | 74 private: |
69 virtual void SetUpCommandLine(CommandLine* command_line) { | 75 virtual void SetUpCommandLine(CommandLine* command_line) { |
70 ExtensionBrowserTest::SetUpCommandLine(command_line); | 76 ExtensionBrowserTest::SetUpCommandLine(command_line); |
71 command_line->AppendSwitchASCII(switches::kAppsCheckoutURL, | 77 command_line->AppendSwitchASCII(switches::kAppsCheckoutURL, |
72 "http://checkout.com:"); | 78 "http://checkout.com:"); |
73 } | 79 } |
74 }; | 80 }; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 // Test that a non-app page has chrome.app.isInstalled = false. | 112 // Test that a non-app page has chrome.app.isInstalled = false. |
107 ui_test_utils::NavigateToURL(browser(), non_app_url); | 113 ui_test_utils::NavigateToURL(browser(), non_app_url); |
108 EXPECT_FALSE(IsAppInstalled()); | 114 EXPECT_FALSE(IsAppInstalled()); |
109 | 115 |
110 // Test that a non-app page returns null for chrome.app.getDetails(). | 116 // Test that a non-app page returns null for chrome.app.getDetails(). |
111 const char kGetAppDetails[] = | 117 const char kGetAppDetails[] = |
112 "window.domAutomationController.send(" | 118 "window.domAutomationController.send(" |
113 " JSON.stringify(window.chrome.app.getDetails()));"; | 119 " JSON.stringify(window.chrome.app.getDetails()));"; |
114 std::string result; | 120 std::string result; |
115 ASSERT_TRUE( | 121 ASSERT_TRUE( |
116 content::ExecuteJavaScriptAndExtractString( | 122 content::ExecuteScriptAndExtractString( |
117 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 123 chrome::GetActiveWebContents(browser()), |
118 "", | |
119 kGetAppDetails, | 124 kGetAppDetails, |
120 &result)); | 125 &result)); |
121 EXPECT_EQ("null", result); | 126 EXPECT_EQ("null", result); |
122 | 127 |
123 // Check that an app page has chrome.app.isInstalled = true. | 128 // Check that an app page has chrome.app.isInstalled = true. |
124 ui_test_utils::NavigateToURL(browser(), app_url); | 129 ui_test_utils::NavigateToURL(browser(), app_url); |
125 EXPECT_TRUE(IsAppInstalled()); | 130 EXPECT_TRUE(IsAppInstalled()); |
126 | 131 |
127 // Check that an app page returns the correct result for | 132 // Check that an app page returns the correct result for |
128 // chrome.app.getDetails(). | 133 // chrome.app.getDetails(). |
129 ui_test_utils::NavigateToURL(browser(), app_url); | 134 ui_test_utils::NavigateToURL(browser(), app_url); |
130 ASSERT_TRUE( | 135 ASSERT_TRUE( |
131 content::ExecuteJavaScriptAndExtractString( | 136 content::ExecuteScriptAndExtractString( |
132 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 137 chrome::GetActiveWebContents(browser()), |
133 "", | |
134 kGetAppDetails, | 138 kGetAppDetails, |
135 &result)); | 139 &result)); |
136 scoped_ptr<DictionaryValue> app_details( | 140 scoped_ptr<DictionaryValue> app_details( |
137 static_cast<DictionaryValue*>(base::JSONReader::Read(result))); | 141 static_cast<DictionaryValue*>(base::JSONReader::Read(result))); |
138 // extension->manifest() does not contain the id. | 142 // extension->manifest() does not contain the id. |
139 app_details->Remove("id", NULL); | 143 app_details->Remove("id", NULL); |
140 EXPECT_TRUE(app_details.get()); | 144 EXPECT_TRUE(app_details.get()); |
141 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); | 145 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); |
142 | 146 |
143 // Try to change app.isInstalled. Should silently fail, so | 147 // Try to change app.isInstalled. Should silently fail, so |
144 // that isInstalled should have the initial value. | 148 // that isInstalled should have the initial value. |
145 ASSERT_TRUE( | 149 ASSERT_TRUE( |
146 content::ExecuteJavaScriptAndExtractString( | 150 content::ExecuteScriptAndExtractString( |
147 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 151 chrome::GetActiveWebContents(browser()), |
148 "", | |
149 "window.domAutomationController.send(" | 152 "window.domAutomationController.send(" |
150 " function() {" | 153 " function() {" |
151 " var value = window.chrome.app.isInstalled;" | 154 " var value = window.chrome.app.isInstalled;" |
152 " window.chrome.app.isInstalled = !value;" | 155 " window.chrome.app.isInstalled = !value;" |
153 " if (window.chrome.app.isInstalled == value) {" | 156 " if (window.chrome.app.isInstalled == value) {" |
154 " return 'true';" | 157 " return 'true';" |
155 " } else {" | 158 " } else {" |
156 " return 'false';" | 159 " return 'false';" |
157 " }" | 160 " }" |
158 " }()" | 161 " }()" |
(...skipping 28 matching lines...) Expand all Loading... |
187 const Extension* extension = LoadExtension( | 190 const Extension* extension = LoadExtension( |
188 test_data_dir_.AppendASCII("app_dot_com_app")); | 191 test_data_dir_.AppendASCII("app_dot_com_app")); |
189 ASSERT_TRUE(extension); | 192 ASSERT_TRUE(extension); |
190 | 193 |
191 // Test that normal pages (even apps) cannot use getDetailsForFrame(). | 194 // Test that normal pages (even apps) cannot use getDetailsForFrame(). |
192 ui_test_utils::NavigateToURL(browser(), app_url); | 195 ui_test_utils::NavigateToURL(browser(), app_url); |
193 const char kTestUnsuccessfulAccess[] = | 196 const char kTestUnsuccessfulAccess[] = |
194 "window.domAutomationController.send(window.testUnsuccessfulAccess())"; | 197 "window.domAutomationController.send(window.testUnsuccessfulAccess())"; |
195 bool result = false; | 198 bool result = false; |
196 ASSERT_TRUE( | 199 ASSERT_TRUE( |
197 content::ExecuteJavaScriptAndExtractBool( | 200 content::ExecuteScriptAndExtractBool( |
198 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 201 chrome::GetActiveWebContents(browser()), |
199 "", | |
200 kTestUnsuccessfulAccess, | 202 kTestUnsuccessfulAccess, |
201 &result)); | 203 &result)); |
202 EXPECT_TRUE(result); | 204 EXPECT_TRUE(result); |
203 | 205 |
204 // Test that checkout can use getDetailsForFrame() and that it works | 206 // Test that checkout can use getDetailsForFrame() and that it works |
205 // correctly. | 207 // correctly. |
206 ui_test_utils::NavigateToURL(browser(), checkout_url); | 208 ui_test_utils::NavigateToURL(browser(), checkout_url); |
207 const char kGetDetailsForFrame[] = | 209 const char kGetDetailsForFrame[] = |
208 "window.domAutomationController.send(" | 210 "window.domAutomationController.send(" |
209 " JSON.stringify(chrome.app.getDetailsForFrame(frames[0])))"; | 211 " JSON.stringify(chrome.app.getDetailsForFrame(frames[0])))"; |
210 std::string json; | 212 std::string json; |
211 ASSERT_TRUE( | 213 ASSERT_TRUE( |
212 content::ExecuteJavaScriptAndExtractString( | 214 content::ExecuteScriptAndExtractString( |
213 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 215 chrome::GetActiveWebContents(browser()), |
214 "", | |
215 kGetDetailsForFrame, | 216 kGetDetailsForFrame, |
216 &json)); | 217 &json)); |
217 | 218 |
218 scoped_ptr<DictionaryValue> app_details( | 219 scoped_ptr<DictionaryValue> app_details( |
219 static_cast<DictionaryValue*>(base::JSONReader::Read(json))); | 220 static_cast<DictionaryValue*>(base::JSONReader::Read(json))); |
220 // extension->manifest() does not contain the id. | 221 // extension->manifest() does not contain the id. |
221 app_details->Remove("id", NULL); | 222 app_details->Remove("id", NULL); |
222 EXPECT_TRUE(app_details.get()); | 223 EXPECT_TRUE(app_details.get()); |
223 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); | 224 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); |
224 } | 225 } |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 GURL non_app_url(test_file_url.ReplaceComponents(replace_host)); | 310 GURL non_app_url(test_file_url.ReplaceComponents(replace_host)); |
310 | 311 |
311 // Check the install and running state of a non-app iframe running | 312 // Check the install and running state of a non-app iframe running |
312 // within an app. | 313 // within an app. |
313 ui_test_utils::NavigateToURL(browser(), app_url); | 314 ui_test_utils::NavigateToURL(browser(), app_url); |
314 | 315 |
315 EXPECT_EQ("not_installed", InstallState("//html/iframe[1]")); | 316 EXPECT_EQ("not_installed", InstallState("//html/iframe[1]")); |
316 EXPECT_EQ("cannot_run", RunningState("//html/iframe[1]")); | 317 EXPECT_EQ("cannot_run", RunningState("//html/iframe[1]")); |
317 EXPECT_FALSE(IsAppInstalled("//html/iframe[1]")); | 318 EXPECT_FALSE(IsAppInstalled("//html/iframe[1]")); |
318 } | 319 } |
OLD | NEW |