1 /*
2  * Copyright (C) 2024 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 <aidl/android/hardware/power/BnPower.h>
18 #include <fmq/AidlMessageQueue.h>
19 #include <fmq/EventFlag.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 
23 #include "aidl/ChannelManager.h"
24 #include "mocks/MockPowerHintSession.h"
25 #include "mocks/MockPowerSessionManager.h"
26 
27 namespace aidl::google::hardware::power::impl::pixel {
28 
29 using namespace std::chrono_literals;
30 using namespace testing;
31 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
32 using ::android::AidlMessageQueue;
33 using android::hardware::power::ChannelMessage;
34 
35 using SessionMessageQueue = AidlMessageQueue<ChannelMessage, SynchronizedReadWrite>;
36 using FlagMessageQueue = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
37 
38 class ChannelManagerTest : public Test {
39   public:
SetUp()40     virtual void SetUp() {
41         mChannelManager = ChannelManager<
42                 ChannelGroup<testing::NiceMock<mock::pixel::MockPowerSessionManager>,
43                              testing::NiceMock<mock::pixel::MockPowerHintSession>>>::getInstance();
44     }
45 
46   protected:
47     ChannelManager<ChannelGroup<testing::NiceMock<mock::pixel::MockPowerSessionManager>,
48                                 testing::NiceMock<mock::pixel::MockPowerHintSession>>>
49             *mChannelManager;
50 };
51 
TEST_F(ChannelManagerTest,testGetChannelConfig)52 TEST_F(ChannelManagerTest, testGetChannelConfig) {
53     int kUid = 3000;
54     int kTgid = 4000;
55     ChannelConfig config;
56     auto out = mChannelManager->getChannelConfig(kUid, kTgid, &config);
57     ASSERT_EQ(out, true);
58 }
59 
TEST_F(ChannelManagerTest,testCloseChannel)60 TEST_F(ChannelManagerTest, testCloseChannel) {
61     int kUid = 3000;
62     int kTgid = 4000;
63     ChannelConfig config;
64     mChannelManager->getChannelConfig(kUid, kTgid, &config);
65     bool success = mChannelManager->closeChannel(kUid, kTgid);
66     ASSERT_EQ(success, true);
67 }
68 
TEST_F(ChannelManagerTest,testManyChannelsSpawnMoreGroups)69 TEST_F(ChannelManagerTest, testManyChannelsSpawnMoreGroups) {
70     int kUid = 3000;
71     int kTgid = 4000;
72     int kChannelsToSpawn = 40;
73     ChannelConfig config;
74     // Spawn first one separately to make sure the group is created
75     mChannelManager->getChannelConfig(kUid, kTgid, &config);
76     ASSERT_EQ(mChannelManager->getChannelCount(), 1);
77     ASSERT_EQ(mChannelManager->getGroupCount(), 1);
78     for (int i = 1; i < kChannelsToSpawn; ++i) {
79         mChannelManager->getChannelConfig(kUid + i, kTgid + i, &config);
80     }
81     ASSERT_GT(mChannelManager->getGroupCount(), 1);
82     ASSERT_EQ(mChannelManager->getChannelCount(), kChannelsToSpawn);
83 }
84 
TEST_F(ChannelManagerTest,testNewChannelsReplaceOldChannels)85 TEST_F(ChannelManagerTest, testNewChannelsReplaceOldChannels) {
86     int kUid = 3000;
87     int kTgid = 4000;
88     int kChannelsToSpawn = 40;
89     ChannelConfig config;
90     // Spawn first one separately to make sure the group isn't destroyed later
91     mChannelManager->getChannelConfig(kUid, kTgid, &config);
92     for (int i = 1; i < kChannelsToSpawn; ++i) {
93         mChannelManager->getChannelConfig(kUid + i, kTgid + i, &config);
94         mChannelManager->closeChannel(kUid + i, kTgid + i);
95     }
96     ASSERT_EQ(mChannelManager->getGroupCount(), 1);
97     ASSERT_EQ(mChannelManager->getChannelCount(), 1);
98 }
99 
TEST_F(ChannelManagerTest,testGroupsCloseOnLastChannelDies)100 TEST_F(ChannelManagerTest, testGroupsCloseOnLastChannelDies) {
101     int kUid = 3000;
102     int kTgid = 4000;
103     int kChannelsToSpawn = 40;
104     ChannelConfig config;
105     for (int i = 0; i < kChannelsToSpawn; ++i) {
106         mChannelManager->getChannelConfig(kUid + i, kTgid + i, &config);
107     }
108     ASSERT_GT(mChannelManager->getGroupCount(), 0);
109     ASSERT_EQ(mChannelManager->getChannelCount(), 40);
110     for (int i = 0; i < kChannelsToSpawn; ++i) {
111         mChannelManager->closeChannel(kUid + i, kTgid + i);
112     }
113     ASSERT_EQ(mChannelManager->getGroupCount(), 0);
114     ASSERT_EQ(mChannelManager->getChannelCount(), 0);
115 }
116 
117 }  // namespace aidl::google::hardware::power::impl::pixel
118