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

Side by Side Diff: chrome/test/ui_test_utils.cc

Issue 101013: Migrating the SSL UI tests to be browser tests (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 7 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
« no previous file with comments | « chrome/test/ui_test_utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 "chrome/test/ui_test_utils.h" 5 #include "chrome/test/ui_test_utils.h"
6 6
7 #include "base/json_reader.h"
7 #include "base/message_loop.h" 8 #include "base/message_loop.h"
8 #include "chrome/browser/browser.h" 9 #include "chrome/browser/browser.h"
9 #include "chrome/browser/dom_operation_notification_details.h" 10 #include "chrome/browser/dom_operation_notification_details.h"
10 #include "chrome/browser/tab_contents/navigation_controller.h" 11 #include "chrome/browser/tab_contents/navigation_controller.h"
11 #include "chrome/browser/tab_contents/web_contents.h" 12 #include "chrome/browser/tab_contents/tab_contents.h"
12 #include "chrome/common/notification_registrar.h" 13 #include "chrome/common/notification_registrar.h"
13 #include "chrome/common/notification_service.h" 14 #include "chrome/common/notification_service.h"
14 #include "chrome/views/widget/accelerator_handler.h" 15 #include "chrome/views/widget/accelerator_handler.h"
15 #include "googleurl/src/gurl.h" 16 #include "googleurl/src/gurl.h"
16 17
17 namespace ui_test_utils { 18 namespace ui_test_utils {
18 19
19 namespace { 20 namespace {
20 21
21 // Used to block until a navigation completes. 22 // Used to block until a navigation completes.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 59
59 // The number of navigations that have been completed. 60 // The number of navigations that have been completed.
60 int navigations_completed_; 61 int navigations_completed_;
61 62
62 // The number of navigations to wait for. 63 // The number of navigations to wait for.
63 int number_of_navigations_; 64 int number_of_navigations_;
64 65
65 DISALLOW_COPY_AND_ASSIGN(NavigationNotificationObserver); 66 DISALLOW_COPY_AND_ASSIGN(NavigationNotificationObserver);
66 }; 67 };
67 68
69 class DOMOperationObserver : public NotificationObserver {
70 public:
71 explicit DOMOperationObserver(TabContents* tab_contents) {
72 registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE,
73 Source<TabContents>(tab_contents));
74 RunMessageLoop();
75 }
76
77 virtual void Observe(NotificationType type,
78 const NotificationSource& source,
79 const NotificationDetails& details) {
80 DCHECK(type == NotificationType::DOM_OPERATION_RESPONSE);
81 Details<DomOperationNotificationDetails> dom_op_details(details);
82 response_ = dom_op_details->json();
83 MessageLoopForUI::current()->Quit();
84 }
85
86 std::string response() const { return response_; }
87
88 private:
89 NotificationRegistrar registrar_;
90 std::string response_;
91
92 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver);
93 };
94
68 } // namespace 95 } // namespace
69 96
70 void RunMessageLoop() { 97 void RunMessageLoop() {
71 MessageLoopForUI* loop = MessageLoopForUI::current(); 98 MessageLoopForUI* loop = MessageLoopForUI::current();
72 bool did_allow_task_nesting = loop->NestableTasksAllowed(); 99 bool did_allow_task_nesting = loop->NestableTasksAllowed();
73 loop->SetNestableTasksAllowed(true); 100 loop->SetNestableTasksAllowed(true);
74 views::AcceleratorHandler handler; 101 views::AcceleratorHandler handler;
75 loop->Run(&handler); 102 loop->Run(&handler);
76 loop->SetNestableTasksAllowed(did_allow_task_nesting); 103 loop->SetNestableTasksAllowed(did_allow_task_nesting);
77 } 104 }
(...skipping 13 matching lines...) Expand all
91 118
92 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, 119 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser,
93 const GURL& url, 120 const GURL& url,
94 int number_of_navigations) { 121 int number_of_navigations) {
95 NavigationController* controller = 122 NavigationController* controller =
96 &browser->GetSelectedTabContents()->controller(); 123 &browser->GetSelectedTabContents()->controller();
97 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED); 124 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
98 WaitForNavigations(controller, number_of_navigations); 125 WaitForNavigations(controller, number_of_navigations);
99 } 126 }
100 127
101 JavaScriptRunner::JavaScriptRunner(WebContents* web_contents, 128 Value* ExecuteJavaScript(TabContents* tab_contents,
102 const std::wstring& frame_xpath, 129 const std::wstring& frame_xpath,
103 const std::wstring& jscript) 130 const std::wstring& original_script) {
104 : web_contents_(web_contents), 131 // TODO(jcampan): we should make the domAutomationController not require an
105 frame_xpath_(frame_xpath), 132 // automation id.
106 jscript_(jscript) { 133 std::wstring script = L"window.domAutomationController.setAutomationId(0);" +
107 NotificationService::current()-> 134 original_script;
108 AddObserver(this, NotificationType::DOM_OPERATION_RESPONSE, 135 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(frame_xpath,
109 Source<WebContents>(web_contents)); 136 script);
137 DOMOperationObserver dom_op_observer(tab_contents);
138 std::string json = dom_op_observer.response();
139 // Wrap |json| in an array before deserializing because valid JSON has an
140 // array or an object as the root.
141 json.insert(0, "[");
142 json.append("]");
143
144 scoped_ptr<Value> root_val(JSONReader::Read(json, true));
145 if (!root_val->IsType(Value::TYPE_LIST))
146 return NULL;
147
148 ListValue* list = static_cast<ListValue*>(root_val.get());
149 Value* result;
150 if (!list || !list->GetSize() ||
151 !list->Remove(0, &result)) // Remove gives us ownership of the value.
152 return NULL;
153
154 return result;
110 } 155 }
111 156
112 void JavaScriptRunner::Observe(NotificationType type, 157 bool ExecuteJavaScriptAndExtractInt(TabContents* tab_contents,
113 const NotificationSource& source, 158 const std::wstring& frame_xpath,
114 const NotificationDetails& details) { 159 const std::wstring& script,
115 Details<DomOperationNotificationDetails> dom_op_details(details); 160 int* result) {
116 result_ = dom_op_details->json(); 161 DCHECK(result);
117 // The Jasonified response has quotes, remove them. 162 scoped_ptr<Value> value(ExecuteJavaScript(tab_contents, frame_xpath, script));
118 if (result_.length() > 1 && result_[0] == '"') 163 if (!value.get())
119 result_ = result_.substr(1, result_.length() - 2); 164 return false;
120 165
121 NotificationService::current()-> 166 return value->GetAsInteger(result);
122 RemoveObserver(this, NotificationType::DOM_OPERATION_RESPONSE,
123 Source<WebContents>(web_contents_));
124 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
125 } 167 }
126 168
127 std::string JavaScriptRunner::Run() { 169 bool ExecuteJavaScriptAndExtractBool(TabContents* tab_contents,
128 // The DOMAutomationController requires an automation ID, even though we are 170 const std::wstring& frame_xpath,
129 // not using it in this case. 171 const std::wstring& script,
130 web_contents_->render_view_host()->ExecuteJavascriptInWebFrame( 172 bool* result) {
131 frame_xpath_, L"window.domAutomationController.setAutomationId(0);"); 173 DCHECK(result);
174 scoped_ptr<Value> value(ExecuteJavaScript(tab_contents, frame_xpath, script));
175 if (!value.get())
176 return false;
132 177
133 web_contents_->render_view_host()->ExecuteJavascriptInWebFrame(frame_xpath_, 178 return value->GetAsBoolean(result);
134 jscript_);
135 ui_test_utils::RunMessageLoop();
136 return result_;
137 } 179 }
138 180
181 bool ExecuteJavaScriptAndExtractString(TabContents* tab_contents,
182 const std::wstring& frame_xpath,
183 const std::wstring& script,
184 std::string* result) {
185 DCHECK(result);
186 scoped_ptr<Value> value(ExecuteJavaScript(tab_contents, frame_xpath, script));
187 if (!value.get())
188 return false;
189
190 return value->GetAsString(result);
191 }
139 } // namespace ui_test_utils 192 } // namespace ui_test_utils
OLDNEW
« no previous file with comments | « chrome/test/ui_test_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698