Files.app: Dispatch 'drive-connection-changed' event on initialization of VolumeManag...
[chromium-blink-merge.git] / extensions / test / result_catcher.cc
blob53c569ff0fc1445a78ed73ae5fd255620b0a3d7b
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) {
15 registrar_.Add(this,
16 extensions::NOTIFICATION_EXTENSION_TEST_PASSED,
17 content::NotificationService::AllSources());
18 registrar_.Add(this,
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
30 // empty.
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();
40 results_.pop_front();
41 message_ = messages_.front();
42 messages_.pop_front();
43 return ret;
46 NOTREACHED();
47 return false;
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_) {
56 return;
59 switch (type) {
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()) {
65 quit_closure_.Run();
67 break;
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()) {
74 quit_closure_.Run();
76 break;
78 default:
79 NOTREACHED();
83 } // namespace extensions