Home | History | Annotate | Download | only in shill
      1 //
      2 // Copyright (C) 2014 The Android Open Source Project
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 //
     16 
     17 #include "shill/connection_tester.h"
     18 
     19 #include <memory>
     20 #include <string>
     21 
     22 #include <base/bind.h>
     23 #include <gmock/gmock.h>
     24 #include <gtest/gtest.h>
     25 
     26 #include "shill/connectivity_trial.h"
     27 #include "shill/mock_connection.h"
     28 #include "shill/mock_connectivity_trial.h"
     29 #include "shill/mock_control.h"
     30 #include "shill/mock_device_info.h"
     31 #include "shill/mock_event_dispatcher.h"
     32 
     33 using base::Bind;
     34 using base::Callback;
     35 using base::Unretained;
     36 using std::string;
     37 using std::unique_ptr;
     38 using std::vector;
     39 using testing::_;
     40 using testing::NiceMock;
     41 using testing::Return;
     42 using testing::StrictMock;
     43 using testing::Test;
     44 
     45 namespace shill {
     46 
     47 class ConnectionTesterTest : public Test {
     48  public:
     49   ConnectionTesterTest()
     50       : device_info_(
     51             new NiceMock<MockDeviceInfo>(&control_, nullptr, nullptr, nullptr)),
     52         connection_(new StrictMock<MockConnection>(device_info_.get())),
     53         connection_tester_(
     54             new ConnectionTester(connection_.get(), &dispatcher_,
     55                                  callback_target_.tester_callback())),
     56         connectivity_trial_(new StrictMock<MockConnectivityTrial>(
     57             connection_, ConnectionTester::kTrialTimeoutSeconds)) {}
     58 
     59   virtual void SetUp() {
     60     EXPECT_CALL(*connection_.get(), IsIPv6())
     61         .WillRepeatedly(Return(false));
     62     connection_tester_->connectivity_trial_
     63         .reset(connectivity_trial_);  // Passes ownership
     64     EXPECT_TRUE(connection_tester()->connectivity_trial_.get());
     65   }
     66 
     67   virtual void TearDown() {
     68     if (connection_tester()->connectivity_trial_.get()) {
     69       EXPECT_CALL(*connectivity_trial(), Stop());
     70 
     71       // Delete the connection tester while expectations still exist.
     72       connection_tester_.reset();
     73     }
     74   }
     75 
     76  protected:
     77   class CallbackTarget {
     78    public:
     79     CallbackTarget()
     80         : tester_callback_(Bind(&CallbackTarget::TesterCallback,
     81                                 Unretained(this))) {
     82     }
     83 
     84     MOCK_METHOD0(TesterCallback, void());
     85     Callback<void()>& tester_callback() {
     86       return tester_callback_;
     87     }
     88 
     89    private:
     90     Callback<void()> tester_callback_;
     91   };
     92 
     93   void StartConnectivityTest() {
     94     connection_tester_->Start();
     95   }
     96 
     97   ConnectionTester* connection_tester() { return connection_tester_.get(); }
     98   MockConnectivityTrial* connectivity_trial() { return connectivity_trial_; }
     99   MockEventDispatcher& dispatcher() { return dispatcher_; }
    100   CallbackTarget& callback_target() { return callback_target_; }
    101 
    102   void ExpectReset() {
    103     EXPECT_TRUE(callback_target_.tester_callback().
    104                 Equals(connection_tester_->tester_callback_));
    105   }
    106 
    107  private:
    108   StrictMock<MockEventDispatcher> dispatcher_;
    109   MockControl control_;
    110   unique_ptr<MockDeviceInfo> device_info_;
    111   scoped_refptr<MockConnection> connection_;
    112   CallbackTarget callback_target_;
    113   unique_ptr<ConnectionTester> connection_tester_;
    114   MockConnectivityTrial* connectivity_trial_;
    115 };
    116 
    117 TEST_F(ConnectionTesterTest, Constructor) {
    118   ExpectReset();
    119 }
    120 
    121 TEST_F(ConnectionTesterTest, StartTest) {
    122   EXPECT_CALL(*connectivity_trial(), Start(_, _)).Times(1);
    123   StartConnectivityTest();
    124 }
    125 
    126 TEST_F(ConnectionTesterTest, StartTestRepeated) {
    127   EXPECT_CALL(*connectivity_trial(), Start(_, _)).WillOnce(Return(true));
    128   StartConnectivityTest();
    129 
    130   EXPECT_CALL(*connectivity_trial(), Start(_, _)).WillOnce(Return(true));
    131   StartConnectivityTest();
    132 }
    133 
    134 TEST_F(ConnectionTesterTest, StopTest) {
    135   EXPECT_CALL(*connectivity_trial(), Stop()).Times(1);
    136   connection_tester()->Stop();
    137 }
    138 
    139 TEST_F(ConnectionTesterTest, CompleteTest) {
    140   ConnectivityTrial::Result result =
    141       ConnectivityTrial::Result(ConnectivityTrial::kPhaseContent,
    142                                 ConnectivityTrial::kStatusSuccess);
    143   EXPECT_CALL(*connectivity_trial(), Stop()).Times(1);
    144   EXPECT_CALL(callback_target(), TesterCallback()).Times(1);
    145   connection_tester()->CompleteTest(result);
    146 }
    147 
    148 }  // namespace shill
    149