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