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

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: Allow the test to verify failing fast to pass. 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, bool is_test) {
13 bool is_test) { 15 if (is_test) {
14 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame( 16 NotificationRegistrar notification_registrar;
15 string16(), UTF8ToUTF16(js_test)); 17 notification_registrar.Add(
16 18 this, NotificationType::EXECUTE_JAVASCRIPT_RESULT,
17 if (is_test) 19 Source<RenderViewHost>(web_ui_->GetRenderViewHost()));
20 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrameNotifyResult(
21 string16(), UTF8ToUTF16(js_test));
18 return WaitForResult(); 22 return WaitForResult();
19 else 23 } else {
24 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
25 string16(), UTF8ToUTF16(js_test));
20 return true; 26 return true;
27 }
21 } 28 }
22 29
23 void WebUITestHandler::HandlePass(const ListValue* args) { 30 void WebUITestHandler::Observe(NotificationType type,
24 test_succeeded_ = true; 31 const NotificationSource& source,
25 if (is_waiting_) 32 const NotificationDetails& details) {
26 MessageLoopForUI::current()->Quit(); 33 // Quit the message loop if we were waiting so Waiting process can get result
27 } 34 // or error.
28
29 void WebUITestHandler::HandleFail(const ListValue* args) {
30 test_succeeded_ = false;
31 if (is_waiting_) 35 if (is_waiting_)
32 MessageLoopForUI::current()->Quit(); 36 MessageLoopForUI::current()->Quit();
33 37
34 std::string message; 38 // Default to failing, and bail early on errors. This allows the
35 ASSERT_TRUE(args->GetString(0, &message)); 39 // RunJavascriptTest to return failure without causing gtest to fail on this
36 LOG(ERROR) << message; 40 // test (so we can pass the test for the failing case).
37 } 41 test_succeeded_ = false;
38 42 Value* value = Details<std::pair<int, Value*> >(details)->second;
39 void WebUITestHandler::RegisterMessages() { 43 ListValue* list_value;
40 web_ui_->RegisterMessageCallback("Pass", 44 if (!value->GetAsList(&list_value))
41 NewCallback(this, &WebUITestHandler::HandlePass)); 45 return;
42 web_ui_->RegisterMessageCallback("Fail", 46 if (!list_value->GetBoolean(0, &test_succeeded_))
43 NewCallback(this, &WebUITestHandler::HandleFail)); 47 return;
48 if (!test_succeeded_) {
49 std::string message;
50 if (!list_value->GetString(1, &message))
51 return;
52 LOG(ERROR) << message;
53 }
44 } 54 }
45 55
46 bool WebUITestHandler::WaitForResult() { 56 bool WebUITestHandler::WaitForResult() {
47 is_waiting_ = true; 57 is_waiting_ = true;
48 ui_test_utils::RunMessageLoop(); 58 ui_test_utils::RunMessageLoop();
49 is_waiting_ = false; 59 is_waiting_ = false;
50 return test_succeeded_; 60 return test_succeeded_;
51 } 61 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698