OLD | NEW |
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/test/base/javascript_test_observer.h" | 5 #include "content/public/test/javascript_test_observer.h" |
6 | 6 |
7 #include "content/public/browser/dom_operation_notification_details.h" | 7 #include "content/public/browser/dom_operation_notification_details.h" |
8 #include "content/public/browser/notification_types.h" | 8 #include "content/public/browser/notification_types.h" |
9 #include "content/public/browser/render_view_host.h" | 9 #include "content/public/browser/render_view_host.h" |
10 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
11 #include "content/public/test/test_utils.h" | 11 #include "content/public/test/test_utils.h" |
12 | 12 |
| 13 namespace content { |
| 14 |
13 TestMessageHandler::TestMessageHandler() : ok_(true) { | 15 TestMessageHandler::TestMessageHandler() : ok_(true) { |
14 } | 16 } |
15 | 17 |
16 TestMessageHandler::~TestMessageHandler() { | 18 TestMessageHandler::~TestMessageHandler() { |
17 } | 19 } |
18 | 20 |
19 void TestMessageHandler::SetError(const std::string& message) { | 21 void TestMessageHandler::SetError(const std::string& message) { |
20 ok_ = false; | 22 ok_ = false; |
21 error_message_ = message; | 23 error_message_ = message; |
22 } | 24 } |
23 | 25 |
24 void TestMessageHandler::Reset() { | 26 void TestMessageHandler::Reset() { |
25 ok_ = true; | 27 ok_ = true; |
26 error_message_.clear(); | 28 error_message_.clear(); |
27 } | 29 } |
28 | 30 |
29 JavascriptTestObserver::JavascriptTestObserver( | 31 JavascriptTestObserver::JavascriptTestObserver( |
30 content::WebContents* web_contents, | 32 WebContents* web_contents, TestMessageHandler* handler) |
31 TestMessageHandler* handler) | |
32 : handler_(handler), | 33 : handler_(handler), |
33 running_(false), | 34 running_(false), |
34 finished_(false) { | 35 finished_(false) { |
35 Reset(); | 36 Reset(); |
36 registrar_.Add(this, | 37 registrar_.Add(this, |
37 content::NOTIFICATION_DOM_OPERATION_RESPONSE, | 38 NOTIFICATION_DOM_OPERATION_RESPONSE, |
38 content::Source<content::WebContents>(web_contents)); | 39 Source<WebContents>(web_contents)); |
39 } | 40 } |
40 | 41 |
41 JavascriptTestObserver::~JavascriptTestObserver() { | 42 JavascriptTestObserver::~JavascriptTestObserver() { |
42 } | 43 } |
43 | 44 |
44 bool JavascriptTestObserver::Run() { | 45 bool JavascriptTestObserver::Run() { |
45 // Messages may have arrived before Run was called. | 46 // Messages may have arrived before Run was called. |
46 if (!finished_) { | 47 if (!finished_) { |
47 CHECK(!running_); | 48 CHECK(!running_); |
48 running_ = true; | 49 running_ = true; |
49 content::RunMessageLoop(); | 50 RunMessageLoop(); |
50 running_ = false; | 51 running_ = false; |
51 } | 52 } |
52 return handler_->ok(); | 53 return handler_->ok(); |
53 } | 54 } |
54 | 55 |
55 void JavascriptTestObserver::Reset() { | 56 void JavascriptTestObserver::Reset() { |
56 CHECK(!running_); | 57 CHECK(!running_); |
57 running_ = false; | 58 running_ = false; |
58 finished_ = false; | 59 finished_ = false; |
59 handler_->Reset(); | 60 handler_->Reset(); |
60 } | 61 } |
61 | 62 |
62 void JavascriptTestObserver::Observe( | 63 void JavascriptTestObserver::Observe( |
63 int type, | 64 int type, |
64 const content::NotificationSource& source, | 65 const NotificationSource& source, |
65 const content::NotificationDetails& details) { | 66 const NotificationDetails& details) { |
66 CHECK(type == content::NOTIFICATION_DOM_OPERATION_RESPONSE); | 67 CHECK(type == NOTIFICATION_DOM_OPERATION_RESPONSE); |
67 content::Details<content::DomOperationNotificationDetails> dom_op_details( | 68 Details<DomOperationNotificationDetails> dom_op_details(details); |
68 details); | |
69 // We might receive responses for other script execution, but we only | 69 // We might receive responses for other script execution, but we only |
70 // care about the test finished message. | 70 // care about the test finished message. |
71 TestMessageHandler::MessageResponse response = | 71 TestMessageHandler::MessageResponse response = |
72 handler_->HandleMessage(dom_op_details->json); | 72 handler_->HandleMessage(dom_op_details->json); |
73 | 73 |
74 if (response == TestMessageHandler::DONE) { | 74 if (response == TestMessageHandler::DONE) { |
75 EndTest(); | 75 EndTest(); |
76 } else { | 76 } else { |
77 Continue(); | 77 Continue(); |
78 } | 78 } |
79 } | 79 } |
80 | 80 |
81 void JavascriptTestObserver::Continue() { | 81 void JavascriptTestObserver::Continue() { |
82 } | 82 } |
83 | 83 |
84 void JavascriptTestObserver::EndTest() { | 84 void JavascriptTestObserver::EndTest() { |
85 finished_ = true; | 85 finished_ = true; |
86 if (running_) { | 86 if (running_) { |
87 running_ = false; | 87 running_ = false; |
88 base::MessageLoopForUI::current()->Quit(); | 88 base::MessageLoopForUI::current()->Quit(); |
89 } | 89 } |
90 } | 90 } |
| 91 |
| 92 } // namespace content |
OLD | NEW |