1 //
2 // Copyright (C) 2020 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 #include <gtest/gtest.h>
17 
18 #include "V2_0/ScopedWakelock.h"
19 
20 namespace android {
21 namespace hardware {
22 namespace sensors {
23 namespace V2_0 {
24 namespace implementation {
25 
26 class RefCounter : public IScopedWakelockRefCounter {
27   public:
28     size_t incCount = 0;
29     size_t decCount = 0;
30 
incrementRefCountAndMaybeAcquireWakelock(size_t,int64_t *)31     bool incrementRefCountAndMaybeAcquireWakelock(size_t /* delta */,
32                                                   int64_t* /* timeoutStart */) override {
33         incCount++;
34         return true;
35     }
36 
decrementRefCountAndMaybeReleaseWakelock(size_t,int64_t)37     void decrementRefCountAndMaybeReleaseWakelock(size_t /* delta */,
38                                                   int64_t /* timeoutStart */) override {
39         decCount++;
40     }
41 };
42 
43 class ScopedWakelockTest : public testing::Test {
44   public:
createScopedWakelock(bool locked)45     ScopedWakelock createScopedWakelock(bool locked) {
46         return ScopedWakelock(&mRefCounter, locked);
47     }
48 
49     RefCounter mRefCounter;
50 };
51 
TEST_F(ScopedWakelockTest,UnlockedAfterMoved)52 TEST_F(ScopedWakelockTest, UnlockedAfterMoved) {
53     ScopedWakelock wakelock = createScopedWakelock(false /* locked */);
54 
55     ScopedWakelock movedWakelock(std::move(wakelock));
56 
57     EXPECT_FALSE(wakelock.isLocked());
58     EXPECT_FALSE(movedWakelock.isLocked());
59 }
60 
TEST_F(ScopedWakelockTest,LockedAfterMoved)61 TEST_F(ScopedWakelockTest, LockedAfterMoved) {
62     ScopedWakelock wakelock = createScopedWakelock(true /* locked */);
63 
64     ScopedWakelock movedWakelock(std::move(wakelock));
65 
66     EXPECT_FALSE(wakelock.isLocked());
67     EXPECT_TRUE(movedWakelock.isLocked());
68 }
69 
TEST_F(ScopedWakelockTest,Locked)70 TEST_F(ScopedWakelockTest, Locked) {
71     ScopedWakelock wakelock = createScopedWakelock(true /* locked */);
72 
73     EXPECT_TRUE(wakelock.isLocked());
74 }
75 
TEST_F(ScopedWakelockTest,Unlocked)76 TEST_F(ScopedWakelockTest, Unlocked) {
77     ScopedWakelock wakelock = createScopedWakelock(false /* locked */);
78 
79     EXPECT_FALSE(wakelock.isLocked());
80 }
81 
TEST_F(ScopedWakelockTest,ScopedLocked)82 TEST_F(ScopedWakelockTest, ScopedLocked) {
83     { createScopedWakelock(true /* locked */); }
84 
85     EXPECT_EQ(mRefCounter.incCount, 1);
86     EXPECT_EQ(mRefCounter.decCount, 1);
87 }
88 
TEST_F(ScopedWakelockTest,ScopedUnlockIsNoop)89 TEST_F(ScopedWakelockTest, ScopedUnlockIsNoop) {
90     { createScopedWakelock(false /* locked */); }
91 
92     EXPECT_EQ(mRefCounter.incCount, 0);
93     EXPECT_EQ(mRefCounter.decCount, 0);
94 }
95 
TEST_F(ScopedWakelockTest,ScopedLockedMove)96 TEST_F(ScopedWakelockTest, ScopedLockedMove) {
97     {
98         ScopedWakelock wakelock = createScopedWakelock(true /* locked */);
99         ScopedWakelock movedWakelock(std::move(wakelock));
100     }
101 
102     EXPECT_EQ(mRefCounter.incCount, 1);
103     EXPECT_EQ(mRefCounter.decCount, 1);
104 }
105 
106 }  // namespace implementation
107 }  // namespace V2_0
108 }  // namespace sensors
109 }  // namespace hardware
110 }  // namespace android