1 // Copyright 2014 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 "extensions/test/result_catcher.h"
7 #include "content/public/browser/notification_service.h"
8 #include "content/public/test/test_utils.h"
9 #include "extensions/browser/notification_types.h"
11 namespace extensions
{
13 ResultCatcher::ResultCatcher()
14 : browser_context_restriction_(NULL
) {
16 extensions::NOTIFICATION_EXTENSION_TEST_PASSED
,
17 content::NotificationService::AllSources());
19 extensions::NOTIFICATION_EXTENSION_TEST_FAILED
,
20 content::NotificationService::AllSources());
23 ResultCatcher::~ResultCatcher() {
26 bool ResultCatcher::GetNextResult() {
27 // Depending on the tests, multiple results can come in from a single call
28 // to RunMessageLoop(), so we maintain a queue of results and just pull them
29 // off as the test calls this, going to the run loop only when the queue is
31 if (results_
.empty()) {
32 base::RunLoop run_loop
;
33 quit_closure_
= content::GetQuitTaskForRunLoop(&run_loop
);
34 content::RunThisRunLoop(&run_loop
);
35 quit_closure_
= base::Closure();
38 if (!results_
.empty()) {
39 bool ret
= results_
.front();
41 message_
= messages_
.front();
42 messages_
.pop_front();
50 void ResultCatcher::Observe(int type
,
51 const content::NotificationSource
& source
,
52 const content::NotificationDetails
& details
) {
53 if (browser_context_restriction_
&&
54 content::Source
<content::BrowserContext
>(source
).ptr() !=
55 browser_context_restriction_
) {
60 case extensions::NOTIFICATION_EXTENSION_TEST_PASSED
:
61 VLOG(1) << "Got EXTENSION_TEST_PASSED notification.";
62 results_
.push_back(true);
63 messages_
.push_back(std::string());
64 if (!quit_closure_
.is_null()) {
69 case extensions::NOTIFICATION_EXTENSION_TEST_FAILED
:
70 VLOG(1) << "Got EXTENSION_TEST_FAILED notification.";
71 results_
.push_back(false);
72 messages_
.push_back(*(content::Details
<std::string
>(details
).ptr()));
73 if (!quit_closure_
.is_null()) {
83 } // namespace extensions