Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / test / base / javascript_test_observer.cc
blobd010b93dcfae86216108c1135e0b2ea953e3ccfd
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
3 // found in the LICENSE file.
5 #include "chrome/test/base/javascript_test_observer.h"
7 #include "content/public/browser/dom_operation_notification_details.h"
8 #include "content/public/browser/notification_types.h"
9 #include "content/public/browser/render_view_host.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/test/test_utils.h"
13 TestMessageHandler::TestMessageHandler() : ok_(true) {
16 TestMessageHandler::~TestMessageHandler() {
19 void TestMessageHandler::SetError(const std::string& message) {
20 ok_ = false;
21 error_message_ = message;
24 void TestMessageHandler::Reset() {
25 ok_ = true;
26 error_message_.clear();
29 JavascriptTestObserver::JavascriptTestObserver(
30 content::WebContents* web_contents,
31 TestMessageHandler* handler)
32 : handler_(handler),
33 running_(false),
34 finished_(false) {
35 Reset();
36 registrar_.Add(this,
37 content::NOTIFICATION_DOM_OPERATION_RESPONSE,
38 content::Source<content::WebContents>(web_contents));
41 JavascriptTestObserver::~JavascriptTestObserver() {
44 bool JavascriptTestObserver::Run() {
45 // Messages may have arrived before Run was called.
46 if (!finished_) {
47 CHECK(!running_);
48 running_ = true;
49 content::RunMessageLoop();
50 running_ = false;
52 return handler_->ok();
55 void JavascriptTestObserver::Reset() {
56 CHECK(!running_);
57 running_ = false;
58 finished_ = false;
59 handler_->Reset();
62 void JavascriptTestObserver::Observe(
63 int type,
64 const content::NotificationSource& source,
65 const content::NotificationDetails& details) {
66 CHECK(type == content::NOTIFICATION_DOM_OPERATION_RESPONSE);
67 content::Details<content::DomOperationNotificationDetails> dom_op_details(
68 details);
69 // We might receive responses for other script execution, but we only
70 // care about the test finished message.
71 TestMessageHandler::MessageResponse response =
72 handler_->HandleMessage(dom_op_details->json);
74 if (response == TestMessageHandler::DONE) {
75 EndTest();
76 } else {
77 Continue();
81 void JavascriptTestObserver::Continue() {
84 void JavascriptTestObserver::EndTest() {
85 finished_ = true;
86 if (running_) {
87 running_ = false;
88 base::MessageLoopForUI::current()->Quit();