blob: 91bbc201c32e3370d0c3138e60241fc91699be56 [file] [log] [blame]
Peter Qiu326b6cf2015-09-02 11:11:42 -07001//
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//
Peter Qiu5dd242d2014-10-14 12:23:21 -070016
17#include "apmanager/manager.h"
18
19#include <gtest/gtest.h>
20
Peter Qiuf9335402015-11-16 12:09:16 -080021#include "apmanager/fake_device_adaptor.h"
22#include "apmanager/mock_control.h"
Peter Qiufb39ba42014-11-21 09:09:59 -080023#include "apmanager/mock_device.h"
24
25using ::testing::_;
26using ::testing::Return;
Peter Qiuf9335402015-11-16 12:09:16 -080027using ::testing::ReturnNew;
Peter Qiufb39ba42014-11-21 09:09:59 -080028
Peter Qiu5dd242d2014-10-14 12:23:21 -070029namespace apmanager {
30
31class ManagerTest : public testing::Test {
32 public:
Peter Qiuf9335402015-11-16 12:09:16 -080033 ManagerTest() : manager_(&control_interface_) {
34 ON_CALL(control_interface_, CreateDeviceAdaptorRaw())
35 .WillByDefault(ReturnNew<FakeDeviceAdaptor>());
36 }
Peter Qiu5dd242d2014-10-14 12:23:21 -070037
Peter Qiufb39ba42014-11-21 09:09:59 -080038 void RegisterDevice(scoped_refptr<Device> device) {
39 manager_.devices_.push_back(device);
40 }
41
Peter Qiu5dd242d2014-10-14 12:23:21 -070042 protected:
Peter Qiuf9335402015-11-16 12:09:16 -080043 MockControl control_interface_;
Peter Qiu5dd242d2014-10-14 12:23:21 -070044 Manager manager_;
45};
46
Peter Qiufb39ba42014-11-21 09:09:59 -080047TEST_F(ManagerTest, GetAvailableDevice) {
Peter Qiu8e785b92014-11-24 10:01:08 -080048 // Register a device without AP support (no preferred AP interface).
Peter Qiuf9335402015-11-16 12:09:16 -080049 scoped_refptr<MockDevice> device0 = new MockDevice(&manager_);
Peter Qiufb39ba42014-11-21 09:09:59 -080050 RegisterDevice(device0);
Peter Qiu8e785b92014-11-24 10:01:08 -080051
52 // No available device for AP operation.
53 EXPECT_EQ(nullptr, manager_.GetAvailableDevice());
54
55 // Add AP support to the device.
56 const char kTestInterface0[] = "test-interface0";
57 device0->SetPreferredApInterface(kTestInterface0);
58 EXPECT_EQ(device0, manager_.GetAvailableDevice());
59
60 // Register another device with AP support.
61 const char kTestInterface1[] = "test-interface1";
Peter Qiuf9335402015-11-16 12:09:16 -080062 scoped_refptr<MockDevice> device1 = new MockDevice(&manager_);
Peter Qiu8e785b92014-11-24 10:01:08 -080063 device1->SetPreferredApInterface(kTestInterface1);
Peter Qiufb39ba42014-11-21 09:09:59 -080064 RegisterDevice(device1);
65
66 // Both devices are idle by default, should return the first added device.
67 EXPECT_EQ(device0, manager_.GetAvailableDevice());
68
69 // Set first one to be in used, should return the non-used device.
Garret Kelly0c0e9e72015-09-01 17:28:01 -040070 device0->SetInUse(true);
Peter Qiufb39ba42014-11-21 09:09:59 -080071 EXPECT_EQ(device1, manager_.GetAvailableDevice());
72
73 // Both devices are in used, should return a nullptr.
Garret Kelly0c0e9e72015-09-01 17:28:01 -040074 device1->SetInUse(true);
Peter Qiufb39ba42014-11-21 09:09:59 -080075 EXPECT_EQ(nullptr, manager_.GetAvailableDevice());
Peter Qiu5dd242d2014-10-14 12:23:21 -070076}
77
Peter Qiufb39ba42014-11-21 09:09:59 -080078TEST_F(ManagerTest, GetDeviceFromInterfaceName) {
79 // Register two devices
Peter Qiuf9335402015-11-16 12:09:16 -080080 scoped_refptr<MockDevice> device0 = new MockDevice(&manager_);
81 scoped_refptr<MockDevice> device1 = new MockDevice(&manager_);
Peter Qiufb39ba42014-11-21 09:09:59 -080082 RegisterDevice(device0);
83 RegisterDevice(device1);
84
85 const char kTestInterface0[] = "test-interface0";
86 const char kTestInterface1[] = "test-interface1";
87
88 // interface0 belongs to device0.
89 EXPECT_CALL(*device0.get(), InterfaceExists(kTestInterface0))
90 .WillOnce(Return(true));
91 EXPECT_EQ(device0, manager_.GetDeviceFromInterfaceName(kTestInterface0));
92
93 // interface1 belongs to device1.
94 EXPECT_CALL(*device0.get(), InterfaceExists(_))
95 .WillRepeatedly(Return(false));
96 EXPECT_CALL(*device1.get(), InterfaceExists(kTestInterface1))
97 .WillOnce(Return(true));
98 EXPECT_EQ(device1, manager_.GetDeviceFromInterfaceName(kTestInterface1));
99
100 // "random" interface is not found.
101 EXPECT_CALL(*device1.get(), InterfaceExists(_))
102 .WillRepeatedly(Return(false));
103 EXPECT_EQ(nullptr, manager_.GetDeviceFromInterfaceName("random"));
Peter Qiu5dd242d2014-10-14 12:23:21 -0700104}
105
106} // namespace apmanager