root/chrome/browser/extensions/extension_test_message_listener_unittest.cc

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. TEST_F

// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/extensions/extension_api_unittest.h"
#include "chrome/browser/extensions/extension_test_message_listener.h"
#include "extensions/browser/api/test/test_api.h"

namespace extensions {

namespace {

const char kTestMessage[] = "test message";
const char kTestMessage2[] = "test message 2";
const char kFailureMessage[] = "failure";

}  // namespace

class ExtensionTestMessageListenerUnittest : public ExtensionApiUnittest {};

TEST_F(ExtensionTestMessageListenerUnittest, BasicTestExtensionMessageTest) {
  // A basic test of sending a message and ensuring the listener is satisfied.
  {
    ExtensionTestMessageListener listener(kTestMessage, false);  // won't reply
    EXPECT_FALSE(listener.was_satisfied());
    RunFunction(new TestSendMessageFunction,
                base::StringPrintf("[\"%s\"]", kTestMessage));
    EXPECT_TRUE(listener.was_satisfied());
    EXPECT_EQ(kTestMessage, listener.message());
  }

  // Test that we can receive an arbitrary message.
  {
    ExtensionTestMessageListener listener(false);  // won't reply
    EXPECT_FALSE(listener.was_satisfied());
    RunFunction(new TestSendMessageFunction,
                base::StringPrintf("[\"%s\"]", kTestMessage2));
    EXPECT_TRUE(listener.was_satisfied());
    EXPECT_EQ(kTestMessage2, listener.message());
  }

  // Test that we can set the listener to be reused, and send/receive multiple
  // messages.
  {
    ExtensionTestMessageListener listener(false);  // won't reply
    EXPECT_FALSE(listener.was_satisfied());
    RunFunction(new TestSendMessageFunction,
                base::StringPrintf("[\"%s\"]", kTestMessage));
    EXPECT_EQ(kTestMessage, listener.message());
    EXPECT_TRUE(listener.was_satisfied());
    listener.Reset();
    EXPECT_FALSE(listener.was_satisfied());
    EXPECT_EQ(base::EmptyString(), listener.message());
    RunFunction(new TestSendMessageFunction,
                base::StringPrintf("[\"%s\"]", kTestMessage2));
    EXPECT_TRUE(listener.was_satisfied());
    EXPECT_EQ(kTestMessage2, listener.message());
  }

  // Test that we can listen for two explicit messages: a success, and a
  // failure.
  {
    ExtensionTestMessageListener listener(kTestMessage, false);  // won't reply
    listener.set_failure_message(kFailureMessage);
    RunFunction(new TestSendMessageFunction,
                base::StringPrintf("[\"%s\"]", kTestMessage));
    EXPECT_TRUE(listener.WaitUntilSatisfied());  // succeeds
    EXPECT_EQ(kTestMessage, listener.message());
    listener.Reset();
    RunFunction(new TestSendMessageFunction,
                base::StringPrintf("[\"%s\"]", kFailureMessage));
    EXPECT_FALSE(listener.WaitUntilSatisfied());  // fails
    EXPECT_EQ(kFailureMessage, listener.message());
  }
}

}  // namespace extensions

/* [<][>][^][v][top][bottom][index][help] */