Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1001)

Side by Side Diff: chrome/browser/extensions/chrome_app_api_browsertest.cc

Issue 10795090: Move ExecuteJavaScript functions from ui_test_utils.h to browser_test_utils.h so they can be reused… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "googleurl/src/gurl.h" 23 #include "googleurl/src/gurl.h"
23 #include "net/base/mock_host_resolver.h" 24 #include "net/base/mock_host_resolver.h"
24 25
25 using extensions::Extension; 26 using extensions::Extension;
26 27
27 class ChromeAppAPITest : public ExtensionBrowserTest { 28 class ChromeAppAPITest : public ExtensionBrowserTest {
28 protected: 29 protected:
29 bool IsAppInstalled() { return IsAppInstalled(L""); } 30 bool IsAppInstalled() { return IsAppInstalled(L""); }
30 bool IsAppInstalled(const std::wstring& frame_xpath) { 31 bool IsAppInstalled(const std::wstring& frame_xpath) {
31 std::wstring get_app_is_installed = 32 std::wstring get_app_is_installed =
32 L"window.domAutomationController.send(window.chrome.app.isInstalled);"; 33 L"window.domAutomationController.send(window.chrome.app.isInstalled);";
33 bool result; 34 bool result;
34 CHECK( 35 CHECK(
35 ui_test_utils::ExecuteJavaScriptAndExtractBool( 36 content::ExecuteJavaScriptAndExtractBool(
36 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), 37 chrome::GetActiveWebContents(browser())->GetRenderViewHost(),
37 frame_xpath, get_app_is_installed, &result)); 38 frame_xpath, get_app_is_installed, &result));
38 return result; 39 return result;
39 } 40 }
40 41
41 std::string InstallState() { return InstallState(L""); } 42 std::string InstallState() { return InstallState(L""); }
42 std::string InstallState(const std::wstring& frame_xpath) { 43 std::string InstallState(const std::wstring& frame_xpath) {
43 std::wstring get_app_install_state = 44 std::wstring get_app_install_state =
44 L"window.chrome.app.installState(" 45 L"window.chrome.app.installState("
45 L"function(s) { window.domAutomationController.send(s); });"; 46 L"function(s) { window.domAutomationController.send(s); });";
46 std::string result; 47 std::string result;
47 CHECK( 48 CHECK(
48 ui_test_utils::ExecuteJavaScriptAndExtractString( 49 content::ExecuteJavaScriptAndExtractString(
49 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), 50 chrome::GetActiveWebContents(browser())->GetRenderViewHost(),
50 frame_xpath, get_app_install_state, &result)); 51 frame_xpath, get_app_install_state, &result));
51 return result; 52 return result;
52 } 53 }
53 54
54 std::string RunningState() { return RunningState(L""); } 55 std::string RunningState() { return RunningState(L""); }
55 std::string RunningState(const std::wstring& frame_xpath) { 56 std::string RunningState(const std::wstring& frame_xpath) {
56 std::wstring get_app_install_state = 57 std::wstring get_app_install_state =
57 L"window.domAutomationController.send(" 58 L"window.domAutomationController.send("
58 L"window.chrome.app.runningState());"; 59 L"window.chrome.app.runningState());";
59 std::string result; 60 std::string result;
60 CHECK( 61 CHECK(
61 ui_test_utils::ExecuteJavaScriptAndExtractString( 62 content::ExecuteJavaScriptAndExtractString(
62 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), 63 chrome::GetActiveWebContents(browser())->GetRenderViewHost(),
63 frame_xpath, get_app_install_state, &result)); 64 frame_xpath, get_app_install_state, &result));
64 return result; 65 return result;
65 } 66 }
66 67
67 private: 68 private:
68 virtual void SetUpCommandLine(CommandLine* command_line) { 69 virtual void SetUpCommandLine(CommandLine* command_line) {
69 ExtensionBrowserTest::SetUpCommandLine(command_line); 70 ExtensionBrowserTest::SetUpCommandLine(command_line);
70 command_line->AppendSwitchASCII(switches::kAppsCheckoutURL, 71 command_line->AppendSwitchASCII(switches::kAppsCheckoutURL,
71 "http://checkout.com:"); 72 "http://checkout.com:");
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 // Test that a non-app page has chrome.app.isInstalled = false. 106 // Test that a non-app page has chrome.app.isInstalled = false.
106 ui_test_utils::NavigateToURL(browser(), non_app_url); 107 ui_test_utils::NavigateToURL(browser(), non_app_url);
107 EXPECT_FALSE(IsAppInstalled()); 108 EXPECT_FALSE(IsAppInstalled());
108 109
109 // 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().
110 std::wstring get_app_details = 111 std::wstring get_app_details =
111 L"window.domAutomationController.send(" 112 L"window.domAutomationController.send("
112 L" JSON.stringify(window.chrome.app.getDetails()));"; 113 L" JSON.stringify(window.chrome.app.getDetails()));";
113 std::string result; 114 std::string result;
114 ASSERT_TRUE( 115 ASSERT_TRUE(
115 ui_test_utils::ExecuteJavaScriptAndExtractString( 116 content::ExecuteJavaScriptAndExtractString(
116 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), 117 chrome::GetActiveWebContents(browser())->GetRenderViewHost(),
117 L"", get_app_details, &result)); 118 L"", get_app_details, &result));
118 EXPECT_EQ("null", result); 119 EXPECT_EQ("null", result);
119 120
120 // Check that an app page has chrome.app.isInstalled = true. 121 // Check that an app page has chrome.app.isInstalled = true.
121 ui_test_utils::NavigateToURL(browser(), app_url); 122 ui_test_utils::NavigateToURL(browser(), app_url);
122 EXPECT_TRUE(IsAppInstalled()); 123 EXPECT_TRUE(IsAppInstalled());
123 124
124 // Check that an app page returns the correct result for 125 // Check that an app page returns the correct result for
125 // chrome.app.getDetails(). 126 // chrome.app.getDetails().
126 ui_test_utils::NavigateToURL(browser(), app_url); 127 ui_test_utils::NavigateToURL(browser(), app_url);
127 ASSERT_TRUE( 128 ASSERT_TRUE(
128 ui_test_utils::ExecuteJavaScriptAndExtractString( 129 content::ExecuteJavaScriptAndExtractString(
129 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), 130 chrome::GetActiveWebContents(browser())->GetRenderViewHost(),
130 L"", get_app_details, &result)); 131 L"", get_app_details, &result));
131 scoped_ptr<DictionaryValue> app_details( 132 scoped_ptr<DictionaryValue> app_details(
132 static_cast<DictionaryValue*>(base::JSONReader::Read(result))); 133 static_cast<DictionaryValue*>(base::JSONReader::Read(result)));
133 // extension->manifest() does not contain the id. 134 // extension->manifest() does not contain the id.
134 app_details->Remove("id", NULL); 135 app_details->Remove("id", NULL);
135 EXPECT_TRUE(app_details.get()); 136 EXPECT_TRUE(app_details.get());
136 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); 137 EXPECT_TRUE(app_details->Equals(extension->manifest()->value()));
137 138
138 // Try to change app.isInstalled. Should silently fail, so 139 // Try to change app.isInstalled. Should silently fail, so
139 // that isInstalled should have the initial value. 140 // that isInstalled should have the initial value.
140 ASSERT_TRUE( 141 ASSERT_TRUE(
141 ui_test_utils::ExecuteJavaScriptAndExtractString( 142 content::ExecuteJavaScriptAndExtractString(
142 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), 143 chrome::GetActiveWebContents(browser())->GetRenderViewHost(),
143 L"", 144 L"",
144 L"window.domAutomationController.send(" 145 L"window.domAutomationController.send("
145 L" function() {" 146 L" function() {"
146 L" var value = window.chrome.app.isInstalled;" 147 L" var value = window.chrome.app.isInstalled;"
147 L" window.chrome.app.isInstalled = !value;" 148 L" window.chrome.app.isInstalled = !value;"
148 L" if (window.chrome.app.isInstalled == value) {" 149 L" if (window.chrome.app.isInstalled == value) {"
149 L" return 'true';" 150 L" return 'true';"
150 L" } else {" 151 L" } else {"
151 L" return 'false';" 152 L" return 'false';"
(...skipping 30 matching lines...) Expand all
182 const Extension* extension = LoadExtension( 183 const Extension* extension = LoadExtension(
183 test_data_dir_.AppendASCII("app_dot_com_app")); 184 test_data_dir_.AppendASCII("app_dot_com_app"));
184 ASSERT_TRUE(extension); 185 ASSERT_TRUE(extension);
185 186
186 // Test that normal pages (even apps) cannot use getDetailsForFrame(). 187 // Test that normal pages (even apps) cannot use getDetailsForFrame().
187 ui_test_utils::NavigateToURL(browser(), app_url); 188 ui_test_utils::NavigateToURL(browser(), app_url);
188 std::wstring test_unsuccessful_access = 189 std::wstring test_unsuccessful_access =
189 L"window.domAutomationController.send(window.testUnsuccessfulAccess())"; 190 L"window.domAutomationController.send(window.testUnsuccessfulAccess())";
190 bool result = false; 191 bool result = false;
191 ASSERT_TRUE( 192 ASSERT_TRUE(
192 ui_test_utils::ExecuteJavaScriptAndExtractBool( 193 content::ExecuteJavaScriptAndExtractBool(
193 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), 194 chrome::GetActiveWebContents(browser())->GetRenderViewHost(),
194 L"", test_unsuccessful_access, &result)); 195 L"", test_unsuccessful_access, &result));
195 EXPECT_TRUE(result); 196 EXPECT_TRUE(result);
196 197
197 // Test that checkout can use getDetailsForFrame() and that it works 198 // Test that checkout can use getDetailsForFrame() and that it works
198 // correctly. 199 // correctly.
199 ui_test_utils::NavigateToURL(browser(), checkout_url); 200 ui_test_utils::NavigateToURL(browser(), checkout_url);
200 std::wstring get_details_for_frame = 201 std::wstring get_details_for_frame =
201 L"window.domAutomationController.send(" 202 L"window.domAutomationController.send("
202 L" JSON.stringify(chrome.app.getDetailsForFrame(frames[0])))"; 203 L" JSON.stringify(chrome.app.getDetailsForFrame(frames[0])))";
203 std::string json; 204 std::string json;
204 ASSERT_TRUE( 205 ASSERT_TRUE(
205 ui_test_utils::ExecuteJavaScriptAndExtractString( 206 content::ExecuteJavaScriptAndExtractString(
206 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), 207 chrome::GetActiveWebContents(browser())->GetRenderViewHost(),
207 L"", get_details_for_frame, &json)); 208 L"", get_details_for_frame, &json));
208 209
209 scoped_ptr<DictionaryValue> app_details( 210 scoped_ptr<DictionaryValue> app_details(
210 static_cast<DictionaryValue*>(base::JSONReader::Read(json))); 211 static_cast<DictionaryValue*>(base::JSONReader::Read(json)));
211 // extension->manifest() does not contain the id. 212 // extension->manifest() does not contain the id.
212 app_details->Remove("id", NULL); 213 app_details->Remove("id", NULL);
213 EXPECT_TRUE(app_details.get()); 214 EXPECT_TRUE(app_details.get());
214 EXPECT_TRUE(app_details->Equals(extension->manifest()->value())); 215 EXPECT_TRUE(app_details->Equals(extension->manifest()->value()));
215 } 216 }
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 GURL non_app_url(test_file_url.ReplaceComponents(replace_host)); 301 GURL non_app_url(test_file_url.ReplaceComponents(replace_host));
301 302
302 // 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
303 // within an app. 304 // within an app.
304 ui_test_utils::NavigateToURL(browser(), app_url); 305 ui_test_utils::NavigateToURL(browser(), app_url);
305 306
306 EXPECT_EQ("not_installed", InstallState(L"//html/iframe[1]")); 307 EXPECT_EQ("not_installed", InstallState(L"//html/iframe[1]"));
307 EXPECT_EQ("cannot_run", RunningState(L"//html/iframe[1]")); 308 EXPECT_EQ("cannot_run", RunningState(L"//html/iframe[1]"));
308 EXPECT_FALSE(IsAppInstalled(L"//html/iframe[1]")); 309 EXPECT_FALSE(IsAppInstalled(L"//html/iframe[1]"));
309 } 310 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/browser_tag_browsertest.cc ('k') | chrome/browser/extensions/content_script_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698