OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "extensions/test/result_catcher.h" |
| 6 |
| 7 #include "content/public/browser/notification_service.h" |
| 8 #include "content/public/test/test_utils.h" |
| 9 #include "extensions/browser/notification_types.h" |
| 10 |
| 11 namespace extensions { |
| 12 |
| 13 ResultCatcher::ResultCatcher() : context_restriction_(NULL), waiting_(false) { |
| 14 registrar_.Add(this, |
| 15 extensions::NOTIFICATION_EXTENSION_TEST_PASSED, |
| 16 content::NotificationService::AllSources()); |
| 17 registrar_.Add(this, |
| 18 extensions::NOTIFICATION_EXTENSION_TEST_FAILED, |
| 19 content::NotificationService::AllSources()); |
| 20 } |
| 21 |
| 22 ResultCatcher::~ResultCatcher() { |
| 23 } |
| 24 |
| 25 bool ResultCatcher::GetNextResult() { |
| 26 // Depending on the tests, multiple results can come in from a single call |
| 27 // to RunMessageLoop(), so we maintain a queue of results and just pull them |
| 28 // off as the test calls this, going to the run loop only when the queue is |
| 29 // empty. |
| 30 if (results_.empty()) { |
| 31 waiting_ = true; |
| 32 content::RunMessageLoop(); |
| 33 waiting_ = false; |
| 34 } |
| 35 |
| 36 if (!results_.empty()) { |
| 37 bool ret = results_.front(); |
| 38 results_.pop_front(); |
| 39 message_ = messages_.front(); |
| 40 messages_.pop_front(); |
| 41 return ret; |
| 42 } |
| 43 |
| 44 NOTREACHED(); |
| 45 return false; |
| 46 } |
| 47 |
| 48 void ResultCatcher::Observe(int type, |
| 49 const content::NotificationSource& source, |
| 50 const content::NotificationDetails& details) { |
| 51 if (context_restriction_ && |
| 52 content::Source<content::BrowserContext>(source).ptr() != |
| 53 context_restriction_) { |
| 54 return; |
| 55 } |
| 56 |
| 57 switch (type) { |
| 58 case extensions::NOTIFICATION_EXTENSION_TEST_PASSED: |
| 59 VLOG(1) << "Got EXTENSION_TEST_PASSED notification."; |
| 60 results_.push_back(true); |
| 61 messages_.push_back(std::string()); |
| 62 if (waiting_) |
| 63 base::MessageLoopForUI::current()->Quit(); |
| 64 break; |
| 65 |
| 66 case extensions::NOTIFICATION_EXTENSION_TEST_FAILED: |
| 67 VLOG(1) << "Got EXTENSION_TEST_FAILED notification."; |
| 68 results_.push_back(false); |
| 69 messages_.push_back(*(content::Details<std::string>(details).ptr())); |
| 70 if (waiting_) |
| 71 base::MessageLoopForUI::current()->Quit(); |
| 72 break; |
| 73 |
| 74 default: |
| 75 NOTREACHED(); |
| 76 } |
| 77 } |
| 78 |
| 79 } // namespace extensions |
OLD | NEW |