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 11namespace extensions { 12 13ResultCatcher::ResultCatcher() 14 : browser_context_restriction_(NULL), waiting_(false) { 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()); 21} 22 23ResultCatcher::~ResultCatcher() { 24} 25 26bool 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 waiting_ = true; 33 content::RunMessageLoop(); 34 waiting_ = false; 35 } 36 37 if (!results_.empty()) { 38 bool ret = results_.front(); 39 results_.pop_front(); 40 message_ = messages_.front(); 41 messages_.pop_front(); 42 return ret; 43 } 44 45 NOTREACHED(); 46 return false; 47} 48 49void ResultCatcher::Observe(int type, 50 const content::NotificationSource& source, 51 const content::NotificationDetails& details) { 52 if (browser_context_restriction_ && 53 content::Source<content::BrowserContext>(source).ptr() != 54 browser_context_restriction_) { 55 return; 56 } 57 58 switch (type) { 59 case extensions::NOTIFICATION_EXTENSION_TEST_PASSED: 60 VLOG(1) << "Got EXTENSION_TEST_PASSED notification."; 61 results_.push_back(true); 62 messages_.push_back(std::string()); 63 if (waiting_) 64 base::MessageLoopForUI::current()->Quit(); 65 break; 66 67 case extensions::NOTIFICATION_EXTENSION_TEST_FAILED: 68 VLOG(1) << "Got EXTENSION_TEST_FAILED notification."; 69 results_.push_back(false); 70 messages_.push_back(*(content::Details<std::string>(details).ptr())); 71 if (waiting_) 72 base::MessageLoopForUI::current()->Quit(); 73 break; 74 75 default: 76 NOTREACHED(); 77 } 78} 79 80} // namespace extensions 81