Peter Qiu | 5dd242d | 2014-10-14 12:23:21 -0700 | [diff] [blame] | 1 | // 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 Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 9 | #include "apmanager/mock_device.h" |
| 10 | |
| 11 | using ::testing::_; |
| 12 | using ::testing::Return; |
| 13 | |
Peter Qiu | 5dd242d | 2014-10-14 12:23:21 -0700 | [diff] [blame] | 14 | namespace apmanager { |
| 15 | |
| 16 | class ManagerTest : public testing::Test { |
| 17 | public: |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 18 | ManagerTest() : manager_() {} |
Peter Qiu | 5dd242d | 2014-10-14 12:23:21 -0700 | [diff] [blame] | 19 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 20 | void RegisterDevice(scoped_refptr<Device> device) { |
| 21 | manager_.devices_.push_back(device); |
| 22 | } |
| 23 | |
Peter Qiu | 5dd242d | 2014-10-14 12:23:21 -0700 | [diff] [blame] | 24 | protected: |
| 25 | Manager manager_; |
| 26 | }; |
| 27 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 28 | TEST_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 Qiu | 5dd242d | 2014-10-14 12:23:21 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 47 | TEST_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 Qiu | 5dd242d | 2014-10-14 12:23:21 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | } // namespace apmanager |