Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / web_ui_test_handler.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/webui/web_ui_test_handler.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "chrome/common/render_messages.h"
12 #include "content/public/browser/notification_details.h"
13 #include "content/public/browser/notification_registrar.h"
14 #include "content/public/browser/notification_types.h"
15 #include "content/public/browser/render_frame_host.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_ui.h"
19 #include "content/public/test/test_utils.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 using content::RenderViewHost;
23
24 WebUITestHandler::WebUITestHandler()
25     : test_done_(false),
26       test_succeeded_(false),
27       run_test_done_(false),
28       run_test_succeeded_(false),
29       is_waiting_(false) {
30 }
31
32 void WebUITestHandler::PreloadJavaScript(const base::string16& js_text,
33                                          RenderViewHost* preload_host) {
34   DCHECK(preload_host);
35   preload_host->Send(new ChromeViewMsg_WebUIJavaScript(
36       preload_host->GetRoutingID(), js_text));
37 }
38
39 void WebUITestHandler::RunJavaScript(const base::string16& js_text) {
40   web_ui()->GetWebContents()->GetMainFrame()->ExecuteJavaScript(js_text);
41 }
42
43 bool WebUITestHandler::RunJavaScriptTestWithResult(
44     const base::string16& js_text) {
45   test_succeeded_ = false;
46   run_test_succeeded_ = false;
47   content::RenderFrameHost* frame = web_ui()->GetWebContents()->GetMainFrame();
48   frame->ExecuteJavaScript(js_text,
49                            base::Bind(&WebUITestHandler::JavaScriptComplete,
50                                       base::Unretained(this)));
51   return WaitForResult();
52 }
53
54 void WebUITestHandler::RegisterMessages() {
55   web_ui()->RegisterMessageCallback("testResult",
56       base::Bind(&WebUITestHandler::HandleTestResult, base::Unretained(this)));
57 }
58
59 void WebUITestHandler::HandleTestResult(const base::ListValue* test_result) {
60   // Quit the message loop if |is_waiting_| so waiting process can get result or
61   // error. To ensure this gets done, do this before ASSERT* calls.
62   if (is_waiting_)
63     base::MessageLoopForUI::current()->Quit();
64
65   SCOPED_TRACE("WebUITestHandler::HandleTestResult");
66
67   EXPECT_FALSE(test_done_);
68   test_done_ = true;
69   test_succeeded_ = false;
70
71   ASSERT_TRUE(test_result->GetBoolean(0, &test_succeeded_));
72   if (!test_succeeded_) {
73     std::string message;
74     ASSERT_TRUE(test_result->GetString(1, &message));
75     LOG(ERROR) << message;
76   }
77 }
78
79 void WebUITestHandler::JavaScriptComplete(const base::Value* result) {
80   // Quit the message loop if |is_waiting_| so waiting process can get result or
81   // error. To ensure this gets done, do this before ASSERT* calls.
82   if (is_waiting_)
83     base::MessageLoopForUI::current()->Quit();
84
85   SCOPED_TRACE("WebUITestHandler::JavaScriptComplete");
86
87   EXPECT_FALSE(run_test_done_);
88   run_test_done_ = true;
89   run_test_succeeded_ = false;
90
91   ASSERT_TRUE(result->GetAsBoolean(&run_test_succeeded_));
92 }
93
94 bool WebUITestHandler::WaitForResult() {
95   SCOPED_TRACE("WebUITestHandler::WaitForResult");
96   test_done_ = false;
97   run_test_done_ = false;
98   is_waiting_ = true;
99
100   // Either sync test completion or the testDone() will cause message loop
101   // to quit.
102   content::RunMessageLoop();
103
104   // Run a second message loop when not |run_test_done_| so that the sync test
105   // completes, or |run_test_succeeded_| but not |test_done_| so async tests
106   // complete.
107   if (!run_test_done_ || (run_test_succeeded_ && !test_done_)) {
108     content::RunMessageLoop();
109   }
110
111   is_waiting_ = false;
112
113   // To succeed the test must execute as well as pass the test.
114   return run_test_succeeded_ && test_succeeded_;
115 }