1 // Copyright 2012 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Tests for ClientPairingSession. 16 17 #include <gtest/gtest.h> 18 #include <polo/pairing/clientpairingsession.h> 19 #include "polo/pairing/mocks.h" 20 #include "polo/wire/mocks.h" 21 22 using ::testing::InSequence; 23 using ::testing::Mock; 24 using ::testing::Return; 25 using ::testing::StrictMock; 26 using ::testing::_; 27 28 namespace polo { 29 namespace pairing { 30 31 class TestClientPairingSession : public ClientPairingSession { 32 public: 33 TestClientPairingSession(wire::PoloWireAdapter* wire, 34 PairingContext* context, 35 PoloChallengeResponse* challenge) 36 : ClientPairingSession(wire, context, challenge, "service1", "client1") { 37 } 38 39 void TestDoInitializationPhase() { 40 DoInitializationPhase(); 41 } 42 43 void TestDoConfigurationPhase() { 44 DoConfigurationPhase(); 45 } 46 47 bool TestSetConfiguration(const message::ConfigurationMessage& config) { 48 return SetConfiguration(config); 49 } 50 }; 51 52 MATCHER_P2(PairingRequestEq, service_name, client_name, "") { 53 return arg.service_name() == service_name 54 && arg.client_name() == client_name; 55 } 56 57 class ClientPairingSessionTest : public ::testing::Test { 58 protected: 59 ClientPairingSessionTest() 60 : interface_(), 61 wire_(&interface_), 62 challenge_(), 63 context_(NULL, NULL, false), 64 session_(&wire_, &context_, &challenge_) { 65 } 66 67 virtual void SetUp() { 68 } 69 70 virtual void TearDown() { 71 } 72 73 void InitSession() { 74 InSequence sequence; 75 76 EXPECT_CALL(listener_, OnSessionCreated()); 77 78 EXPECT_CALL(wire_, SendPairingRequestMessage( 79 PairingRequestEq("service1", "client1"))); 80 81 EXPECT_CALL(wire_, GetNextMessage()); 82 83 session_.DoPair(&listener_); 84 } 85 86 StrictMock<wire::MockWireInterface> interface_; 87 StrictMock<wire::MockWireAdapter> wire_; 88 StrictMock<MockChallengeResponse> challenge_; 89 PairingContext context_; 90 StrictMock<MockPairingListener> listener_; 91 StrictMock<TestClientPairingSession> session_; 92 }; 93 94 TEST_F(ClientPairingSessionTest, DoInitializationPhase) { 95 InitSession(); 96 } 97 98 TEST_F(ClientPairingSessionTest, DoConfigurationPhase) { 99 InitSession(); 100 InSequence sequence; 101 encoding::EncodingOption encoding(encoding::EncodingOption::kHexadecimal, 8); 102 message::ConfigurationMessage configuration(encoding, 103 message::OptionsMessage::kInputDevice); 104 session_.TestSetConfiguration(configuration); 105 106 EXPECT_CALL(wire_, SendConfigurationMessage(_)); 107 EXPECT_CALL(wire_, GetNextMessage()); 108 109 session_.TestDoConfigurationPhase(); 110 } 111 112 TEST_F(ClientPairingSessionTest, OnPairingRequestAckMessage) { 113 InitSession(); 114 InSequence sequence; 115 EXPECT_CALL(wire_, SendOptionsMessage(_)); 116 EXPECT_CALL(wire_, GetNextMessage()); 117 118 message::PairingRequestAckMessage message; 119 session_.OnPairingRequestAckMessage(message); 120 } 121 122 TEST_F(ClientPairingSessionTest, OnOptionsMessage) { 123 encoding::EncodingOption encoding(encoding::EncodingOption::kHexadecimal, 8); 124 session_.AddInputEncoding(encoding); 125 session_.AddOutputEncoding(encoding); 126 127 InitSession(); 128 129 InSequence sequence; 130 131 EXPECT_CALL(wire_, SendConfigurationMessage(_)); 132 EXPECT_CALL(wire_, GetNextMessage()); 133 134 message::OptionsMessage message; 135 message.AddInputEncoding(encoding); 136 message.AddOutputEncoding(encoding); 137 message.set_protocol_role_preference(message::OptionsMessage::kInputDevice); 138 session_.OnOptionsMessage(message); 139 } 140 141 TEST_F(ClientPairingSessionTest, OnConfigurationAckMessage) { 142 InitSession(); 143 InSequence sequence; 144 encoding::EncodingOption encoding(encoding::EncodingOption::kHexadecimal, 8); 145 message::ConfigurationMessage configuration(encoding, 146 message::OptionsMessage::kInputDevice); 147 session_.TestSetConfiguration(configuration); 148 149 EXPECT_CALL(listener_, OnPerformInputDeviceRole()); 150 151 message::ConfigurationAckMessage message; 152 session_.OnConfigurationAckMessage(message); 153 } 154 155 } // namespace pairing 156 } // namespace polo 157