extension_test_message_listener.cc revision bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293
1// Copyright (c) 2010 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/common/notification_service.h"
8#include "chrome/common/notification_type.h"
9#include "chrome/test/ui_test_utils.h"
10
11ExtensionTestMessageListener::ExtensionTestMessageListener(
12    const std::string& expected_message)
13    : expected_message_(expected_message), satisfied_(false),
14      waiting_(false) {
15  registrar_.Add(this, NotificationType::EXTENSION_TEST_MESSAGE,
16                 NotificationService::AllSources());
17}
18
19ExtensionTestMessageListener::~ExtensionTestMessageListener() {}
20
21bool ExtensionTestMessageListener::WaitUntilSatisfied()  {
22  if (satisfied_)
23    return true;
24  waiting_ = true;
25  ui_test_utils::RunMessageLoop();
26  return satisfied_;
27}
28
29void ExtensionTestMessageListener::Observe(
30    NotificationType type,
31    const NotificationSource& source,
32    const NotificationDetails& details) {
33  const std::string& content = *Details<std::string>(details).ptr();
34  if (!satisfied_ && content == expected_message_) {
35    satisfied_ = true;
36    registrar_.RemoveAll();  // Stop listening for more messages.
37    if (waiting_) {
38      waiting_ = false;
39      MessageLoopForUI::current()->Quit();
40    }
41  }
42}
43