1 /*
2  * Copyright 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 
17 #include <android/system/suspend/BnSuspendCallback.h>
18 #include <android/system/suspend/BnWakelockCallback.h>
19 #include <android/system/suspend/ISuspendControlService.h>
20 #include <binder/IServiceManager.h>
21 #include <gtest/gtest.h>
22 
23 using android::sp;
24 using android::status_t;
25 using android::String16;
26 using android::binder::Status;
27 using android::system::suspend::BnSuspendCallback;
28 using android::system::suspend::BnWakelockCallback;
29 using android::system::suspend::ISuspendControlService;
30 
31 static const std::string kTestWakelockName("test_lock");
32 static const std::string kTestWakelockName2("test_lock2");
33 
34 class WakelockCallback : public BnWakelockCallback {
35    public:
notifyAcquired()36     Status notifyAcquired() override { return Status::ok(); };
notifyReleased()37     Status notifyReleased() override { return Status::ok(); };
38 };
39 
40 class WakeupCallback : public BnSuspendCallback {
41    public:
notifyWakeup(bool success,const std::vector<std::string> & wakeupReasons)42     Status notifyWakeup([[maybe_unused]] bool success,
43                         [[maybe_unused]] const std::vector<std::string>& wakeupReasons) override {
44         return Status::ok();
45     }
46 };
47 
48 class SystemSuspendTest : public testing::Test {
49    public:
50     sp<ISuspendControlService> suspendControlService = nullptr;
51 
SetUp()52     virtual void SetUp() override {
53         status_t err = android::getService<ISuspendControlService>(String16("suspend_control"),
54                                                                    &suspendControlService);
55         ASSERT_EQ(err, android::OK);
56         ASSERT_NE(suspendControlService, nullptr);
57     }
58 };
59 
TEST_F(SystemSuspendTest,RegisterWakeupCallback)60 TEST_F(SystemSuspendTest, RegisterWakeupCallback) {
61     bool isRegistered = false;
62 
63     Status ret = suspendControlService->registerCallback(new WakeupCallback(), &isRegistered);
64     ASSERT_TRUE(ret.isOk());
65     EXPECT_EQ(true, isRegistered);
66 }
67 
TEST_F(SystemSuspendTest,RegisterMultipleWakeupCallback)68 TEST_F(SystemSuspendTest, RegisterMultipleWakeupCallback) {
69     bool isRegistered = false;
70 
71     Status ret = suspendControlService->registerCallback(new WakeupCallback(), &isRegistered);
72     ASSERT_TRUE(ret.isOk());
73     EXPECT_EQ(true, isRegistered);
74 
75     isRegistered = false;
76     ret = suspendControlService->registerCallback(new WakeupCallback(), &isRegistered);
77     ASSERT_TRUE(ret.isOk());
78     EXPECT_EQ(true, isRegistered);
79 }
80 
TEST_F(SystemSuspendTest,RegisterWakelockCallback)81 TEST_F(SystemSuspendTest, RegisterWakelockCallback) {
82     bool isRegistered = false;
83 
84     Status ret = suspendControlService->registerWakelockCallback(new WakelockCallback(),
85                                                                  kTestWakelockName, &isRegistered);
86     ASSERT_TRUE(ret.isOk());
87     EXPECT_EQ(true, isRegistered);
88 }
89 
TEST_F(SystemSuspendTest,RegisterSameWakelockCallbackWithSameName)90 TEST_F(SystemSuspendTest, RegisterSameWakelockCallbackWithSameName) {
91     bool isRegistered = false;
92     sp<WakelockCallback> callback = new WakelockCallback();
93 
94     Status ret =
95         suspendControlService->registerWakelockCallback(callback, kTestWakelockName, &isRegistered);
96     ASSERT_TRUE(ret.isOk());
97     EXPECT_EQ(true, isRegistered);
98 
99     isRegistered = false;
100     ret =
101         suspendControlService->registerWakelockCallback(callback, kTestWakelockName, &isRegistered);
102     ASSERT_TRUE(ret.isOk());
103     EXPECT_EQ(false, isRegistered);
104 }
105 
TEST_F(SystemSuspendTest,RegisterSameWakelockCallbackWithDifferentNames)106 TEST_F(SystemSuspendTest, RegisterSameWakelockCallbackWithDifferentNames) {
107     bool isRegistered = false;
108     sp<WakelockCallback> callback = new WakelockCallback();
109 
110     Status ret =
111         suspendControlService->registerWakelockCallback(callback, kTestWakelockName, &isRegistered);
112     ASSERT_TRUE(ret.isOk());
113     EXPECT_EQ(true, isRegistered);
114 
115     isRegistered = false;
116     ret = suspendControlService->registerWakelockCallback(callback, kTestWakelockName2,
117                                                           &isRegistered);
118     ASSERT_TRUE(ret.isOk());
119     EXPECT_EQ(true, isRegistered);
120 }
121 
TEST_F(SystemSuspendTest,RegisterDifferentWakelockCallbacksWithSameName)122 TEST_F(SystemSuspendTest, RegisterDifferentWakelockCallbacksWithSameName) {
123     bool isRegistered = false;
124     sp<WakelockCallback> callback1 = new WakelockCallback();
125     sp<WakelockCallback> callback2 = new WakelockCallback();
126 
127     Status ret = suspendControlService->registerWakelockCallback(callback1, kTestWakelockName,
128                                                                  &isRegistered);
129     ASSERT_TRUE(ret.isOk());
130     EXPECT_EQ(true, isRegistered);
131 
132     isRegistered = false;
133     ret = suspendControlService->registerWakelockCallback(callback2, kTestWakelockName,
134                                                           &isRegistered);
135     ASSERT_TRUE(ret.isOk());
136     EXPECT_EQ(true, isRegistered);
137 }
138