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

Side by Side Diff: chrome/browser/ui/webui/web_ui_test_handler.cc

Issue 7146024: Use ExecuteJavascriptInWebFrameNotifyResult to return results even in error condition (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sorted includes. Created 9 years, 6 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) 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 "chrome/browser/ui/webui/web_ui_test_handler.h" 5 #include "chrome/browser/ui/webui/web_ui_test_handler.h"
6 6
7 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "chrome/test/ui_test_utils.h" 9 #include "chrome/test/ui_test_utils.h"
10 #include "content/browser/renderer_host/render_view_host.h" 10 #include "content/browser/renderer_host/render_view_host.h"
11 #include "content/common/notification_details.h"
12 #include "content/common/notification_registrar.h"
11 13
12 bool WebUITestHandler::RunJavascript(const std::string& js_test, 14 bool WebUITestHandler::RunJavascript(const std::string& js_test,
13 bool is_test) { 15 bool is_test) {
Paweł Hajdan Jr. 2011/06/15 08:18:21 nit: Please align this line.
Sheridan Rawlins 2011/06/15 15:41:14 Done.
14 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame( 16 if (is_test) {
15 string16(), UTF8ToUTF16(js_test)); 17 NotificationRegistrar notification_registrar;
16 18 notification_registrar.Add(
17 if (is_test) 19 this, NotificationType::EXECUTE_JAVASCRIPT_RESULT,
20 Source<RenderViewHost>(web_ui_->GetRenderViewHost()));
21 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrameNotifyResult(
22 string16(), UTF8ToUTF16(js_test));
18 return WaitForResult(); 23 return WaitForResult();
19 else 24 } else {
25 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
26 string16(), UTF8ToUTF16(js_test));
20 return true; 27 return true;
28 }
21 } 29 }
22 30
23 void WebUITestHandler::HandlePass(const ListValue* args) { 31 void WebUITestHandler::Observe(NotificationType type,
24 test_succeeded_ = true; 32 const NotificationSource& source,
25 if (is_waiting_) 33 const NotificationDetails& details) {
26 MessageLoopForUI::current()->Quit(); 34 // Always quit the message loop so Waiting process can get result or error.
27 } 35 MessageLoopForUI::current()->Quit();
28 36
29 void WebUITestHandler::HandleFail(const ListValue* args) { 37 Value* value = Details<std::pair<int, Value*> >(details)->second;
30 test_succeeded_ = false; 38 ListValue* list_value;
31 if (is_waiting_) 39 ASSERT_TRUE(value->GetAsList(&list_value));
32 MessageLoopForUI::current()->Quit(); 40 ASSERT_TRUE(list_value->GetBoolean(0, &test_succeeded_));
33 41 if (!test_succeeded_) {
34 std::string message; 42 std::string message;
35 ASSERT_TRUE(args->GetString(0, &message)); 43 ASSERT_TRUE(list_value->GetString(1, &message));
36 LOG(ERROR) << message; 44 LOG(ERROR) << message;
37 } 45 }
38
39 void WebUITestHandler::RegisterMessages() {
40 web_ui_->RegisterMessageCallback("Pass",
41 NewCallback(this, &WebUITestHandler::HandlePass));
42 web_ui_->RegisterMessageCallback("Fail",
43 NewCallback(this, &WebUITestHandler::HandleFail));
44 } 46 }
45 47
46 bool WebUITestHandler::WaitForResult() { 48 bool WebUITestHandler::WaitForResult() {
47 is_waiting_ = true; 49 is_waiting_ = true;
Paweł Hajdan Jr. 2011/06/15 08:18:21 The new version of the code doesn't seem to use is
Sheridan Rawlins 2011/06/15 15:41:14 Nice catch - actually is_waiting_ is needed. I'll
48 ui_test_utils::RunMessageLoop(); 50 ui_test_utils::RunMessageLoop();
49 is_waiting_ = false; 51 is_waiting_ = false;
50 return test_succeeded_; 52 return test_succeeded_;
51 } 53 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698