Home | History | Annotate | Download | only in extensions
      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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TEST_MESSAGE_LISTENER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_TEST_MESSAGE_LISTENER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/compiler_specific.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "content/public/browser/notification_observer.h"
     13 #include "content/public/browser/notification_registrar.h"
     14 
     15 namespace extensions {
     16 class TestSendMessageFunction;
     17 }
     18 
     19 // This class helps us wait for incoming messages sent from javascript via
     20 // chrome.test.sendMessage(). A sample usage would be:
     21 //
     22 //   ExtensionTestMessageListener listener("foo");
     23 //   ... do some work
     24 //   ASSERT_TRUE(listener.WaitUntilSatisfied());
     25 //
     26 // It is also possible to have the extension wait for our reply. This is
     27 // useful for coordinating multiple pages/processes and having them wait on
     28 // each other. Example:
     29 //
     30 //   ExtensionTestMessageListener listener1("foo1");
     31 //   ExtensionTestMessageListener listener2("foo2");
     32 //   ASSERT_TRUE(listener1.WaitUntilSatisfied());
     33 //   ASSERT_TRUE(listener2.WaitUntilSatisfied());
     34 //   ... do some work
     35 //   listener1.Reply("foo2 is ready");
     36 //   listener2.Reply("foo1 is ready");
     37 //
     38 // TODO(asargent) - In the future we may want to add the ability to listen for
     39 // multiple messages, and/or to wait for "any" message and then retrieve the
     40 // contents of that message. We may also want to specify an extension id as
     41 // satisfaction criteria in addition to message content.
     42 //
     43 // Note that when using it in browser tests, you need to make sure it gets
     44 // destructed *before* the browser gets torn down. Two common patterns are to
     45 // either make it a local variable inside your test body, or if it's a member
     46 // variable of a ExtensionBrowserTest subclass, override the
     47 // InProcessBrowserTest::CleanUpOnMainThread() method and clean it up there.
     48 class ExtensionTestMessageListener : public content::NotificationObserver {
     49  public:
     50   // We immediately start listening for |expected_message|.
     51   ExtensionTestMessageListener(const std::string& expected_message,
     52                                bool will_reply);
     53   virtual ~ExtensionTestMessageListener();
     54 
     55   void AlsoListenForFailureMessage(const std::string& failure_message) {
     56     failure_message_ = failure_message;
     57   }
     58 
     59   // This returns true immediately if we've already gotten the expected
     60   // message, or waits until it arrives. Returns false if the wait is
     61   // interrupted and we still haven't gotten the message.
     62   bool WaitUntilSatisfied();
     63 
     64   // Send the given message as a reply. It is only valid to call this after
     65   // WaitUntilSatisfied has returned true, and if will_reply is true.
     66   void Reply(const std::string& message);
     67 
     68   // Convenience method that formats int as a string and sends it.
     69   void Reply(int message);
     70 
     71   // Implements the content::NotificationObserver interface.
     72   virtual void Observe(int type,
     73                        const content::NotificationSource& source,
     74                        const content::NotificationDetails& details) OVERRIDE;
     75 
     76   bool was_satisfied() const { return satisfied_; }
     77 
     78  private:
     79   content::NotificationRegistrar registrar_;
     80 
     81   // The message we're expecting.
     82   std::string expected_message_;
     83 
     84   // Whether we've seen expected_message_ yet.
     85   bool satisfied_;
     86 
     87   // If we're waiting, then we want to post a quit task when the expected
     88   // message arrives.
     89   bool waiting_;
     90 
     91   // If true, we expect the calling code to manually send a reply. Otherwise,
     92   // we send an automatic empty reply to the extension.
     93   bool will_reply_;
     94 
     95   // The message that signals failure.
     96   std::string failure_message_;
     97 
     98   // If we received a message that was the failure message.
     99   bool failed_;
    100 
    101   // The function we need to reply to.
    102   scoped_refptr<extensions::TestSendMessageFunction> function_;
    103 };
    104 
    105 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_TEST_MESSAGE_LISTENER_H_
    106