blob: 5d7e5a437accbe6f0fee65b30e917af5563b0c78 [file] [log] [blame]
Peter Qiu5dd242d2014-10-14 12:23:21 -07001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "apmanager/manager.h"
6
7#include <gtest/gtest.h>
8
Peter Qiufb39ba42014-11-21 09:09:59 -08009#include "apmanager/mock_device.h"
10
11using ::testing::_;
12using ::testing::Return;
13
Peter Qiu5dd242d2014-10-14 12:23:21 -070014namespace apmanager {
15
16class ManagerTest : public testing::Test {
17 public:
Peter Qiu376e4042014-11-13 09:40:28 -080018 ManagerTest() : manager_() {}
Peter Qiu5dd242d2014-10-14 12:23:21 -070019
Peter Qiufb39ba42014-11-21 09:09:59 -080020 void RegisterDevice(scoped_refptr<Device> device) {
21 manager_.devices_.push_back(device);
22 }
23
Peter Qiu5dd242d2014-10-14 12:23:21 -070024 protected:
25 Manager manager_;
26};
27
Peter Qiufb39ba42014-11-21 09:09:59 -080028TEST_F(ManagerTest, GetAvailableDevice) {
Peter Qiu8e785b92014-11-24 10:01:08 -080029 // Register a device without AP support (no preferred AP interface).
Peter Qiufb39ba42014-11-21 09:09:59 -080030 scoped_refptr<MockDevice> device0 = new MockDevice();
Peter Qiufb39ba42014-11-21 09:09:59 -080031 RegisterDevice(device0);
Peter Qiu8e785b92014-11-24 10:01:08 -080032
33 // No available device for AP operation.
34 EXPECT_EQ(nullptr, manager_.GetAvailableDevice());
35
36 // Add AP support to the device.
37 const char kTestInterface0[] = "test-interface0";
38 device0->SetPreferredApInterface(kTestInterface0);
39 EXPECT_EQ(device0, manager_.GetAvailableDevice());
40
41 // Register another device with AP support.
42 const char kTestInterface1[] = "test-interface1";
43 scoped_refptr<MockDevice> device1 = new MockDevice();
44 device1->SetPreferredApInterface(kTestInterface1);
Peter Qiufb39ba42014-11-21 09:09:59 -080045 RegisterDevice(device1);
46
47 // Both devices are idle by default, should return the first added device.
48 EXPECT_EQ(device0, manager_.GetAvailableDevice());
49
50 // Set first one to be in used, should return the non-used device.
51 device0->SetInUsed(true);
52 EXPECT_EQ(device1, manager_.GetAvailableDevice());
53
54 // Both devices are in used, should return a nullptr.
55 device1->SetInUsed(true);
56 EXPECT_EQ(nullptr, manager_.GetAvailableDevice());
Peter Qiu5dd242d2014-10-14 12:23:21 -070057}
58
Peter Qiufb39ba42014-11-21 09:09:59 -080059TEST_F(ManagerTest, GetDeviceFromInterfaceName) {
60 // Register two devices
61 scoped_refptr<MockDevice> device0 = new MockDevice();
62 scoped_refptr<MockDevice> device1 = new MockDevice();
63 RegisterDevice(device0);
64 RegisterDevice(device1);
65
66 const char kTestInterface0[] = "test-interface0";
67 const char kTestInterface1[] = "test-interface1";
68
69 // interface0 belongs to device0.
70 EXPECT_CALL(*device0.get(), InterfaceExists(kTestInterface0))
71 .WillOnce(Return(true));
72 EXPECT_EQ(device0, manager_.GetDeviceFromInterfaceName(kTestInterface0));
73
74 // interface1 belongs to device1.
75 EXPECT_CALL(*device0.get(), InterfaceExists(_))
76 .WillRepeatedly(Return(false));
77 EXPECT_CALL(*device1.get(), InterfaceExists(kTestInterface1))
78 .WillOnce(Return(true));
79 EXPECT_EQ(device1, manager_.GetDeviceFromInterfaceName(kTestInterface1));
80
81 // "random" interface is not found.
82 EXPECT_CALL(*device1.get(), InterfaceExists(_))
83 .WillRepeatedly(Return(false));
84 EXPECT_EQ(nullptr, manager_.GetDeviceFromInterfaceName("random"));
Peter Qiu5dd242d2014-10-14 12:23:21 -070085}
86
87} // namespace apmanager