| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/common/extensions/manifest.h" | 16 #include "chrome/common/extensions/manifest.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" | 17 #include "chrome/test/base/ui_test_utils.h" |
| 18 #include "content/browser/tab_contents/tab_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 19 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
| 20 #include "net/base/mock_host_resolver.h" | 20 #include "net/base/mock_host_resolver.h" |
| 21 | 21 |
| 22 class ChromeAppAPITest : public ExtensionBrowserTest { | 22 class ChromeAppAPITest : public ExtensionBrowserTest { |
| 23 protected: | 23 protected: |
| 24 bool IsAppInstalled() { | 24 bool IsAppInstalled() { |
| 25 std::wstring get_app_is_installed = | 25 std::wstring get_app_is_installed = |
| 26 L"window.domAutomationController.send(window.chrome.app.isInstalled);"; | 26 L"window.domAutomationController.send(window.chrome.app.isInstalled);"; |
| 27 bool result; | 27 bool result; |
| 28 CHECK( | 28 CHECK( |
| 29 ui_test_utils::ExecuteJavaScriptAndExtractBool( | 29 ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 30 browser()->GetSelectedTabContents()->GetRenderViewHost(), | 30 browser()->GetSelectedWebContents()->GetRenderViewHost(), |
| 31 L"", get_app_is_installed, &result)); | 31 L"", get_app_is_installed, &result)); |
| 32 return result; | 32 return result; |
| 33 } | 33 } |
| 34 | 34 |
| 35 private: | 35 private: |
| 36 virtual void SetUpCommandLine(CommandLine* command_line) { | 36 virtual void SetUpCommandLine(CommandLine* command_line) { |
| 37 ExtensionBrowserTest::SetUpCommandLine(command_line); | 37 ExtensionBrowserTest::SetUpCommandLine(command_line); |
| 38 command_line->AppendSwitchASCII(switches::kAppsCheckoutURL, | 38 command_line->AppendSwitchASCII(switches::kAppsCheckoutURL, |
| 39 "http://checkout.com:"); | 39 "http://checkout.com:"); |
| 40 } | 40 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 ui_test_utils::NavigateToURL(browser(), non_app_url); | 74 ui_test_utils::NavigateToURL(browser(), non_app_url); |
| 75 EXPECT_FALSE(IsAppInstalled()); | 75 EXPECT_FALSE(IsAppInstalled()); |
| 76 | 76 |
| 77 // Test that a non-app page returns null for chrome.app.getDetails(). | 77 // Test that a non-app page returns null for chrome.app.getDetails(). |
| 78 std::wstring get_app_details = | 78 std::wstring get_app_details = |
| 79 L"window.domAutomationController.send(" | 79 L"window.domAutomationController.send(" |
| 80 L" JSON.stringify(window.chrome.app.getDetails()));"; | 80 L" JSON.stringify(window.chrome.app.getDetails()));"; |
| 81 std::string result; | 81 std::string result; |
| 82 ASSERT_TRUE( | 82 ASSERT_TRUE( |
| 83 ui_test_utils::ExecuteJavaScriptAndExtractString( | 83 ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 84 browser()->GetSelectedTabContents()->GetRenderViewHost(), | 84 browser()->GetSelectedWebContents()->GetRenderViewHost(), |
| 85 L"", get_app_details, &result)); | 85 L"", get_app_details, &result)); |
| 86 EXPECT_EQ("null", result); | 86 EXPECT_EQ("null", result); |
| 87 | 87 |
| 88 // Check that an app page has chrome.app.isInstalled = true. | 88 // Check that an app page has chrome.app.isInstalled = true. |
| 89 ui_test_utils::NavigateToURL(browser(), app_url); | 89 ui_test_utils::NavigateToURL(browser(), app_url); |
| 90 EXPECT_TRUE(IsAppInstalled()); | 90 EXPECT_TRUE(IsAppInstalled()); |
| 91 | 91 |
| 92 // Check that an app page returns the correct result for | 92 // Check that an app page returns the correct result for |
| 93 // chrome.app.getDetails(). | 93 // chrome.app.getDetails(). |
| 94 ui_test_utils::NavigateToURL(browser(), app_url); | 94 ui_test_utils::NavigateToURL(browser(), app_url); |
| 95 ASSERT_TRUE( | 95 ASSERT_TRUE( |
| 96 ui_test_utils::ExecuteJavaScriptAndExtractString( | 96 ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 97 browser()->GetSelectedTabContents()->GetRenderViewHost(), | 97 browser()->GetSelectedWebContents()->GetRenderViewHost(), |
| 98 L"", get_app_details, &result)); | 98 L"", get_app_details, &result)); |
| 99 scoped_ptr<DictionaryValue> app_details( | 99 scoped_ptr<DictionaryValue> app_details( |
| 100 static_cast<DictionaryValue*>( | 100 static_cast<DictionaryValue*>( |
| 101 base::JSONReader::Read(result, false /* allow trailing comma */))); | 101 base::JSONReader::Read(result, false /* allow trailing comma */))); |
| 102 // extension->manifest() does not contain the id. | 102 // extension->manifest() does not contain the id. |
| 103 app_details->Remove("id", NULL); | 103 app_details->Remove("id", NULL); |
| 104 EXPECT_TRUE(app_details.get()); | 104 EXPECT_TRUE(app_details.get()); |
| 105 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); | 105 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); |
| 106 | 106 |
| 107 // Try to change app.isInstalled. Should silently fail, so | 107 // Try to change app.isInstalled. Should silently fail, so |
| 108 // that isInstalled should have the initial value. | 108 // that isInstalled should have the initial value. |
| 109 ASSERT_TRUE( | 109 ASSERT_TRUE( |
| 110 ui_test_utils::ExecuteJavaScriptAndExtractString( | 110 ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 111 browser()->GetSelectedTabContents()->GetRenderViewHost(), | 111 browser()->GetSelectedWebContents()->GetRenderViewHost(), |
| 112 L"", | 112 L"", |
| 113 L"window.domAutomationController.send(" | 113 L"window.domAutomationController.send(" |
| 114 L" function() {" | 114 L" function() {" |
| 115 L" var value = window.chrome.app.isInstalled;" | 115 L" var value = window.chrome.app.isInstalled;" |
| 116 L" window.chrome.app.isInstalled = !value;" | 116 L" window.chrome.app.isInstalled = !value;" |
| 117 L" if (window.chrome.app.isInstalled == value) {" | 117 L" if (window.chrome.app.isInstalled == value) {" |
| 118 L" return 'true';" | 118 L" return 'true';" |
| 119 L" } else {" | 119 L" } else {" |
| 120 L" return 'false';" | 120 L" return 'false';" |
| 121 L" }" | 121 L" }" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 152 test_data_dir_.AppendASCII("app_dot_com_app")); | 152 test_data_dir_.AppendASCII("app_dot_com_app")); |
| 153 ASSERT_TRUE(extension); | 153 ASSERT_TRUE(extension); |
| 154 | 154 |
| 155 // Test that normal pages (even apps) cannot use getDetailsForFrame(). | 155 // Test that normal pages (even apps) cannot use getDetailsForFrame(). |
| 156 ui_test_utils::NavigateToURL(browser(), app_url); | 156 ui_test_utils::NavigateToURL(browser(), app_url); |
| 157 std::wstring test_unsuccessful_access = | 157 std::wstring test_unsuccessful_access = |
| 158 L"window.domAutomationController.send(window.testUnsuccessfulAccess())"; | 158 L"window.domAutomationController.send(window.testUnsuccessfulAccess())"; |
| 159 bool result = false; | 159 bool result = false; |
| 160 ASSERT_TRUE( | 160 ASSERT_TRUE( |
| 161 ui_test_utils::ExecuteJavaScriptAndExtractBool( | 161 ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 162 browser()->GetSelectedTabContents()->GetRenderViewHost(), | 162 browser()->GetSelectedWebContents()->GetRenderViewHost(), |
| 163 L"", test_unsuccessful_access, &result)); | 163 L"", test_unsuccessful_access, &result)); |
| 164 EXPECT_TRUE(result); | 164 EXPECT_TRUE(result); |
| 165 | 165 |
| 166 // Test that checkout can use getDetailsForFrame() and that it works | 166 // Test that checkout can use getDetailsForFrame() and that it works |
| 167 // correctly. | 167 // correctly. |
| 168 ui_test_utils::NavigateToURL(browser(), checkout_url); | 168 ui_test_utils::NavigateToURL(browser(), checkout_url); |
| 169 std::wstring get_details_for_frame = | 169 std::wstring get_details_for_frame = |
| 170 L"window.domAutomationController.send(" | 170 L"window.domAutomationController.send(" |
| 171 L" JSON.stringify(chrome.app.getDetailsForFrame(frames[0])))"; | 171 L" JSON.stringify(chrome.app.getDetailsForFrame(frames[0])))"; |
| 172 std::string json; | 172 std::string json; |
| 173 ASSERT_TRUE( | 173 ASSERT_TRUE( |
| 174 ui_test_utils::ExecuteJavaScriptAndExtractString( | 174 ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 175 browser()->GetSelectedTabContents()->GetRenderViewHost(), | 175 browser()->GetSelectedWebContents()->GetRenderViewHost(), |
| 176 L"", get_details_for_frame, &json)); | 176 L"", get_details_for_frame, &json)); |
| 177 | 177 |
| 178 scoped_ptr<DictionaryValue> app_details( | 178 scoped_ptr<DictionaryValue> app_details( |
| 179 static_cast<DictionaryValue*>( | 179 static_cast<DictionaryValue*>( |
| 180 base::JSONReader::Read(json, false /* allow trailing comma */))); | 180 base::JSONReader::Read(json, false /* allow trailing comma */))); |
| 181 // extension->manifest() does not contain the id. | 181 // extension->manifest() does not contain the id. |
| 182 app_details->Remove("id", NULL); | 182 app_details->Remove("id", NULL); |
| 183 EXPECT_TRUE(app_details.get()); | 183 EXPECT_TRUE(app_details.get()); |
| 184 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); | 184 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); |
| 185 } | 185 } |
| OLD | NEW |