• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2014 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "shill/device_claimer.h"
18 
19 #include <string>
20 
21 #include <gtest/gtest.h>
22 
23 #include "shill/error.h"
24 #include "shill/mock_control.h"
25 #include "shill/mock_device_info.h"
26 
27 using std::string;
28 using ::testing::_;
29 using ::testing::Mock;
30 using ::testing::Return;
31 
32 namespace shill {
33 
34 const char kServiceName[] = "org.chromium.TestService";
35 const char kTestDevice1Name[] = "test_device1";
36 const char kTestDevice2Name[] = "test_device2";
37 
38 class DeviceClaimerTest : public testing::Test {
39  public:
DeviceClaimerTest()40   DeviceClaimerTest()
41      : device_info_(nullptr, nullptr, nullptr, nullptr),
42        device_claimer_(kServiceName, &device_info_, false) {}
43 
44  protected:
45   MockDeviceInfo device_info_;
46   DeviceClaimer device_claimer_;
47 };
48 
TEST_F(DeviceClaimerTest,ClaimAndReleaseDevices)49 TEST_F(DeviceClaimerTest, ClaimAndReleaseDevices) {
50   // Should not have any device claimed initially.
51   EXPECT_FALSE(device_claimer_.DevicesClaimed());
52 
53   // Claim device 1.
54   Error error;
55   EXPECT_CALL(device_info_, AddDeviceToBlackList(kTestDevice1Name)).Times(1);
56   EXPECT_TRUE(device_claimer_.Claim(kTestDevice1Name, &error));
57   EXPECT_EQ(Error::kSuccess, error.type());
58   EXPECT_TRUE(device_claimer_.DevicesClaimed());
59   Mock::VerifyAndClearExpectations(&device_info_);
60 
61   // Claim device 2.
62   error.Reset();
63   EXPECT_CALL(device_info_, AddDeviceToBlackList(kTestDevice2Name)).Times(1);
64   EXPECT_TRUE(device_claimer_.Claim(kTestDevice2Name, &error));
65   EXPECT_EQ(Error::kSuccess, error.type());
66   EXPECT_TRUE(device_claimer_.DevicesClaimed());
67   Mock::VerifyAndClearExpectations(&device_info_);
68 
69   // Claim device 1 again, should fail since it is already claimed.
70   const char kDuplicateDevice1Error[] =
71       "Device test_device1 had already been claimed";
72   error.Reset();
73   EXPECT_CALL(device_info_, AddDeviceToBlackList(_)).Times(0);
74   EXPECT_FALSE(device_claimer_.Claim(kTestDevice1Name, &error));
75   EXPECT_EQ(string(kDuplicateDevice1Error), error.message());
76   Mock::VerifyAndClearExpectations(&device_info_);
77 
78   // Release device 1.
79   error.Reset();
80   EXPECT_CALL(device_info_,
81               RemoveDeviceFromBlackList(kTestDevice1Name)).Times(1);
82   EXPECT_TRUE(device_claimer_.Release(kTestDevice1Name, &error));
83   EXPECT_EQ(Error::kSuccess, error.type());
84   // Should still have one device claimed.
85   EXPECT_TRUE(device_claimer_.DevicesClaimed());
86   Mock::VerifyAndClearExpectations(&device_info_);
87 
88   // Release device 1 again, should fail since device 1 is not currently
89   // claimed.
90   const char kDevice1NotClaimedError[] =
91       "Device test_device1 have not been claimed";
92   error.Reset();
93   EXPECT_CALL(device_info_, RemoveDeviceFromBlackList(_)).Times(0);
94   EXPECT_FALSE(device_claimer_.Release(kTestDevice1Name, &error));
95   EXPECT_EQ(string(kDevice1NotClaimedError), error.message());
96   // Should still have one device claimed.
97   EXPECT_TRUE(device_claimer_.DevicesClaimed());
98   Mock::VerifyAndClearExpectations(&device_info_);
99 
100   // Release device 2
101   error.Reset();
102   EXPECT_CALL(device_info_,
103               RemoveDeviceFromBlackList(kTestDevice2Name)).Times(1);
104   EXPECT_TRUE(device_claimer_.Release(kTestDevice2Name, &error));
105   EXPECT_EQ(Error::kSuccess, error.type());
106   Mock::VerifyAndClearExpectations(&device_info_);
107 
108   // Should not have any claimed devices.
109   EXPECT_FALSE(device_claimer_.DevicesClaimed());
110 }
111 
112 }  // namespace shill
113