| // Copyright 2015 The Chromium OS 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 <string> |
| |
| #include "gmock/gmock.h" |
| #include "gtest/gtest.h" |
| |
| #include "base/bind.h" |
| #if BASE_VER < 860220 |
| #include "base/bind_helpers.h" |
| #endif |
| #include "base/callback.h" |
| #if BASE_VER >= 860220 |
| #include "base/callback_helpers.h" |
| #endif |
| |
| #include "thermald/key_value_publisher.h" |
| |
| using std::string; |
| |
| namespace thermald { |
| |
| static const char *kKey = "key"; |
| static const int kValue = 23; |
| |
| class MockSubscriber { |
| public: |
| MOCK_METHOD2(OnPublish, void(const string &, int)); |
| }; |
| |
| class KeyValuePublisherTest : public testing::Test { |
| protected: |
| KeyValuePublisher pub_; |
| }; |
| |
| TEST_F(KeyValuePublisherTest, PublishWithoutSubscribesDoesNotCrash) { |
| pub_.Publish(kKey, kValue); |
| } |
| |
| TEST_F(KeyValuePublisherTest, NotifiesSingleSubscriber) { |
| MockSubscriber sub; |
| |
| auto s = pub_.Subscribe(base::Bind(&MockSubscriber::OnPublish, |
| base::Unretained(&sub))); |
| |
| EXPECT_CALL(sub, OnPublish(kKey, kValue)); |
| |
| pub_.Publish(kKey, kValue); |
| } |
| |
| TEST_F(KeyValuePublisherTest, NotifiesMultipleSubscribers) { |
| MockSubscriber sub1; |
| MockSubscriber sub2; |
| |
| auto s1 = pub_.Subscribe(base::Bind(&MockSubscriber::OnPublish, |
| base::Unretained(&sub1))); |
| auto s2 = pub_.Subscribe(base::Bind(&MockSubscriber::OnPublish, |
| base::Unretained(&sub2))); |
| |
| EXPECT_CALL(sub1, OnPublish(kKey, kValue)); |
| EXPECT_CALL(sub2, OnPublish(kKey, kValue)); |
| |
| pub_.Publish(kKey, kValue); |
| } |
| |
| TEST_F(KeyValuePublisherTest, NoNotificationsAfterSubscriptionIsDeleted) { |
| MockSubscriber sub; |
| |
| { |
| auto s = pub_.Subscribe(base::Bind(&MockSubscriber::OnPublish, |
| base::Unretained(&sub))); |
| } |
| |
| EXPECT_CALL(sub, OnPublish(kKey, kValue)).Times(0); |
| |
| pub_.Publish(kKey, kValue); |
| } |
| |
| } // namespace thermald |