blob: 6d7a4046bfc2f95d727ee7ae37691fe582c8ac91 [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 Qiufb39ba42014-11-21 09:09:59 -080021#include "apmanager/mock_device.h"
22
23using ::testing::_;
24using ::testing::Return;
25
Peter Qiu5dd242d2014-10-14 12:23:21 -070026namespace apmanager {
27
28class ManagerTest : public testing::Test {
29 public:
Peter Qiu376e4042014-11-13 09:40:28 -080030 ManagerTest() : manager_() {}
Peter Qiu5dd242d2014-10-14 12:23:21 -070031
Peter Qiufb39ba42014-11-21 09:09:59 -080032 void RegisterDevice(scoped_refptr<Device> device) {
33 manager_.devices_.push_back(device);
34 }
35
Peter Qiu5dd242d2014-10-14 12:23:21 -070036 protected:
37 Manager manager_;
38};
39
Peter Qiufb39ba42014-11-21 09:09:59 -080040TEST_F(ManagerTest, GetAvailableDevice) {
Peter Qiu8e785b92014-11-24 10:01:08 -080041 // Register a device without AP support (no preferred AP interface).
Peter Qiufb39ba42014-11-21 09:09:59 -080042 scoped_refptr<MockDevice> device0 = new MockDevice();
Peter Qiufb39ba42014-11-21 09:09:59 -080043 RegisterDevice(device0);
Peter Qiu8e785b92014-11-24 10:01:08 -080044
45 // No available device for AP operation.
46 EXPECT_EQ(nullptr, manager_.GetAvailableDevice());
47
48 // Add AP support to the device.
49 const char kTestInterface0[] = "test-interface0";
50 device0->SetPreferredApInterface(kTestInterface0);
51 EXPECT_EQ(device0, manager_.GetAvailableDevice());
52
53 // Register another device with AP support.
54 const char kTestInterface1[] = "test-interface1";
55 scoped_refptr<MockDevice> device1 = new MockDevice();
56 device1->SetPreferredApInterface(kTestInterface1);
Peter Qiufb39ba42014-11-21 09:09:59 -080057 RegisterDevice(device1);
58
59 // Both devices are idle by default, should return the first added device.
60 EXPECT_EQ(device0, manager_.GetAvailableDevice());
61
62 // Set first one to be in used, should return the non-used device.
63 device0->SetInUsed(true);
64 EXPECT_EQ(device1, manager_.GetAvailableDevice());
65
66 // Both devices are in used, should return a nullptr.
67 device1->SetInUsed(true);
68 EXPECT_EQ(nullptr, manager_.GetAvailableDevice());
Peter Qiu5dd242d2014-10-14 12:23:21 -070069}
70
Peter Qiufb39ba42014-11-21 09:09:59 -080071TEST_F(ManagerTest, GetDeviceFromInterfaceName) {
72 // Register two devices
73 scoped_refptr<MockDevice> device0 = new MockDevice();
74 scoped_refptr<MockDevice> device1 = new MockDevice();
75 RegisterDevice(device0);
76 RegisterDevice(device1);
77
78 const char kTestInterface0[] = "test-interface0";
79 const char kTestInterface1[] = "test-interface1";
80
81 // interface0 belongs to device0.
82 EXPECT_CALL(*device0.get(), InterfaceExists(kTestInterface0))
83 .WillOnce(Return(true));
84 EXPECT_EQ(device0, manager_.GetDeviceFromInterfaceName(kTestInterface0));
85
86 // interface1 belongs to device1.
87 EXPECT_CALL(*device0.get(), InterfaceExists(_))
88 .WillRepeatedly(Return(false));
89 EXPECT_CALL(*device1.get(), InterfaceExists(kTestInterface1))
90 .WillOnce(Return(true));
91 EXPECT_EQ(device1, manager_.GetDeviceFromInterfaceName(kTestInterface1));
92
93 // "random" interface is not found.
94 EXPECT_CALL(*device1.get(), InterfaceExists(_))
95 .WillRepeatedly(Return(false));
96 EXPECT_EQ(nullptr, manager_.GetDeviceFromInterfaceName("random"));
Peter Qiu5dd242d2014-10-14 12:23:21 -070097}
98
99} // namespace apmanager