• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 "chrome/browser/extensions/extension_test_message_listener.h"
6 
7 #include "chrome/browser/extensions/extension_test_api.h"
8 #include "chrome/test/ui_test_utils.h"
9 #include "content/common/notification_service.h"
10 #include "content/common/notification_type.h"
11 
ExtensionTestMessageListener(const std::string & expected_message,bool will_reply)12 ExtensionTestMessageListener::ExtensionTestMessageListener(
13     const std::string& expected_message,
14     bool will_reply)
15     : expected_message_(expected_message),
16       satisfied_(false),
17       waiting_(false),
18       will_reply_(will_reply) {
19   registrar_.Add(this, NotificationType::EXTENSION_TEST_MESSAGE,
20                  NotificationService::AllSources());
21 }
22 
~ExtensionTestMessageListener()23 ExtensionTestMessageListener::~ExtensionTestMessageListener() {}
24 
WaitUntilSatisfied()25 bool ExtensionTestMessageListener::WaitUntilSatisfied()  {
26   if (satisfied_)
27     return true;
28   waiting_ = true;
29   ui_test_utils::RunMessageLoop();
30   return satisfied_;
31 }
32 
Reply(const std::string & message)33 void ExtensionTestMessageListener::Reply(const std::string& message) {
34   DCHECK(satisfied_);
35   DCHECK(will_reply_);
36   function_->Reply(message);
37   function_ = NULL;
38   will_reply_ = false;
39 }
40 
Observe(NotificationType type,const NotificationSource & source,const NotificationDetails & details)41 void ExtensionTestMessageListener::Observe(
42     NotificationType type,
43     const NotificationSource& source,
44     const NotificationDetails& details) {
45   const std::string& content = *Details<std::string>(details).ptr();
46   if (!satisfied_ && content == expected_message_) {
47     function_ = Source<ExtensionTestSendMessageFunction>(source).ptr();
48     satisfied_ = true;
49     registrar_.RemoveAll();  // Stop listening for more messages.
50     if (!will_reply_) {
51       function_->Reply("");
52       function_ = NULL;
53     }
54     if (waiting_) {
55       waiting_ = false;
56       MessageLoopForUI::current()->Quit();
57     }
58   }
59 }
60