Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS 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 "shill/connection_tester.h" |
| 6 | |
Ben Chan | 22f1fbc | 2014-10-17 14:18:07 -0700 | [diff] [blame] | 7 | #include <memory> |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
| 10 | #include <base/bind.h> |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 11 | #include <gmock/gmock.h> |
| 12 | #include <gtest/gtest.h> |
| 13 | |
| 14 | #include "shill/connectivity_trial.h" |
| 15 | #include "shill/mock_connection.h" |
| 16 | #include "shill/mock_connectivity_trial.h" |
| 17 | #include "shill/mock_control.h" |
| 18 | #include "shill/mock_device_info.h" |
| 19 | #include "shill/mock_event_dispatcher.h" |
| 20 | |
| 21 | using base::Bind; |
| 22 | using base::Callback; |
| 23 | using base::Unretained; |
| 24 | using std::string; |
Ben Chan | 22f1fbc | 2014-10-17 14:18:07 -0700 | [diff] [blame] | 25 | using std::unique_ptr; |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 26 | using std::vector; |
| 27 | using testing::_; |
| 28 | using testing::NiceMock; |
| 29 | using testing::Return; |
| 30 | using testing::StrictMock; |
| 31 | using testing::Test; |
| 32 | |
| 33 | namespace shill { |
| 34 | |
| 35 | class ConnectionTesterTest : public Test { |
| 36 | public: |
| 37 | ConnectionTesterTest() |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 38 | : device_info_( |
| 39 | new NiceMock<MockDeviceInfo>(&control_, nullptr, nullptr, nullptr)), |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 40 | connection_(new StrictMock<MockConnection>(device_info_.get())), |
| 41 | connection_tester_( |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 42 | new ConnectionTester(connection_.get(), &dispatcher_, |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 43 | callback_target_.tester_callback())), |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 44 | connectivity_trial_(new StrictMock<MockConnectivityTrial>( |
| 45 | connection_, ConnectionTester::kTrialTimeoutSeconds)) {} |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 46 | |
| 47 | virtual void SetUp() { |
| 48 | EXPECT_CALL(*connection_.get(), IsIPv6()) |
| 49 | .WillRepeatedly(Return(false)); |
| 50 | connection_tester_->connectivity_trial_ |
| 51 | .reset(connectivity_trial_); // Passes ownership |
| 52 | EXPECT_TRUE(connection_tester()->connectivity_trial_.get()); |
| 53 | } |
| 54 | |
| 55 | virtual void TearDown() { |
| 56 | if (connection_tester()->connectivity_trial_.get()) { |
| 57 | EXPECT_CALL(*connectivity_trial(), Stop()); |
| 58 | |
| 59 | // Delete the connection tester while expectations still exist. |
| 60 | connection_tester_.reset(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | protected: |
| 65 | class CallbackTarget { |
| 66 | public: |
| 67 | CallbackTarget() |
| 68 | : tester_callback_(Bind(&CallbackTarget::TesterCallback, |
| 69 | Unretained(this))) { |
| 70 | } |
| 71 | |
| 72 | MOCK_METHOD0(TesterCallback, void()); |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 73 | Callback<void()>& tester_callback() { |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 74 | return tester_callback_; |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | Callback<void()> tester_callback_; |
| 79 | }; |
| 80 | |
| 81 | void StartConnectivityTest() { |
| 82 | connection_tester_->Start(); |
| 83 | } |
| 84 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 85 | ConnectionTester* connection_tester() { return connection_tester_.get(); } |
| 86 | MockConnectivityTrial* connectivity_trial() { return connectivity_trial_; } |
| 87 | MockEventDispatcher& dispatcher() { return dispatcher_; } |
| 88 | CallbackTarget& callback_target() { return callback_target_; } |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 89 | |
| 90 | void ExpectReset() { |
| 91 | EXPECT_TRUE(callback_target_.tester_callback(). |
| 92 | Equals(connection_tester_->tester_callback_)); |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | StrictMock<MockEventDispatcher> dispatcher_; |
| 97 | MockControl control_; |
Ben Chan | 22f1fbc | 2014-10-17 14:18:07 -0700 | [diff] [blame] | 98 | unique_ptr<MockDeviceInfo> device_info_; |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 99 | scoped_refptr<MockConnection> connection_; |
| 100 | CallbackTarget callback_target_; |
Ben Chan | 22f1fbc | 2014-10-17 14:18:07 -0700 | [diff] [blame] | 101 | unique_ptr<ConnectionTester> connection_tester_; |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 102 | MockConnectivityTrial* connectivity_trial_; |
| 103 | }; |
| 104 | |
| 105 | TEST_F(ConnectionTesterTest, Constructor) { |
| 106 | ExpectReset(); |
| 107 | } |
| 108 | |
| 109 | TEST_F(ConnectionTesterTest, StartTest) { |
| 110 | EXPECT_CALL(*connectivity_trial(), Start(_, _)).Times(1); |
| 111 | StartConnectivityTest(); |
| 112 | } |
| 113 | |
| 114 | TEST_F(ConnectionTesterTest, StartTestRepeated) { |
| 115 | EXPECT_CALL(*connectivity_trial(), Start(_, _)).WillOnce(Return(true)); |
| 116 | StartConnectivityTest(); |
| 117 | |
| 118 | EXPECT_CALL(*connectivity_trial(), Start(_, _)).WillOnce(Return(true)); |
| 119 | StartConnectivityTest(); |
| 120 | } |
| 121 | |
| 122 | TEST_F(ConnectionTesterTest, StopTest) { |
| 123 | EXPECT_CALL(*connectivity_trial(), Stop()).Times(1); |
| 124 | connection_tester()->Stop(); |
| 125 | } |
| 126 | |
| 127 | TEST_F(ConnectionTesterTest, CompleteTest) { |
| 128 | ConnectivityTrial::Result result = |
| 129 | ConnectivityTrial::Result(ConnectivityTrial::kPhaseContent, |
| 130 | ConnectivityTrial::kStatusSuccess); |
| 131 | EXPECT_CALL(*connectivity_trial(), Stop()).Times(1); |
| 132 | EXPECT_CALL(callback_target(), TesterCallback()).Times(1); |
| 133 | connection_tester()->CompleteTest(result); |
| 134 | } |
| 135 | |
| 136 | } // namespace shill |