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 "base/memory/ref_counted.h"
12 #include "content/public/browser/notification_observer.h"
13 #include "content/public/browser/notification_registrar.h"
15 namespace extensions {
16 class TestSendMessageFunction;
19 // This class helps us wait for incoming messages sent from javascript via
20 // chrome.test.sendMessage(). A sample usage would be:
22 // ExtensionTestMessageListener listener("foo", false); // won't reply
24 // ASSERT_TRUE(listener.WaitUntilSatisfied());
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:
30 // ExtensionTestMessageListener listener1("foo1", true); // will reply
31 // ExtensionTestMessageListener listener2("foo2", true); // will reply
32 // ASSERT_TRUE(listener1.WaitUntilSatisfied());
33 // ASSERT_TRUE(listener2.WaitUntilSatisfied());
35 // listener1.Reply("foo2 is ready");
36 // listener2.Reply("foo1 is ready");
38 // Further, we can use this to listen for a success and failure message:
40 // ExtensionTestMessageListener listener("success", will_reply);
41 // listener.set_failure_message("failure");
42 // ASSERT_TRUE(listener.WaitUntilSatisfied());
43 // if (listener.message() == "success") {
46 // ASSERT_EQ("failure", listener.message());
50 // Or, use it to listen to any arbitrary message:
52 // ExtensionTestMessageListener listener(will_reply);
53 // ASSERT_TRUE(listener.WaitUntilSatisfied());
54 // if (listener.message() == "foo")
56 // else if (listener.message() == "bar")
58 // else if (listener.message() == "baz")
63 // You can also use the class to listen for messages from a specified extension:
65 // ExtensionTestMessageListener listener(will_reply);
66 // listener.set_extension_id(extension->id());
67 // ASSERT_TRUE(listener.WaitUntilSatisfied());
70 // Finally, you can reset the listener to reuse it.
72 // ExtensionTestMessageListener listener(true); // will reply
73 // ASSERT_TRUE(listener.WaitUntilSatisfied());
74 // while (listener.message() != "end") {
75 // Handle(listener.message());
76 // listener.Reply("bar");
78 // ASSERT_TRUE(listener.WaitUntilSatisfied());
81 // Note that when using it in browser tests, you need to make sure it gets
82 // destructed *before* the browser gets torn down. Two common patterns are to
83 // either make it a local variable inside your test body, or if it's a member
84 // variable of a ExtensionBrowserTest subclass, override the
85 // BrowserTestBase::TearDownOnMainThread() method and clean it up there.
86 class ExtensionTestMessageListener : public content::NotificationObserver {
88 // We immediately start listening for |expected_message|.
89 ExtensionTestMessageListener(const std::string& expected_message,
91 // Construct a message listener which will listen for any message.
92 explicit ExtensionTestMessageListener(bool will_reply);
94 virtual ~ExtensionTestMessageListener();
96 // This returns true immediately if we've already gotten the expected
97 // message, or waits until it arrives.
98 // Returns false if the wait is interrupted and we still haven't gotten the
99 // message, or if the message was equal to |failure_message_|.
100 bool WaitUntilSatisfied();
102 // Send the given message as a reply. It is only valid to call this after
103 // WaitUntilSatisfied has returned true, and if will_reply is true.
104 void Reply(const std::string& message);
106 // Convenience method that formats int as a string and sends it.
107 void Reply(int message);
109 void ReplyWithError(const std::string& error);
111 // Reset the listener to listen again. No settings (such as messages to
112 // listen for) are modified.
115 // Getters and setters.
117 bool was_satisfied() const { return satisfied_; }
119 void set_failure_message(const std::string& failure_message) {
120 failure_message_ = failure_message;
123 const std::string& extension_id() const { return extension_id_; }
124 void set_extension_id(const std::string& extension_id) {
125 extension_id_ = extension_id;
128 const std::string& message() const { return message_; }
131 // Implements the content::NotificationObserver interface.
132 virtual void Observe(int type,
133 const content::NotificationSource& source,
134 const content::NotificationDetails& details) OVERRIDE;
136 content::NotificationRegistrar registrar_;
138 // The message we're expecting.
139 std::string expected_message_;
141 // The last message we received.
142 std::string message_;
144 // Whether we've seen expected_message_ yet.
147 // If we're waiting, then we want to post a quit task when the expected
151 // Whether or not we will wait for any message, regardless of contents.
152 bool wait_for_any_message_;
154 // If true, we expect the calling code to manually send a reply. Otherwise,
155 // we send an automatic empty reply to the extension.
158 // Whether or not we have already replied (we can only reply once).
161 // The extension id that we listen for, or empty.
162 std::string extension_id_;
164 // The message that signals failure.
165 std::string failure_message_;
167 // If we received a message that was the failure message.
170 // The function we need to reply to.
171 scoped_refptr<extensions::TestSendMessageFunction> function_;
174 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TEST_MESSAGE_LISTENER_H_