-
Notifications
You must be signed in to change notification settings - Fork 7
/
device_claimer_test.cc
99 lines (81 loc) · 3.31 KB
/
device_claimer_test.cc
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
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "shill/device_claimer.h"
#include <string>
#include <gtest/gtest.h>
#include "shill/error.h"
#include "shill/mock_control.h"
#include "shill/mock_device_info.h"
using std::string;
using ::testing::_;
using ::testing::Mock;
namespace shill {
const char kServiceName[] = "org.chromium.TestService";
const char kTestDevice1Name[] = "test_device1";
const char kTestDevice2Name[] = "test_device2";
class DeviceClaimerTest : public testing::Test {
public:
DeviceClaimerTest()
: device_info_(nullptr),
device_claimer_(kServiceName, &device_info_, false) {}
protected:
MockDeviceInfo device_info_;
DeviceClaimer device_claimer_;
};
TEST_F(DeviceClaimerTest, ClaimAndReleaseDevices) {
// Should not have any device claimed initially.
EXPECT_FALSE(device_claimer_.DevicesClaimed());
// Claim device 1.
Error error;
EXPECT_CALL(device_info_, AddDeviceToBlackList(kTestDevice1Name)).Times(1);
EXPECT_TRUE(device_claimer_.Claim(kTestDevice1Name, &error));
EXPECT_EQ(Error::kSuccess, error.type());
EXPECT_TRUE(device_claimer_.DevicesClaimed());
Mock::VerifyAndClearExpectations(&device_info_);
// Claim device 2.
error.Reset();
EXPECT_CALL(device_info_, AddDeviceToBlackList(kTestDevice2Name)).Times(1);
EXPECT_TRUE(device_claimer_.Claim(kTestDevice2Name, &error));
EXPECT_EQ(Error::kSuccess, error.type());
EXPECT_TRUE(device_claimer_.DevicesClaimed());
Mock::VerifyAndClearExpectations(&device_info_);
// Claim device 1 again, should fail since it is already claimed.
const char kDuplicateDevice1Error[] =
"Device test_device1 had already been claimed";
error.Reset();
EXPECT_CALL(device_info_, AddDeviceToBlackList(_)).Times(0);
EXPECT_FALSE(device_claimer_.Claim(kTestDevice1Name, &error));
EXPECT_EQ(string(kDuplicateDevice1Error), error.message());
Mock::VerifyAndClearExpectations(&device_info_);
// Release device 1.
error.Reset();
EXPECT_CALL(device_info_, RemoveDeviceFromBlackList(kTestDevice1Name))
.Times(1);
EXPECT_TRUE(device_claimer_.Release(kTestDevice1Name, &error));
EXPECT_EQ(Error::kSuccess, error.type());
// Should still have one device claimed.
EXPECT_TRUE(device_claimer_.DevicesClaimed());
Mock::VerifyAndClearExpectations(&device_info_);
// Release device 1 again, should fail since device 1 is not currently
// claimed.
const char kDevice1NotClaimedError[] =
"Device test_device1 have not been claimed";
error.Reset();
EXPECT_CALL(device_info_, RemoveDeviceFromBlackList(_)).Times(0);
EXPECT_FALSE(device_claimer_.Release(kTestDevice1Name, &error));
EXPECT_EQ(string(kDevice1NotClaimedError), error.message());
// Should still have one device claimed.
EXPECT_TRUE(device_claimer_.DevicesClaimed());
Mock::VerifyAndClearExpectations(&device_info_);
// Release device 2
error.Reset();
EXPECT_CALL(device_info_, RemoveDeviceFromBlackList(kTestDevice2Name))
.Times(1);
EXPECT_TRUE(device_claimer_.Release(kTestDevice2Name, &error));
EXPECT_EQ(Error::kSuccess, error.type());
Mock::VerifyAndClearExpectations(&device_info_);
// Should not have any claimed devices.
EXPECT_FALSE(device_claimer_.DevicesClaimed());
}
} // namespace shill