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