summaryrefslogtreecommitdiff
path: root/manager_unittest.cc
blob: f959d98f37656b9dcd43e38c14178b44ba4da165 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "apmanager/manager.h"

#include <gtest/gtest.h>

#include "apmanager/fake_device_adaptor.h"
#include "apmanager/mock_control.h"
#include "apmanager/mock_device.h"

using ::testing::_;
using ::testing::Return;
using ::testing::ReturnNew;

namespace apmanager {

class ManagerTest : public testing::Test {
 public:
  ManagerTest() : manager_(&control_interface_) {
    ON_CALL(control_interface_, CreateDeviceAdaptorRaw())
        .WillByDefault(ReturnNew<FakeDeviceAdaptor>());
  }

  void RegisterDevice(const scoped_refptr<Device>& device) {
    manager_.devices_.push_back(device);
  }

 protected:
  MockControl control_interface_;
  Manager manager_;
};

TEST_F(ManagerTest, GetAvailableDevice) {
  // Register a device without AP support (no preferred AP interface).
  scoped_refptr<MockDevice> device0 = new MockDevice(&manager_);
  RegisterDevice(device0);

  // No available device for AP operation.
  EXPECT_EQ(nullptr, manager_.GetAvailableDevice());

  // Add AP support to the device.
  const char kTestInterface0[] = "test-interface0";
  device0->SetPreferredApInterface(kTestInterface0);
  EXPECT_EQ(device0, manager_.GetAvailableDevice());

  // Register another device with AP support.
  const char kTestInterface1[] = "test-interface1";
  scoped_refptr<MockDevice> device1 = new MockDevice(&manager_);
  device1->SetPreferredApInterface(kTestInterface1);
  RegisterDevice(device1);

  // Both devices are idle by default, should return the first added device.
  EXPECT_EQ(device0, manager_.GetAvailableDevice());

  // Set first one to be in used, should return the non-used device.
  device0->SetInUse(true);
  EXPECT_EQ(device1, manager_.GetAvailableDevice());

  // Both devices are in used, should return a nullptr.
  device1->SetInUse(true);
  EXPECT_EQ(nullptr, manager_.GetAvailableDevice());
}

TEST_F(ManagerTest, GetDeviceFromInterfaceName) {
  // Register two devices
  scoped_refptr<MockDevice> device0 = new MockDevice(&manager_);
  scoped_refptr<MockDevice> device1 = new MockDevice(&manager_);
  RegisterDevice(device0);
  RegisterDevice(device1);

  const char kTestInterface0[] = "test-interface0";
  const char kTestInterface1[] = "test-interface1";

  // interface0 belongs to device0.
  EXPECT_CALL(*device0.get(), InterfaceExists(kTestInterface0))
      .WillOnce(Return(true));
  EXPECT_EQ(device0, manager_.GetDeviceFromInterfaceName(kTestInterface0));

  // interface1 belongs to device1.
  EXPECT_CALL(*device0.get(), InterfaceExists(_))
      .WillRepeatedly(Return(false));
  EXPECT_CALL(*device1.get(), InterfaceExists(kTestInterface1))
      .WillOnce(Return(true));
  EXPECT_EQ(device1, manager_.GetDeviceFromInterfaceName(kTestInterface1));

  // "random" interface is not found.
  EXPECT_CALL(*device1.get(), InterfaceExists(_))
      .WillRepeatedly(Return(false));
  EXPECT_EQ(nullptr, manager_.GetDeviceFromInterfaceName("random"));
}

}  // namespace apmanager