1 /*
2  * Copyright (C) 2021 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 #define LOG_TAG "PerformanceHintNativeTest"
18 
19 #include <android/os/IHintManager.h>
20 #include <android/os/IHintSession.h>
21 #include <binder/IBinder.h>
22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
24 #include <performance_hint_private.h>
25 #include <memory>
26 #include <vector>
27 
28 using android::binder::Status;
29 using android::os::IHintManager;
30 using android::os::IHintSession;
31 
32 using namespace android;
33 using namespace testing;
34 
35 class MockIHintManager : public IHintManager {
36 public:
37     MOCK_METHOD(Status, createHintSession,
38                 (const ::android::sp<::android::IBinder>& token, const ::std::vector<int32_t>& tids,
39                  int64_t durationNanos, ::android::sp<::android::os::IHintSession>* _aidl_return),
40                 (override));
41     MOCK_METHOD(Status, getHintSessionPreferredRate, (int64_t * _aidl_return), (override));
42     MOCK_METHOD(IBinder*, onAsBinder, (), (override));
43 };
44 
45 class MockIHintSession : public IHintSession {
46 public:
47     MOCK_METHOD(Status, updateTargetWorkDuration, (int64_t targetDurationNanos), (override));
48     MOCK_METHOD(Status, reportActualWorkDuration,
49                 (const ::std::vector<int64_t>& actualDurationNanos,
50                  const ::std::vector<int64_t>& timeStampNanos),
51                 (override));
52     MOCK_METHOD(Status, close, (), (override));
53     MOCK_METHOD(IBinder*, onAsBinder, (), (override));
54 };
55 
56 class PerformanceHintTest : public Test {
57 public:
SetUp()58     void SetUp() override {
59         mMockIHintManager = new StrictMock<MockIHintManager>();
60         APerformanceHint_setIHintManagerForTesting(mMockIHintManager);
61     }
62 
TearDown()63     void TearDown() override {
64         mMockIHintManager = nullptr;
65         // Destroys MockIHintManager.
66         APerformanceHint_setIHintManagerForTesting(nullptr);
67     }
68 
createManager()69     APerformanceHintManager* createManager() {
70         EXPECT_CALL(*mMockIHintManager, getHintSessionPreferredRate(_))
71                 .Times(Exactly(1))
72                 .WillRepeatedly(DoAll(SetArgPointee<0>(123L), Return(Status())));
73         return APerformanceHint_getManager();
74     }
75 
76     StrictMock<MockIHintManager>* mMockIHintManager = nullptr;
77 };
78 
TEST_F(PerformanceHintTest,TestGetPreferredUpdateRateNanos)79 TEST_F(PerformanceHintTest, TestGetPreferredUpdateRateNanos) {
80     APerformanceHintManager* manager = createManager();
81     int64_t preferredUpdateRateNanos = APerformanceHint_getPreferredUpdateRateNanos(manager);
82     EXPECT_EQ(123L, preferredUpdateRateNanos);
83 }
84 
TEST_F(PerformanceHintTest,TestSession)85 TEST_F(PerformanceHintTest, TestSession) {
86     APerformanceHintManager* manager = createManager();
87 
88     std::vector<int32_t> tids;
89     tids.push_back(1);
90     tids.push_back(2);
91     int64_t targetDuration = 56789L;
92 
93     StrictMock<MockIHintSession>* iSession = new StrictMock<MockIHintSession>();
94     sp<IHintSession> session_sp(iSession);
95 
96     EXPECT_CALL(*mMockIHintManager, createHintSession(_, Eq(tids), Eq(targetDuration), _))
97             .Times(Exactly(1))
98             .WillRepeatedly(DoAll(SetArgPointee<3>(std::move(session_sp)), Return(Status())));
99 
100     APerformanceHintSession* session =
101             APerformanceHint_createSession(manager, tids.data(), tids.size(), targetDuration);
102     ASSERT_TRUE(session);
103 
104     int64_t targetDurationNanos = 10;
105     EXPECT_CALL(*iSession, updateTargetWorkDuration(Eq(targetDurationNanos))).Times(Exactly(1));
106     int result = APerformanceHint_updateTargetWorkDuration(session, targetDurationNanos);
107     EXPECT_EQ(0, result);
108 
109     usleep(2); // Sleep for longer than preferredUpdateRateNanos.
110     int64_t actualDurationNanos = 20;
111     std::vector<int64_t> actualDurations;
112     actualDurations.push_back(20);
113     EXPECT_CALL(*iSession, reportActualWorkDuration(Eq(actualDurations), _)).Times(Exactly(1));
114     result = APerformanceHint_reportActualWorkDuration(session, actualDurationNanos);
115     EXPECT_EQ(0, result);
116 
117     result = APerformanceHint_updateTargetWorkDuration(session, -1L);
118     EXPECT_EQ(EINVAL, result);
119     result = APerformanceHint_reportActualWorkDuration(session, -1L);
120     EXPECT_EQ(EINVAL, result);
121 
122     EXPECT_CALL(*iSession, close()).Times(Exactly(1));
123     APerformanceHint_closeSession(session);
124 }
125