OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/test/base/javascript_test_observer.h" | |
6 | |
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" | |
12 | |
13 TestMessageHandler::TestMessageHandler() : ok_(true) { | |
14 } | |
15 | |
16 TestMessageHandler::~TestMessageHandler() { | |
17 } | |
18 | |
19 void TestMessageHandler::SetError(const std::string& message) { | |
20 ok_ = false; | |
21 error_message_ = message; | |
22 } | |
23 | |
24 void TestMessageHandler::Reset() { | |
25 ok_ = true; | |
26 error_message_.clear(); | |
27 } | |
28 | |
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)); | |
39 } | |
40 | |
41 JavascriptTestObserver::~JavascriptTestObserver() { | |
42 } | |
43 | |
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; | |
51 } | |
52 return handler_->ok(); | |
53 } | |
54 | |
55 void JavascriptTestObserver::Reset() { | |
56 CHECK(!running_); | |
57 running_ = false; | |
58 finished_ = false; | |
59 handler_->Reset(); | |
60 } | |
61 | |
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); | |
73 | |
74 if (response == TestMessageHandler::DONE) { | |
75 EndTest(); | |
76 } else { | |
77 Continue(); | |
78 } | |
79 } | |
80 | |
81 void JavascriptTestObserver::Continue() { | |
82 } | |
83 | |
84 void JavascriptTestObserver::EndTest() { | |
85 finished_ = true; | |
86 if (running_) { | |
87 running_ = false; | |
88 base::MessageLoopForUI::current()->Quit(); | |
89 } | |
90 } | |
OLD | NEW |