blob: a3c6b3393944ff5d6a5cb5019051ecc986f26bd2 [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) {
29 // Register two devices.
30 scoped_refptr<MockDevice> device0 = new MockDevice();
31 scoped_refptr<MockDevice> device1 = new MockDevice();
32 RegisterDevice(device0);
33 RegisterDevice(device1);
34
35 // Both devices are idle by default, should return the first added device.
36 EXPECT_EQ(device0, manager_.GetAvailableDevice());
37
38 // Set first one to be in used, should return the non-used device.
39 device0->SetInUsed(true);
40 EXPECT_EQ(device1, manager_.GetAvailableDevice());
41
42 // Both devices are in used, should return a nullptr.
43 device1->SetInUsed(true);
44 EXPECT_EQ(nullptr, manager_.GetAvailableDevice());
Peter Qiu5dd242d2014-10-14 12:23:21 -070045}
46
Peter Qiufb39ba42014-11-21 09:09:59 -080047TEST_F(ManagerTest, GetDeviceFromInterfaceName) {
48 // Register two devices
49 scoped_refptr<MockDevice> device0 = new MockDevice();
50 scoped_refptr<MockDevice> device1 = new MockDevice();
51 RegisterDevice(device0);
52 RegisterDevice(device1);
53
54 const char kTestInterface0[] = "test-interface0";
55 const char kTestInterface1[] = "test-interface1";
56
57 // interface0 belongs to device0.
58 EXPECT_CALL(*device0.get(), InterfaceExists(kTestInterface0))
59 .WillOnce(Return(true));
60 EXPECT_EQ(device0, manager_.GetDeviceFromInterfaceName(kTestInterface0));
61
62 // interface1 belongs to device1.
63 EXPECT_CALL(*device0.get(), InterfaceExists(_))
64 .WillRepeatedly(Return(false));
65 EXPECT_CALL(*device1.get(), InterfaceExists(kTestInterface1))
66 .WillOnce(Return(true));
67 EXPECT_EQ(device1, manager_.GetDeviceFromInterfaceName(kTestInterface1));
68
69 // "random" interface is not found.
70 EXPECT_CALL(*device1.get(), InterfaceExists(_))
71 .WillRepeatedly(Return(false));
72 EXPECT_EQ(nullptr, manager_.GetDeviceFromInterfaceName("random"));
Peter Qiu5dd242d2014-10-14 12:23:21 -070073}
74
75} // namespace apmanager