blob: 4cf5f0a1e0812057684cf1d298764e353f694dba [file] [log] [blame]
Chong Zhanga9d45c72020-09-09 12:41:17 -07001/*
2 * Copyright 2015 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 <gtest/gtest.h>
18
19#include "ResourceManagerService.h"
20#include <aidl/android/media/BnResourceManagerClient.h>
21#include <media/MediaResource.h>
22#include <media/MediaResourcePolicy.h>
23#include <media/stagefright/foundation/ADebug.h>
24#include <media/stagefright/ProcessInfoInterface.h>
25
26namespace aidl {
27namespace android {
28namespace media {
29bool operator== (const MediaResourceParcel& lhs, const MediaResourceParcel& rhs) {
30 return lhs.type == rhs.type && lhs.subType == rhs.subType &&
31 lhs.id == rhs.id && lhs.value == rhs.value;
32}
33}}}
34
35namespace android {
36
37using Status = ::ndk::ScopedAStatus;
38using ::aidl::android::media::BnResourceManagerClient;
39using ::aidl::android::media::IResourceManagerService;
40using ::aidl::android::media::IResourceManagerClient;
41using ::aidl::android::media::MediaResourceParcel;
42
43static int64_t getId(const std::shared_ptr<IResourceManagerClient>& client) {
44 return (int64_t) client.get();
45}
46
47struct TestProcessInfo : public ProcessInfoInterface {
48 TestProcessInfo() {}
49 virtual ~TestProcessInfo() {}
50
51 virtual bool getPriority(int pid, int *priority) {
52 // For testing, use pid as priority.
53 // Lower the value higher the priority.
54 *priority = pid;
55 return true;
56 }
57
58 virtual bool isValidPid(int /* pid */) {
59 return true;
60 }
61
Chong Zhang97d367b2020-09-16 12:53:14 -070062 virtual bool overrideProcessInfo(
63 int /* pid */, int /* procState */, int /* oomScore */) {
64 return true;
65 }
66
67 virtual void removeProcessInfoOverride(int /* pid */) {
68 }
69
Chong Zhanga9d45c72020-09-09 12:41:17 -070070private:
71 DISALLOW_EVIL_CONSTRUCTORS(TestProcessInfo);
72};
73
74struct TestSystemCallback :
75 public ResourceManagerService::SystemCallbackInterface {
76 TestSystemCallback() :
77 mLastEvent({EventType::INVALID, 0}), mEventCount(0) {}
78
79 enum EventType {
80 INVALID = -1,
81 VIDEO_ON = 0,
82 VIDEO_OFF = 1,
83 VIDEO_RESET = 2,
84 CPUSET_ENABLE = 3,
85 CPUSET_DISABLE = 4,
86 };
87
88 struct EventEntry {
89 EventType type;
90 int arg;
91 };
92
93 virtual void noteStartVideo(int uid) override {
94 mLastEvent = {EventType::VIDEO_ON, uid};
95 mEventCount++;
96 }
97
98 virtual void noteStopVideo(int uid) override {
99 mLastEvent = {EventType::VIDEO_OFF, uid};
100 mEventCount++;
101 }
102
103 virtual void noteResetVideo() override {
104 mLastEvent = {EventType::VIDEO_RESET, 0};
105 mEventCount++;
106 }
107
108 virtual bool requestCpusetBoost(bool enable) override {
109 mLastEvent = {enable ? EventType::CPUSET_ENABLE : EventType::CPUSET_DISABLE, 0};
110 mEventCount++;
111 return true;
112 }
113
114 size_t eventCount() { return mEventCount; }
115 EventType lastEventType() { return mLastEvent.type; }
116 EventEntry lastEvent() { return mLastEvent; }
117
118protected:
119 virtual ~TestSystemCallback() {}
120
121private:
122 EventEntry mLastEvent;
123 size_t mEventCount;
124
125 DISALLOW_EVIL_CONSTRUCTORS(TestSystemCallback);
126};
127
128
129struct TestClient : public BnResourceManagerClient {
130 TestClient(int pid, const std::shared_ptr<ResourceManagerService> &service)
131 : mReclaimed(false), mPid(pid), mService(service) {}
132
133 Status reclaimResource(bool* _aidl_return) override {
134 mService->removeClient(mPid, getId(ref<TestClient>()));
135 mReclaimed = true;
136 *_aidl_return = true;
137 return Status::ok();
138 }
139
140 Status getName(::std::string* _aidl_return) override {
141 *_aidl_return = "test_client";
142 return Status::ok();
143 }
144
145 bool reclaimed() const {
146 return mReclaimed;
147 }
148
149 void reset() {
150 mReclaimed = false;
151 }
152
153 virtual ~TestClient() {}
154
155private:
156 bool mReclaimed;
157 int mPid;
158 std::shared_ptr<ResourceManagerService> mService;
159 DISALLOW_EVIL_CONSTRUCTORS(TestClient);
160};
161
162static const int kTestPid1 = 30;
163static const int kTestUid1 = 1010;
164
165static const int kTestPid2 = 20;
166static const int kTestUid2 = 1011;
167
168static const int kLowPriorityPid = 40;
169static const int kMidPriorityPid = 25;
170static const int kHighPriorityPid = 10;
171
172using EventType = TestSystemCallback::EventType;
173using EventEntry = TestSystemCallback::EventEntry;
174bool operator== (const EventEntry& lhs, const EventEntry& rhs) {
175 return lhs.type == rhs.type && lhs.arg == rhs.arg;
176}
177
178#define CHECK_STATUS_TRUE(condition) \
179 EXPECT_TRUE((condition).isOk() && (result))
180
181#define CHECK_STATUS_FALSE(condition) \
182 EXPECT_TRUE((condition).isOk() && !(result))
183
184class ResourceManagerServiceTestBase : public ::testing::Test {
185public:
186 ResourceManagerServiceTestBase()
187 : mSystemCB(new TestSystemCallback()),
188 mService(::ndk::SharedRefBase::make<ResourceManagerService>(
189 new TestProcessInfo, mSystemCB)),
190 mTestClient1(::ndk::SharedRefBase::make<TestClient>(kTestPid1, mService)),
191 mTestClient2(::ndk::SharedRefBase::make<TestClient>(kTestPid2, mService)),
192 mTestClient3(::ndk::SharedRefBase::make<TestClient>(kTestPid2, mService)) {
193 }
194
195 sp<TestSystemCallback> mSystemCB;
196 std::shared_ptr<ResourceManagerService> mService;
197 std::shared_ptr<IResourceManagerClient> mTestClient1;
198 std::shared_ptr<IResourceManagerClient> mTestClient2;
199 std::shared_ptr<IResourceManagerClient> mTestClient3;
200
201protected:
202 static bool isEqualResources(const std::vector<MediaResourceParcel> &resources1,
203 const ResourceList &resources2) {
204 // convert resource1 to ResourceList
205 ResourceList r1;
206 for (size_t i = 0; i < resources1.size(); ++i) {
207 const auto &res = resources1[i];
208 const auto resType = std::tuple(res.type, res.subType, res.id);
209 r1[resType] = res;
210 }
211 return r1 == resources2;
212 }
213
214 static void expectEqResourceInfo(const ResourceInfo &info,
215 int uid,
216 std::shared_ptr<IResourceManagerClient> client,
217 const std::vector<MediaResourceParcel> &resources) {
218 EXPECT_EQ(uid, info.uid);
219 EXPECT_EQ(client, info.client);
220 EXPECT_TRUE(isEqualResources(resources, info.resources));
221 }
222};
223
224} // namespace android