| 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(""); } | 30 bool IsAppInstalled() { return IsAppInstalled(L""); } |
| 31 bool IsAppInstalled(const char* frame_xpath) { | 31 bool IsAppInstalled(const std::wstring& frame_xpath) { |
| 32 const char kGetAppIsInstalled[] = | 32 std::wstring get_app_is_installed = |
| 33 "window.domAutomationController.send(window.chrome.app.isInstalled);"; | 33 L"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, kGetAppIsInstalled, &result)); | 38 frame_xpath, get_app_is_installed, &result)); |
| 39 return result; | 39 return result; |
| 40 } | 40 } |
| 41 | 41 |
| 42 std::string InstallState() { return InstallState(""); } | 42 std::string InstallState() { return InstallState(L""); } |
| 43 std::string InstallState(const char* frame_xpath) { | 43 std::string InstallState(const std::wstring& frame_xpath) { |
| 44 const char kGetAppInstallState[] = | 44 std::wstring get_app_install_state = |
| 45 "window.chrome.app.installState(" | 45 L"window.chrome.app.installState(" |
| 46 " function(s) { window.domAutomationController.send(s); });"; | 46 L"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, kGetAppInstallState, &result)); | 51 frame_xpath, get_app_install_state, &result)); |
| 52 return result; | 52 return result; |
| 53 } | 53 } |
| 54 | 54 |
| 55 std::string RunningState() { return RunningState(""); } | 55 std::string RunningState() { return RunningState(L""); } |
| 56 std::string RunningState(const char* frame_xpath) { | 56 std::string RunningState(const std::wstring& frame_xpath) { |
| 57 const char kGetAppRunningState[] = | 57 std::wstring get_app_install_state = |
| 58 "window.domAutomationController.send(" | 58 L"window.domAutomationController.send(" |
| 59 " window.chrome.app.runningState());"; | 59 L"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, kGetAppRunningState, &result)); | 64 frame_xpath, get_app_install_state, &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 const char kGetAppDetails[] = | 111 std::wstring get_app_details = |
| 112 "window.domAutomationController.send(" | 112 L"window.domAutomationController.send(" |
| 113 " JSON.stringify(window.chrome.app.getDetails()));"; | 113 L" 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 "", | 118 L"", get_app_details, &result)); |
| 119 kGetAppDetails, | |
| 120 &result)); | |
| 121 EXPECT_EQ("null", result); | 119 EXPECT_EQ("null", result); |
| 122 | 120 |
| 123 // Check that an app page has chrome.app.isInstalled = true. | 121 // Check that an app page has chrome.app.isInstalled = true. |
| 124 ui_test_utils::NavigateToURL(browser(), app_url); | 122 ui_test_utils::NavigateToURL(browser(), app_url); |
| 125 EXPECT_TRUE(IsAppInstalled()); | 123 EXPECT_TRUE(IsAppInstalled()); |
| 126 | 124 |
| 127 // Check that an app page returns the correct result for | 125 // Check that an app page returns the correct result for |
| 128 // chrome.app.getDetails(). | 126 // chrome.app.getDetails(). |
| 129 ui_test_utils::NavigateToURL(browser(), app_url); | 127 ui_test_utils::NavigateToURL(browser(), app_url); |
| 130 ASSERT_TRUE( | 128 ASSERT_TRUE( |
| 131 content::ExecuteJavaScriptAndExtractString( | 129 content::ExecuteJavaScriptAndExtractString( |
| 132 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 130 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
| 133 "", | 131 L"", get_app_details, &result)); |
| 134 kGetAppDetails, | |
| 135 &result)); | |
| 136 scoped_ptr<DictionaryValue> app_details( | 132 scoped_ptr<DictionaryValue> app_details( |
| 137 static_cast<DictionaryValue*>(base::JSONReader::Read(result))); | 133 static_cast<DictionaryValue*>(base::JSONReader::Read(result))); |
| 138 // extension->manifest() does not contain the id. | 134 // extension->manifest() does not contain the id. |
| 139 app_details->Remove("id", NULL); | 135 app_details->Remove("id", NULL); |
| 140 EXPECT_TRUE(app_details.get()); | 136 EXPECT_TRUE(app_details.get()); |
| 141 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); | 137 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); |
| 142 | 138 |
| 143 // Try to change app.isInstalled. Should silently fail, so | 139 // Try to change app.isInstalled. Should silently fail, so |
| 144 // that isInstalled should have the initial value. | 140 // that isInstalled should have the initial value. |
| 145 ASSERT_TRUE( | 141 ASSERT_TRUE( |
| 146 content::ExecuteJavaScriptAndExtractString( | 142 content::ExecuteJavaScriptAndExtractString( |
| 147 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 143 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
| 148 "", | 144 L"", |
| 149 "window.domAutomationController.send(" | 145 L"window.domAutomationController.send(" |
| 150 " function() {" | 146 L" function() {" |
| 151 " var value = window.chrome.app.isInstalled;" | 147 L" var value = window.chrome.app.isInstalled;" |
| 152 " window.chrome.app.isInstalled = !value;" | 148 L" window.chrome.app.isInstalled = !value;" |
| 153 " if (window.chrome.app.isInstalled == value) {" | 149 L" if (window.chrome.app.isInstalled == value) {" |
| 154 " return 'true';" | 150 L" return 'true';" |
| 155 " } else {" | 151 L" } else {" |
| 156 " return 'false';" | 152 L" return 'false';" |
| 157 " }" | 153 L" }" |
| 158 " }()" | 154 L" }()" |
| 159 ");", | 155 L");", |
| 160 &result)); | 156 &result)); |
| 161 | 157 |
| 162 // Should not be able to alter window.chrome.app.isInstalled from javascript"; | 158 // Should not be able to alter window.chrome.app.isInstalled from javascript"; |
| 163 EXPECT_EQ("true", result); | 159 EXPECT_EQ("true", result); |
| 164 } | 160 } |
| 165 | 161 |
| 166 IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, GetDetailsForFrame) { | 162 IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, GetDetailsForFrame) { |
| 167 std::string app_host("app.com"); | 163 std::string app_host("app.com"); |
| 168 std::string nonapp_host("nonapp.com"); | 164 std::string nonapp_host("nonapp.com"); |
| 169 std::string checkout_host("checkout.com"); | 165 std::string checkout_host("checkout.com"); |
| 170 | 166 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 183 replace_host.SetHostStr(app_host); | 179 replace_host.SetHostStr(app_host); |
| 184 GURL app_url(test_file_url.ReplaceComponents(replace_host)); | 180 GURL app_url(test_file_url.ReplaceComponents(replace_host)); |
| 185 | 181 |
| 186 // Load an app which includes app.com in its extent. | 182 // Load an app which includes app.com in its extent. |
| 187 const Extension* extension = LoadExtension( | 183 const Extension* extension = LoadExtension( |
| 188 test_data_dir_.AppendASCII("app_dot_com_app")); | 184 test_data_dir_.AppendASCII("app_dot_com_app")); |
| 189 ASSERT_TRUE(extension); | 185 ASSERT_TRUE(extension); |
| 190 | 186 |
| 191 // Test that normal pages (even apps) cannot use getDetailsForFrame(). | 187 // Test that normal pages (even apps) cannot use getDetailsForFrame(). |
| 192 ui_test_utils::NavigateToURL(browser(), app_url); | 188 ui_test_utils::NavigateToURL(browser(), app_url); |
| 193 const char kTestUnsuccessfulAccess[] = | 189 std::wstring test_unsuccessful_access = |
| 194 "window.domAutomationController.send(window.testUnsuccessfulAccess())"; | 190 L"window.domAutomationController.send(window.testUnsuccessfulAccess())"; |
| 195 bool result = false; | 191 bool result = false; |
| 196 ASSERT_TRUE( | 192 ASSERT_TRUE( |
| 197 content::ExecuteJavaScriptAndExtractBool( | 193 content::ExecuteJavaScriptAndExtractBool( |
| 198 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 194 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
| 199 "", | 195 L"", test_unsuccessful_access, &result)); |
| 200 kTestUnsuccessfulAccess, | |
| 201 &result)); | |
| 202 EXPECT_TRUE(result); | 196 EXPECT_TRUE(result); |
| 203 | 197 |
| 204 // Test that checkout can use getDetailsForFrame() and that it works | 198 // Test that checkout can use getDetailsForFrame() and that it works |
| 205 // correctly. | 199 // correctly. |
| 206 ui_test_utils::NavigateToURL(browser(), checkout_url); | 200 ui_test_utils::NavigateToURL(browser(), checkout_url); |
| 207 const char kGetDetailsForFrame[] = | 201 std::wstring get_details_for_frame = |
| 208 "window.domAutomationController.send(" | 202 L"window.domAutomationController.send(" |
| 209 " JSON.stringify(chrome.app.getDetailsForFrame(frames[0])))"; | 203 L" JSON.stringify(chrome.app.getDetailsForFrame(frames[0])))"; |
| 210 std::string json; | 204 std::string json; |
| 211 ASSERT_TRUE( | 205 ASSERT_TRUE( |
| 212 content::ExecuteJavaScriptAndExtractString( | 206 content::ExecuteJavaScriptAndExtractString( |
| 213 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 207 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
| 214 "", | 208 L"", get_details_for_frame, &json)); |
| 215 kGetDetailsForFrame, | |
| 216 &json)); | |
| 217 | 209 |
| 218 scoped_ptr<DictionaryValue> app_details( | 210 scoped_ptr<DictionaryValue> app_details( |
| 219 static_cast<DictionaryValue*>(base::JSONReader::Read(json))); | 211 static_cast<DictionaryValue*>(base::JSONReader::Read(json))); |
| 220 // extension->manifest() does not contain the id. | 212 // extension->manifest() does not contain the id. |
| 221 app_details->Remove("id", NULL); | 213 app_details->Remove("id", NULL); |
| 222 EXPECT_TRUE(app_details.get()); | 214 EXPECT_TRUE(app_details.get()); |
| 223 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); | 215 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); |
| 224 } | 216 } |
| 225 | 217 |
| 226 | 218 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 EXPECT_EQ("ready_to_run", RunningState()); | 269 EXPECT_EQ("ready_to_run", RunningState()); |
| 278 EXPECT_FALSE(IsAppInstalled()); | 270 EXPECT_FALSE(IsAppInstalled()); |
| 279 | 271 |
| 280 // The non-app URL should still not be installed or running. | 272 // The non-app URL should still not be installed or running. |
| 281 ui_test_utils::NavigateToURL(browser(), non_app_url); | 273 ui_test_utils::NavigateToURL(browser(), non_app_url); |
| 282 | 274 |
| 283 EXPECT_EQ("not_installed", InstallState()); | 275 EXPECT_EQ("not_installed", InstallState()); |
| 284 EXPECT_EQ("cannot_run", RunningState()); | 276 EXPECT_EQ("cannot_run", RunningState()); |
| 285 EXPECT_FALSE(IsAppInstalled()); | 277 EXPECT_FALSE(IsAppInstalled()); |
| 286 | 278 |
| 287 EXPECT_EQ("installed", InstallState("//html/iframe[1]")); | 279 EXPECT_EQ("installed", InstallState(L"//html/iframe[1]")); |
| 288 EXPECT_EQ("cannot_run", RunningState("//html/iframe[1]")); | 280 EXPECT_EQ("cannot_run", RunningState(L"//html/iframe[1]")); |
| 289 EXPECT_FALSE(IsAppInstalled("//html/iframe[1]")); | 281 EXPECT_FALSE(IsAppInstalled(L"//html/iframe[1]")); |
| 290 | 282 |
| 291 } | 283 } |
| 292 | 284 |
| 293 IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, InstallAndRunningStateFrame) { | 285 IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, InstallAndRunningStateFrame) { |
| 294 std::string app_host("app.com"); | 286 std::string app_host("app.com"); |
| 295 std::string non_app_host("nonapp.com"); | 287 std::string non_app_host("nonapp.com"); |
| 296 | 288 |
| 297 host_resolver()->AddRule(app_host, "127.0.0.1"); | 289 host_resolver()->AddRule(app_host, "127.0.0.1"); |
| 298 host_resolver()->AddRule(non_app_host, "127.0.0.1"); | 290 host_resolver()->AddRule(non_app_host, "127.0.0.1"); |
| 299 ASSERT_TRUE(test_server()->Start()); | 291 ASSERT_TRUE(test_server()->Start()); |
| 300 | 292 |
| 301 GURL test_file_url(test_server()->GetURL( | 293 GURL test_file_url(test_server()->GetURL( |
| 302 "files/extensions/get_app_details_for_frame_reversed.html")); | 294 "files/extensions/get_app_details_for_frame_reversed.html")); |
| 303 GURL::Replacements replace_host; | 295 GURL::Replacements replace_host; |
| 304 | 296 |
| 305 replace_host.SetHostStr(app_host); | 297 replace_host.SetHostStr(app_host); |
| 306 GURL app_url(test_file_url.ReplaceComponents(replace_host)); | 298 GURL app_url(test_file_url.ReplaceComponents(replace_host)); |
| 307 | 299 |
| 308 replace_host.SetHostStr(non_app_host); | 300 replace_host.SetHostStr(non_app_host); |
| 309 GURL non_app_url(test_file_url.ReplaceComponents(replace_host)); | 301 GURL non_app_url(test_file_url.ReplaceComponents(replace_host)); |
| 310 | 302 |
| 311 // Check the install and running state of a non-app iframe running | 303 // Check the install and running state of a non-app iframe running |
| 312 // within an app. | 304 // within an app. |
| 313 ui_test_utils::NavigateToURL(browser(), app_url); | 305 ui_test_utils::NavigateToURL(browser(), app_url); |
| 314 | 306 |
| 315 EXPECT_EQ("not_installed", InstallState("//html/iframe[1]")); | 307 EXPECT_EQ("not_installed", InstallState(L"//html/iframe[1]")); |
| 316 EXPECT_EQ("cannot_run", RunningState("//html/iframe[1]")); | 308 EXPECT_EQ("cannot_run", RunningState(L"//html/iframe[1]")); |
| 317 EXPECT_FALSE(IsAppInstalled("//html/iframe[1]")); | 309 EXPECT_FALSE(IsAppInstalled(L"//html/iframe[1]")); |
| 318 } | 310 } |
| OLD | NEW |