blob: 87d0a30322e5319e2ff6984ceef4e51dd289cdf5 [file] [log] [blame]
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -07001// 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 Chan22f1fbc2014-10-17 14:18:07 -07007#include <memory>
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -07008#include <string>
9
10#include <base/bind.h>
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -070011#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
21using base::Bind;
22using base::Callback;
23using base::Unretained;
24using std::string;
Ben Chan22f1fbc2014-10-17 14:18:07 -070025using std::unique_ptr;
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -070026using std::vector;
27using testing::_;
28using testing::NiceMock;
29using testing::Return;
30using testing::StrictMock;
31using testing::Test;
32
33namespace shill {
34
35class ConnectionTesterTest : public Test {
36 public:
37 ConnectionTesterTest()
Ben Chancc225ef2014-09-30 13:26:51 -070038 : device_info_(
39 new NiceMock<MockDeviceInfo>(&control_, nullptr, nullptr, nullptr)),
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -070040 connection_(new StrictMock<MockConnection>(device_info_.get())),
41 connection_tester_(
Ben Chancc225ef2014-09-30 13:26:51 -070042 new ConnectionTester(connection_.get(), &dispatcher_,
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -070043 callback_target_.tester_callback())),
Ben Chancc225ef2014-09-30 13:26:51 -070044 connectivity_trial_(new StrictMock<MockConnectivityTrial>(
45 connection_, ConnectionTester::kTrialTimeoutSeconds)) {}
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -070046
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 Stewart3b30ca52015-06-16 13:13:10 -070073 Callback<void()>& tester_callback() {
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -070074 return tester_callback_;
75 }
76
77 private:
78 Callback<void()> tester_callback_;
79 };
80
81 void StartConnectivityTest() {
82 connection_tester_->Start();
83 }
84
Paul Stewart3b30ca52015-06-16 13:13:10 -070085 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 Silbersteinf4365a62014-09-16 11:40:32 -070089
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 Chan22f1fbc2014-10-17 14:18:07 -070098 unique_ptr<MockDeviceInfo> device_info_;
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -070099 scoped_refptr<MockConnection> connection_;
100 CallbackTarget callback_target_;
Ben Chan22f1fbc2014-10-17 14:18:07 -0700101 unique_ptr<ConnectionTester> connection_tester_;
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -0700102 MockConnectivityTrial* connectivity_trial_;
103};
104
105TEST_F(ConnectionTesterTest, Constructor) {
106 ExpectReset();
107}
108
109TEST_F(ConnectionTesterTest, StartTest) {
110 EXPECT_CALL(*connectivity_trial(), Start(_, _)).Times(1);
111 StartConnectivityTest();
112}
113
114TEST_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
122TEST_F(ConnectionTesterTest, StopTest) {
123 EXPECT_CALL(*connectivity_trial(), Stop()).Times(1);
124 connection_tester()->Stop();
125}
126
127TEST_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