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 "apmanager/manager.h"
18
19 #include <gtest/gtest.h>
20
21 #include "apmanager/fake_device_adaptor.h"
22 #include "apmanager/mock_control.h"
23 #include "apmanager/mock_device.h"
24
25 using ::testing::_;
26 using ::testing::Return;
27 using ::testing::ReturnNew;
28
29 namespace apmanager {
30
31 class ManagerTest : public testing::Test {
32 public:
ManagerTest()33 ManagerTest() : manager_(&control_interface_) {
34 ON_CALL(control_interface_, CreateDeviceAdaptorRaw())
35 .WillByDefault(ReturnNew<FakeDeviceAdaptor>());
36 }
37
RegisterDevice(scoped_refptr<Device> device)38 void RegisterDevice(scoped_refptr<Device> device) {
39 manager_.devices_.push_back(device);
40 }
41
42 protected:
43 MockControl control_interface_;
44 Manager manager_;
45 };
46
TEST_F(ManagerTest,GetAvailableDevice)47 TEST_F(ManagerTest, GetAvailableDevice) {
48 // Register a device without AP support (no preferred AP interface).
49 scoped_refptr<MockDevice> device0 = new MockDevice(&manager_);
50 RegisterDevice(device0);
51
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";
62 scoped_refptr<MockDevice> device1 = new MockDevice(&manager_);
63 device1->SetPreferredApInterface(kTestInterface1);
64 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.
70 device0->SetInUse(true);
71 EXPECT_EQ(device1, manager_.GetAvailableDevice());
72
73 // Both devices are in used, should return a nullptr.
74 device1->SetInUse(true);
75 EXPECT_EQ(nullptr, manager_.GetAvailableDevice());
76 }
77
TEST_F(ManagerTest,GetDeviceFromInterfaceName)78 TEST_F(ManagerTest, GetDeviceFromInterfaceName) {
79 // Register two devices
80 scoped_refptr<MockDevice> device0 = new MockDevice(&manager_);
81 scoped_refptr<MockDevice> device1 = new MockDevice(&manager_);
82 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"));
104 }
105
106 } // namespace apmanager
107