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/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
17 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
18 #include "chrome/common/extensions/manifest.h" | 19 #include "chrome/common/extensions/manifest.h" |
19 #include "chrome/test/base/ui_test_utils.h" | 20 #include "chrome/test/base/ui_test_utils.h" |
20 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
21 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
22 #include "net/base/mock_host_resolver.h" | 23 #include "net/base/mock_host_resolver.h" |
23 | 24 |
24 using extensions::Extension; | 25 using extensions::Extension; |
25 | 26 |
26 class ChromeAppAPITest : public ExtensionBrowserTest { | 27 class ChromeAppAPITest : public ExtensionBrowserTest { |
27 protected: | 28 protected: |
28 bool IsAppInstalled() { return IsAppInstalled(L""); } | 29 bool IsAppInstalled() { return IsAppInstalled(L""); } |
29 bool IsAppInstalled(const std::wstring& frame_xpath) { | 30 bool IsAppInstalled(const std::wstring& frame_xpath) { |
30 std::wstring get_app_is_installed = | 31 std::wstring get_app_is_installed = |
31 L"window.domAutomationController.send(window.chrome.app.isInstalled);"; | 32 L"window.domAutomationController.send(window.chrome.app.isInstalled);"; |
32 bool result; | 33 bool result; |
33 CHECK( | 34 CHECK( |
34 ui_test_utils::ExecuteJavaScriptAndExtractBool( | 35 ui_test_utils::ExecuteJavaScriptAndExtractBool( |
35 browser()->GetActiveWebContents()->GetRenderViewHost(), | 36 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
36 frame_xpath, get_app_is_installed, &result)); | 37 frame_xpath, get_app_is_installed, &result)); |
37 return result; | 38 return result; |
38 } | 39 } |
39 | 40 |
40 std::string InstallState() { return InstallState(L""); } | 41 std::string InstallState() { return InstallState(L""); } |
41 std::string InstallState(const std::wstring& frame_xpath) { | 42 std::string InstallState(const std::wstring& frame_xpath) { |
42 std::wstring get_app_install_state = | 43 std::wstring get_app_install_state = |
43 L"window.chrome.app.installState(" | 44 L"window.chrome.app.installState(" |
44 L"function(s) { window.domAutomationController.send(s); });"; | 45 L"function(s) { window.domAutomationController.send(s); });"; |
45 std::string result; | 46 std::string result; |
46 CHECK( | 47 CHECK( |
47 ui_test_utils::ExecuteJavaScriptAndExtractString( | 48 ui_test_utils::ExecuteJavaScriptAndExtractString( |
48 browser()->GetActiveWebContents()->GetRenderViewHost(), | 49 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
49 frame_xpath, get_app_install_state, &result)); | 50 frame_xpath, get_app_install_state, &result)); |
50 return result; | 51 return result; |
51 } | 52 } |
52 | 53 |
53 std::string RunningState() { return RunningState(L""); } | 54 std::string RunningState() { return RunningState(L""); } |
54 std::string RunningState(const std::wstring& frame_xpath) { | 55 std::string RunningState(const std::wstring& frame_xpath) { |
55 std::wstring get_app_install_state = | 56 std::wstring get_app_install_state = |
56 L"window.domAutomationController.send(" | 57 L"window.domAutomationController.send(" |
57 L"window.chrome.app.runningState());"; | 58 L"window.chrome.app.runningState());"; |
58 std::string result; | 59 std::string result; |
59 CHECK( | 60 CHECK( |
60 ui_test_utils::ExecuteJavaScriptAndExtractString( | 61 ui_test_utils::ExecuteJavaScriptAndExtractString( |
61 browser()->GetActiveWebContents()->GetRenderViewHost(), | 62 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
62 frame_xpath, get_app_install_state, &result)); | 63 frame_xpath, get_app_install_state, &result)); |
63 return result; | 64 return result; |
64 } | 65 } |
65 | 66 |
66 private: | 67 private: |
67 virtual void SetUpCommandLine(CommandLine* command_line) { | 68 virtual void SetUpCommandLine(CommandLine* command_line) { |
68 ExtensionBrowserTest::SetUpCommandLine(command_line); | 69 ExtensionBrowserTest::SetUpCommandLine(command_line); |
69 command_line->AppendSwitchASCII(switches::kAppsCheckoutURL, | 70 command_line->AppendSwitchASCII(switches::kAppsCheckoutURL, |
70 "http://checkout.com:"); | 71 "http://checkout.com:"); |
71 } | 72 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 ui_test_utils::NavigateToURL(browser(), non_app_url); | 106 ui_test_utils::NavigateToURL(browser(), non_app_url); |
106 EXPECT_FALSE(IsAppInstalled()); | 107 EXPECT_FALSE(IsAppInstalled()); |
107 | 108 |
108 // Test that a non-app page returns null for chrome.app.getDetails(). | 109 // Test that a non-app page returns null for chrome.app.getDetails(). |
109 std::wstring get_app_details = | 110 std::wstring get_app_details = |
110 L"window.domAutomationController.send(" | 111 L"window.domAutomationController.send(" |
111 L" JSON.stringify(window.chrome.app.getDetails()));"; | 112 L" JSON.stringify(window.chrome.app.getDetails()));"; |
112 std::string result; | 113 std::string result; |
113 ASSERT_TRUE( | 114 ASSERT_TRUE( |
114 ui_test_utils::ExecuteJavaScriptAndExtractString( | 115 ui_test_utils::ExecuteJavaScriptAndExtractString( |
115 browser()->GetActiveWebContents()->GetRenderViewHost(), | 116 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
116 L"", get_app_details, &result)); | 117 L"", get_app_details, &result)); |
117 EXPECT_EQ("null", result); | 118 EXPECT_EQ("null", result); |
118 | 119 |
119 // Check that an app page has chrome.app.isInstalled = true. | 120 // Check that an app page has chrome.app.isInstalled = true. |
120 ui_test_utils::NavigateToURL(browser(), app_url); | 121 ui_test_utils::NavigateToURL(browser(), app_url); |
121 EXPECT_TRUE(IsAppInstalled()); | 122 EXPECT_TRUE(IsAppInstalled()); |
122 | 123 |
123 // Check that an app page returns the correct result for | 124 // Check that an app page returns the correct result for |
124 // chrome.app.getDetails(). | 125 // chrome.app.getDetails(). |
125 ui_test_utils::NavigateToURL(browser(), app_url); | 126 ui_test_utils::NavigateToURL(browser(), app_url); |
126 ASSERT_TRUE( | 127 ASSERT_TRUE( |
127 ui_test_utils::ExecuteJavaScriptAndExtractString( | 128 ui_test_utils::ExecuteJavaScriptAndExtractString( |
128 browser()->GetActiveWebContents()->GetRenderViewHost(), | 129 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
129 L"", get_app_details, &result)); | 130 L"", get_app_details, &result)); |
130 scoped_ptr<DictionaryValue> app_details( | 131 scoped_ptr<DictionaryValue> app_details( |
131 static_cast<DictionaryValue*>(base::JSONReader::Read(result))); | 132 static_cast<DictionaryValue*>(base::JSONReader::Read(result))); |
132 // extension->manifest() does not contain the id. | 133 // extension->manifest() does not contain the id. |
133 app_details->Remove("id", NULL); | 134 app_details->Remove("id", NULL); |
134 EXPECT_TRUE(app_details.get()); | 135 EXPECT_TRUE(app_details.get()); |
135 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); | 136 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); |
136 | 137 |
137 // Try to change app.isInstalled. Should silently fail, so | 138 // Try to change app.isInstalled. Should silently fail, so |
138 // that isInstalled should have the initial value. | 139 // that isInstalled should have the initial value. |
139 ASSERT_TRUE( | 140 ASSERT_TRUE( |
140 ui_test_utils::ExecuteJavaScriptAndExtractString( | 141 ui_test_utils::ExecuteJavaScriptAndExtractString( |
141 browser()->GetActiveWebContents()->GetRenderViewHost(), | 142 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
142 L"", | 143 L"", |
143 L"window.domAutomationController.send(" | 144 L"window.domAutomationController.send(" |
144 L" function() {" | 145 L" function() {" |
145 L" var value = window.chrome.app.isInstalled;" | 146 L" var value = window.chrome.app.isInstalled;" |
146 L" window.chrome.app.isInstalled = !value;" | 147 L" window.chrome.app.isInstalled = !value;" |
147 L" if (window.chrome.app.isInstalled == value) {" | 148 L" if (window.chrome.app.isInstalled == value) {" |
148 L" return 'true';" | 149 L" return 'true';" |
149 L" } else {" | 150 L" } else {" |
150 L" return 'false';" | 151 L" return 'false';" |
151 L" }" | 152 L" }" |
(...skipping 30 matching lines...) Expand all Loading... |
182 test_data_dir_.AppendASCII("app_dot_com_app")); | 183 test_data_dir_.AppendASCII("app_dot_com_app")); |
183 ASSERT_TRUE(extension); | 184 ASSERT_TRUE(extension); |
184 | 185 |
185 // Test that normal pages (even apps) cannot use getDetailsForFrame(). | 186 // Test that normal pages (even apps) cannot use getDetailsForFrame(). |
186 ui_test_utils::NavigateToURL(browser(), app_url); | 187 ui_test_utils::NavigateToURL(browser(), app_url); |
187 std::wstring test_unsuccessful_access = | 188 std::wstring test_unsuccessful_access = |
188 L"window.domAutomationController.send(window.testUnsuccessfulAccess())"; | 189 L"window.domAutomationController.send(window.testUnsuccessfulAccess())"; |
189 bool result = false; | 190 bool result = false; |
190 ASSERT_TRUE( | 191 ASSERT_TRUE( |
191 ui_test_utils::ExecuteJavaScriptAndExtractBool( | 192 ui_test_utils::ExecuteJavaScriptAndExtractBool( |
192 browser()->GetActiveWebContents()->GetRenderViewHost(), | 193 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
193 L"", test_unsuccessful_access, &result)); | 194 L"", test_unsuccessful_access, &result)); |
194 EXPECT_TRUE(result); | 195 EXPECT_TRUE(result); |
195 | 196 |
196 // Test that checkout can use getDetailsForFrame() and that it works | 197 // Test that checkout can use getDetailsForFrame() and that it works |
197 // correctly. | 198 // correctly. |
198 ui_test_utils::NavigateToURL(browser(), checkout_url); | 199 ui_test_utils::NavigateToURL(browser(), checkout_url); |
199 std::wstring get_details_for_frame = | 200 std::wstring get_details_for_frame = |
200 L"window.domAutomationController.send(" | 201 L"window.domAutomationController.send(" |
201 L" JSON.stringify(chrome.app.getDetailsForFrame(frames[0])))"; | 202 L" JSON.stringify(chrome.app.getDetailsForFrame(frames[0])))"; |
202 std::string json; | 203 std::string json; |
203 ASSERT_TRUE( | 204 ASSERT_TRUE( |
204 ui_test_utils::ExecuteJavaScriptAndExtractString( | 205 ui_test_utils::ExecuteJavaScriptAndExtractString( |
205 browser()->GetActiveWebContents()->GetRenderViewHost(), | 206 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
206 L"", get_details_for_frame, &json)); | 207 L"", get_details_for_frame, &json)); |
207 | 208 |
208 scoped_ptr<DictionaryValue> app_details( | 209 scoped_ptr<DictionaryValue> app_details( |
209 static_cast<DictionaryValue*>(base::JSONReader::Read(json))); | 210 static_cast<DictionaryValue*>(base::JSONReader::Read(json))); |
210 // extension->manifest() does not contain the id. | 211 // extension->manifest() does not contain the id. |
211 app_details->Remove("id", NULL); | 212 app_details->Remove("id", NULL); |
212 EXPECT_TRUE(app_details.get()); | 213 EXPECT_TRUE(app_details.get()); |
213 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); | 214 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); |
214 } | 215 } |
215 | 216 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 GURL non_app_url(test_file_url.ReplaceComponents(replace_host)); | 300 GURL non_app_url(test_file_url.ReplaceComponents(replace_host)); |
300 | 301 |
301 // Check the install and running state of a non-app iframe running | 302 // Check the install and running state of a non-app iframe running |
302 // within an app. | 303 // within an app. |
303 ui_test_utils::NavigateToURL(browser(), app_url); | 304 ui_test_utils::NavigateToURL(browser(), app_url); |
304 | 305 |
305 EXPECT_EQ("not_installed", InstallState(L"//html/iframe[1]")); | 306 EXPECT_EQ("not_installed", InstallState(L"//html/iframe[1]")); |
306 EXPECT_EQ("cannot_run", RunningState(L"//html/iframe[1]")); | 307 EXPECT_EQ("cannot_run", RunningState(L"//html/iframe[1]")); |
307 EXPECT_FALSE(IsAppInstalled(L"//html/iframe[1]")); | 308 EXPECT_FALSE(IsAppInstalled(L"//html/iframe[1]")); |
308 } | 309 } |
OLD | NEW |