Home | History | Annotate | Download | only in test
      1 // Copyright (c) 2012 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/extension_test_message_listener.h"
      6 
      7 #include "base/strings/string_number_conversions.h"
      8 #include "base/strings/string_util.h"
      9 #include "content/public/browser/notification_service.h"
     10 #include "content/public/browser/notification_source.h"
     11 #include "content/public/test/test_utils.h"
     12 #include "extensions/browser/api/test/test_api.h"
     13 #include "extensions/browser/notification_types.h"
     14 
     15 ExtensionTestMessageListener::ExtensionTestMessageListener(
     16     const std::string& expected_message,
     17     bool will_reply)
     18     : expected_message_(expected_message),
     19       satisfied_(false),
     20       waiting_(false),
     21       wait_for_any_message_(false),
     22       will_reply_(will_reply),
     23       replied_(false),
     24       failed_(false) {
     25   registrar_.Add(this,
     26                  extensions::NOTIFICATION_EXTENSION_TEST_MESSAGE,
     27                  content::NotificationService::AllSources());
     28 }
     29 
     30 ExtensionTestMessageListener::ExtensionTestMessageListener(bool will_reply)
     31     : satisfied_(false),
     32       waiting_(false),
     33       wait_for_any_message_(true),
     34       will_reply_(will_reply),
     35       replied_(false),
     36       failed_(false) {
     37   registrar_.Add(this,
     38                  extensions::NOTIFICATION_EXTENSION_TEST_MESSAGE,
     39                  content::NotificationService::AllSources());
     40 }
     41 
     42 ExtensionTestMessageListener::~ExtensionTestMessageListener() {}
     43 
     44 bool ExtensionTestMessageListener::WaitUntilSatisfied()  {
     45   if (satisfied_)
     46     return !failed_;
     47   waiting_ = true;
     48   content::RunMessageLoop();
     49   return !failed_;
     50 }
     51 
     52 void ExtensionTestMessageListener::Reply(const std::string& message) {
     53   CHECK(satisfied_);
     54   CHECK(!replied_);
     55 
     56   replied_ = true;
     57   function_->Reply(message);
     58   function_ = NULL;
     59 }
     60 
     61 void ExtensionTestMessageListener::Reply(int message) {
     62   Reply(base::IntToString(message));
     63 }
     64 
     65 void ExtensionTestMessageListener::ReplyWithError(const std::string& error) {
     66   CHECK(satisfied_);
     67   CHECK(!replied_);
     68 
     69   replied_ = true;
     70   function_->ReplyWithError(error);
     71   function_ = NULL;
     72 }
     73 
     74 void ExtensionTestMessageListener::Reset() {
     75   satisfied_ = false;
     76   failed_ = false;
     77   message_.clear();
     78   replied_ = false;
     79 }
     80 
     81 void ExtensionTestMessageListener::Observe(
     82     int type,
     83     const content::NotificationSource& source,
     84     const content::NotificationDetails& details) {
     85   DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_TEST_MESSAGE, type);
     86 
     87   // Return immediately if we're already satisfied or it's not the right
     88   // extension.
     89   extensions::TestSendMessageFunction* function =
     90       content::Source<extensions::TestSendMessageFunction>(source).ptr();
     91   if (satisfied_ ||
     92       (!extension_id_.empty() && function->extension_id() != extension_id_)) {
     93     return;
     94   }
     95 
     96   // We should have an empty message if we're not already satisfied.
     97   CHECK(message_.empty());
     98 
     99   const std::string& message = *content::Details<std::string>(details).ptr();
    100   if (message == expected_message_ || wait_for_any_message_ ||
    101       (!failure_message_.empty() && message == failure_message_)) {
    102     message_ = message;
    103     satisfied_ = true;
    104     failed_ = (message_ == failure_message_);
    105 
    106     // Reply immediately, or save the function for future use.
    107     function_ = function;
    108     if (!will_reply_)
    109       Reply(std::string());
    110 
    111     if (waiting_) {
    112       waiting_ = false;
    113       base::MessageLoopForUI::current()->Quit();
    114     }
    115   }
    116 }
    117