1 /*
2 * Copyright (C) 2023 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 <gmock/gmock.h>
18 #include <gtest/gtest.h>
19
20 #include "TestHelper.h"
21 #include "aidl/PowerHintSession.h"
22 #include "aidl/PowerSessionManager.h"
23 #include "mocks/MockHintManager.h"
24 #include "perfmgr/AdpfConfig.h"
25
26 using namespace testing;
27
28 namespace aidl::google::hardware::power::impl::pixel {
29
30 using TestingPowerSessionManager = PowerSessionManager<NiceMock<mock::pixel::MockHintManager>>;
31
32 class PowerSessionManagerTest : public Test {
33 public:
SetUp()34 void SetUp() {
35 mTestConfig = std::make_shared<::android::perfmgr::AdpfConfig>(makeMockConfig());
36 mMockHintManager = NiceMock<mock::pixel::MockHintManager>::GetInstance();
37 ON_CALL(*mMockHintManager, GetAdpfProfile()).WillByDefault(Return(mTestConfig));
38
39 mPowerSessionManager =
40 PowerSessionManager<NiceMock<mock::pixel::MockHintManager>>::getInstance();
41 }
42
TearDown()43 void TearDown() { Mock::VerifyAndClear(mMockHintManager); }
44
45 int mTgid = 10000;
46 int mUid = 1001;
47 std::vector<int> mTids = {10000};
48
49 // Make an actual hint session that is associated with this PowerSessionManager, but still has
50 // HintManager mocked
51 std::shared_ptr<
52 PowerHintSession<NiceMock<mock::pixel::MockHintManager>, TestingPowerSessionManager>>
makeHintSession()53 makeHintSession() {
54 std::vector<int> tids = {10000};
55 return ndk::SharedRefBase::make<PowerHintSession<NiceMock<mock::pixel::MockHintManager>,
56 TestingPowerSessionManager>>(
57 10000, 1001, tids, 1, SessionTag::OTHER);
58 }
59
60 protected:
61 std::shared_ptr<::android::perfmgr::AdpfConfig> mTestConfig;
62 NiceMock<mock::pixel::MockHintManager> *mMockHintManager;
63 PowerSessionManager<NiceMock<mock::pixel::MockHintManager>> *mPowerSessionManager;
64 };
65
TEST_F(PowerSessionManagerTest,ensureSessionTrackerWorks)66 TEST_F(PowerSessionManagerTest, ensureSessionTrackerWorks) {
67 auto session = makeHintSession();
68 SessionConfig config;
69 session->getSessionConfig(&config);
70
71 // Insert the session into the tracker
72 mPowerSessionManager->registerSession(session, config.id);
73
74 // Ensure they are the exact same session
75 auto trackedSession = std::static_pointer_cast<
76 PowerHintSession<NiceMock<mock::pixel::MockHintManager>, TestingPowerSessionManager>>(
77 mPowerSessionManager->getSession(config.id));
78 ASSERT_EQ(trackedSession.get(), session.get());
79
80 // Remove the session
81 mPowerSessionManager->unregisterSession(config.id);
82
83 // Ensure it is gone
84 trackedSession = std::static_pointer_cast<
85 PowerHintSession<NiceMock<mock::pixel::MockHintManager>, TestingPowerSessionManager>>(
86 mPowerSessionManager->getSession(config.id));
87 ASSERT_EQ(trackedSession.get(), nullptr);
88 }
89
TEST_F(PowerSessionManagerTest,ensureSessionDeregistersOnDeath)90 TEST_F(PowerSessionManagerTest, ensureSessionDeregistersOnDeath) {
91 SessionConfig config;
92 {
93 auto temporaryHintSession = makeHintSession();
94 temporaryHintSession->getSessionConfig(&config);
95
96 // Insert the session into the tracker
97 mPowerSessionManager->registerSession(temporaryHintSession, config.id);
98
99 // Ensure it is there
100 auto trackedSession =
101 std::static_pointer_cast<PowerHintSession<NiceMock<mock::pixel::MockHintManager>,
102 TestingPowerSessionManager>>(
103 mPowerSessionManager->getSession(config.id));
104 ASSERT_NE(trackedSession, nullptr);
105
106 // Kill the session
107 }
108
109 // Ensure it is gone
110 auto trackedSession = std::static_pointer_cast<
111 PowerHintSession<NiceMock<mock::pixel::MockHintManager>, TestingPowerSessionManager>>(
112 mPowerSessionManager->getSession(config.id));
113 ASSERT_EQ(trackedSession.get(), nullptr);
114 }
115
116 } // namespace aidl::google::hardware::power::impl::pixel
117