blob: cffedc6cdccfb8d96e82a2580eb5e396dec727de [file] [log] [blame]
Ronghua Wu231c3d12015-03-11 15:10:32 -07001/*
Ronghua Wu67e7f542015-03-13 10:47:08 -07002 * Copyright 2015 The Android Open Source Project
Ronghua Wu231c3d12015-03-11 15:10:32 -07003 *
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_NDEBUG 0
18#define LOG_TAG "ResourceManagerService_test"
19#include <utils/Log.h>
20
21#include <gtest/gtest.h>
22
23#include "ResourceManagerService.h"
24#include <media/IResourceManagerService.h>
25#include <media/MediaResource.h>
26#include <media/MediaResourcePolicy.h>
27#include <media/stagefright/foundation/ADebug.h>
28#include <media/stagefright/ProcessInfoInterface.h>
29
30namespace android {
31
Ronghua Wu37c89242015-07-15 12:23:48 -070032static int64_t getId(sp<IResourceManagerClient> client) {
33 return (int64_t) client.get();
34}
35
Ronghua Wu231c3d12015-03-11 15:10:32 -070036struct TestProcessInfo : public ProcessInfoInterface {
37 TestProcessInfo() {}
38 virtual ~TestProcessInfo() {}
39
40 virtual bool getPriority(int pid, int *priority) {
41 // For testing, use pid as priority.
42 // Lower the value higher the priority.
43 *priority = pid;
44 return true;
45 }
46
Ronghua Wud11c43a2016-01-27 16:26:12 -080047 virtual bool isValidPid(int /* pid */) {
48 return true;
49 }
50
Ronghua Wu231c3d12015-03-11 15:10:32 -070051private:
52 DISALLOW_EVIL_CONSTRUCTORS(TestProcessInfo);
53};
54
55struct TestClient : public BnResourceManagerClient {
Ronghua Wu37c89242015-07-15 12:23:48 -070056 TestClient(int pid, sp<ResourceManagerService> service)
57 : mReclaimed(false), mPid(pid), mService(service) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -070058
59 virtual bool reclaimResource() {
60 sp<IResourceManagerClient> client(this);
Ronghua Wu37c89242015-07-15 12:23:48 -070061 mService->removeResource(mPid, (int64_t) client.get());
Ronghua Wu231c3d12015-03-11 15:10:32 -070062 mReclaimed = true;
63 return true;
64 }
65
Ronghua Wu8f9dd872015-04-23 15:24:25 -070066 virtual String8 getName() {
67 return String8("test_client");
68 }
69
Ronghua Wu231c3d12015-03-11 15:10:32 -070070 bool reclaimed() const {
71 return mReclaimed;
72 }
73
74 void reset() {
75 mReclaimed = false;
76 }
77
78protected:
79 virtual ~TestClient() {}
80
81private:
82 bool mReclaimed;
Ronghua Wu37c89242015-07-15 12:23:48 -070083 int mPid;
Ronghua Wu231c3d12015-03-11 15:10:32 -070084 sp<ResourceManagerService> mService;
85 DISALLOW_EVIL_CONSTRUCTORS(TestClient);
86};
87
88static const int kTestPid1 = 30;
89static const int kTestPid2 = 20;
90
Ronghua Wu05d89f12015-07-07 16:47:42 -070091static const int kLowPriorityPid = 40;
92static const int kMidPriorityPid = 25;
93static const int kHighPriorityPid = 10;
94
Ronghua Wu231c3d12015-03-11 15:10:32 -070095class ResourceManagerServiceTest : public ::testing::Test {
96public:
97 ResourceManagerServiceTest()
98 : mService(new ResourceManagerService(new TestProcessInfo)),
Ronghua Wu37c89242015-07-15 12:23:48 -070099 mTestClient1(new TestClient(kTestPid1, mService)),
100 mTestClient2(new TestClient(kTestPid2, mService)),
101 mTestClient3(new TestClient(kTestPid2, mService)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700102 }
103
104protected:
105 static bool isEqualResources(const Vector<MediaResource> &resources1,
106 const Vector<MediaResource> &resources2) {
107 if (resources1.size() != resources2.size()) {
108 return false;
109 }
110 for (size_t i = 0; i < resources1.size(); ++i) {
111 if (resources1[i] != resources2[i]) {
112 return false;
113 }
114 }
115 return true;
116 }
117
118 static void expectEqResourceInfo(const ResourceInfo &info, sp<IResourceManagerClient> client,
119 const Vector<MediaResource> &resources) {
120 EXPECT_EQ(client, info.client);
121 EXPECT_TRUE(isEqualResources(resources, info.resources));
122 }
123
124 void verifyClients(bool c1, bool c2, bool c3) {
125 TestClient *client1 = static_cast<TestClient*>(mTestClient1.get());
126 TestClient *client2 = static_cast<TestClient*>(mTestClient2.get());
127 TestClient *client3 = static_cast<TestClient*>(mTestClient3.get());
128
129 EXPECT_EQ(c1, client1->reclaimed());
130 EXPECT_EQ(c2, client2->reclaimed());
131 EXPECT_EQ(c3, client3->reclaimed());
132
133 client1->reset();
134 client2->reset();
135 client3->reset();
136 }
137
Ronghua Wu67e7f542015-03-13 10:47:08 -0700138 // test set up
139 // ---------------------------------------------------------------------------------
140 // pid priority client type number
141 // ---------------------------------------------------------------------------------
142 // kTestPid1(30) 30 mTestClient1 secure codec 1
143 // graphic memory 200
144 // graphic memory 200
145 // ---------------------------------------------------------------------------------
146 // kTestPid2(20) 20 mTestClient2 non-secure codec 1
147 // graphic memory 300
148 // -------------------------------------------
149 // mTestClient3 secure codec 1
150 // graphic memory 100
151 // ---------------------------------------------------------------------------------
Ronghua Wu231c3d12015-03-11 15:10:32 -0700152 void addResource() {
153 // kTestPid1 mTestClient1
154 Vector<MediaResource> resources1;
155 resources1.push_back(MediaResource(String8(kResourceSecureCodec), 1));
Ronghua Wu37c89242015-07-15 12:23:48 -0700156 mService->addResource(kTestPid1, getId(mTestClient1), mTestClient1, resources1);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700157 resources1.push_back(MediaResource(String8(kResourceGraphicMemory), 200));
158 Vector<MediaResource> resources11;
159 resources11.push_back(MediaResource(String8(kResourceGraphicMemory), 200));
Ronghua Wu37c89242015-07-15 12:23:48 -0700160 mService->addResource(kTestPid1, getId(mTestClient1), mTestClient1, resources11);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700161
162 // kTestPid2 mTestClient2
163 Vector<MediaResource> resources2;
164 resources2.push_back(MediaResource(String8(kResourceNonSecureCodec), 1));
165 resources2.push_back(MediaResource(String8(kResourceGraphicMemory), 300));
Ronghua Wu37c89242015-07-15 12:23:48 -0700166 mService->addResource(kTestPid2, getId(mTestClient2), mTestClient2, resources2);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700167
168 // kTestPid2 mTestClient3
169 Vector<MediaResource> resources3;
Ronghua Wu37c89242015-07-15 12:23:48 -0700170 mService->addResource(kTestPid2, getId(mTestClient3), mTestClient3, resources3);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700171 resources3.push_back(MediaResource(String8(kResourceSecureCodec), 1));
172 resources3.push_back(MediaResource(String8(kResourceGraphicMemory), 100));
Ronghua Wu37c89242015-07-15 12:23:48 -0700173 mService->addResource(kTestPid2, getId(mTestClient3), mTestClient3, resources3);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700174
175 const PidResourceInfosMap &map = mService->mMap;
176 EXPECT_EQ(2u, map.size());
177 ssize_t index1 = map.indexOfKey(kTestPid1);
178 ASSERT_GE(index1, 0);
179 const ResourceInfos &infos1 = map[index1];
180 EXPECT_EQ(1u, infos1.size());
181 expectEqResourceInfo(infos1[0], mTestClient1, resources1);
182
183 ssize_t index2 = map.indexOfKey(kTestPid2);
184 ASSERT_GE(index2, 0);
185 const ResourceInfos &infos2 = map[index2];
186 EXPECT_EQ(2u, infos2.size());
187 expectEqResourceInfo(infos2[0], mTestClient2, resources2);
188 expectEqResourceInfo(infos2[1], mTestClient3, resources3);
189 }
190
191 void testConfig() {
192 EXPECT_TRUE(mService->mSupportsMultipleSecureCodecs);
193 EXPECT_TRUE(mService->mSupportsSecureWithNonSecureCodec);
194
195 Vector<MediaResourcePolicy> policies1;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700196 policies1.push_back(
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700197 MediaResourcePolicy(
198 String8(kPolicySupportsMultipleSecureCodecs),
199 String8("true")));
200 policies1.push_back(
201 MediaResourcePolicy(
202 String8(kPolicySupportsSecureWithNonSecureCodec),
203 String8("false")));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700204 mService->config(policies1);
205 EXPECT_TRUE(mService->mSupportsMultipleSecureCodecs);
206 EXPECT_FALSE(mService->mSupportsSecureWithNonSecureCodec);
207
208 Vector<MediaResourcePolicy> policies2;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700209 policies2.push_back(
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700210 MediaResourcePolicy(
211 String8(kPolicySupportsMultipleSecureCodecs),
212 String8("false")));
213 policies2.push_back(
214 MediaResourcePolicy(
215 String8(kPolicySupportsSecureWithNonSecureCodec),
216 String8("true")));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700217 mService->config(policies2);
218 EXPECT_FALSE(mService->mSupportsMultipleSecureCodecs);
219 EXPECT_TRUE(mService->mSupportsSecureWithNonSecureCodec);
220 }
221
222 void testRemoveResource() {
223 addResource();
224
Ronghua Wu37c89242015-07-15 12:23:48 -0700225 mService->removeResource(kTestPid2, getId(mTestClient2));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700226
227 const PidResourceInfosMap &map = mService->mMap;
228 EXPECT_EQ(2u, map.size());
229 const ResourceInfos &infos1 = map.valueFor(kTestPid1);
230 const ResourceInfos &infos2 = map.valueFor(kTestPid2);
231 EXPECT_EQ(1u, infos1.size());
232 EXPECT_EQ(1u, infos2.size());
233 // mTestClient2 has been removed.
234 EXPECT_EQ(mTestClient3, infos2[0].client);
235 }
236
237 void testGetAllClients() {
238 addResource();
239
240 String8 type = String8(kResourceSecureCodec);
241 String8 unknowType = String8("unknowType");
242 Vector<sp<IResourceManagerClient> > clients;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700243 EXPECT_FALSE(mService->getAllClients_l(kLowPriorityPid, type, &clients));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700244 // some higher priority process (e.g. kTestPid2) owns the resource, so getAllClients_l
245 // will fail.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700246 EXPECT_FALSE(mService->getAllClients_l(kMidPriorityPid, type, &clients));
247 EXPECT_TRUE(mService->getAllClients_l(kHighPriorityPid, unknowType, &clients));
248 EXPECT_TRUE(mService->getAllClients_l(kHighPriorityPid, type, &clients));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700249
250 EXPECT_EQ(2u, clients.size());
251 EXPECT_EQ(mTestClient3, clients[0]);
252 EXPECT_EQ(mTestClient1, clients[1]);
253 }
254
255 void testReclaimResourceSecure() {
256 Vector<MediaResource> resources;
257 resources.push_back(MediaResource(String8(kResourceSecureCodec), 1));
258 resources.push_back(MediaResource(String8(kResourceGraphicMemory), 150));
259
260 // ### secure codec can't coexist and secure codec can coexist with non-secure codec ###
261 {
262 addResource();
263 mService->mSupportsMultipleSecureCodecs = false;
264 mService->mSupportsSecureWithNonSecureCodec = true;
265
266 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700267 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
268 EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700269
270 // reclaim all secure codecs
Ronghua Wu05d89f12015-07-07 16:47:42 -0700271 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
272 verifyClients(true /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700273
274 // call again should reclaim one largest graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700275 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
276 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700277
278 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700279 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700280 }
281
282 // ### secure codecs can't coexist and secure codec can't coexist with non-secure codec ###
283 {
284 addResource();
285 mService->mSupportsMultipleSecureCodecs = false;
286 mService->mSupportsSecureWithNonSecureCodec = false;
287
288 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700289 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
290 EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700291
292 // reclaim all secure and non-secure codecs
Ronghua Wu05d89f12015-07-07 16:47:42 -0700293 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
294 verifyClients(true /* c1 */, true /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700295
296 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700297 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700298 }
299
300
301 // ### secure codecs can coexist but secure codec can't coexist with non-secure codec ###
302 {
303 addResource();
304 mService->mSupportsMultipleSecureCodecs = true;
305 mService->mSupportsSecureWithNonSecureCodec = false;
306
307 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700308 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
309 EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700310
311 // reclaim all non-secure codecs
Ronghua Wu05d89f12015-07-07 16:47:42 -0700312 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
313 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700314
315 // call again should reclaim one largest graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700316 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
317 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700318
319 // call again should reclaim another largest graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700320 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
321 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700322
323 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700324 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700325 }
326
327 // ### secure codecs can coexist and secure codec can coexist with non-secure codec ###
328 {
329 addResource();
330 mService->mSupportsMultipleSecureCodecs = true;
331 mService->mSupportsSecureWithNonSecureCodec = true;
332
333 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700334 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700335
Ronghua Wu05d89f12015-07-07 16:47:42 -0700336 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700337 // one largest graphic memory from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700338 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700339
340 // call again should reclaim another graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700341 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
342 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700343
344 // call again should reclaim another graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700345 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
346 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700347
348 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700349 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700350 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700351
352 // ### secure codecs can coexist and secure codec can coexist with non-secure codec ###
353 {
354 addResource();
355 mService->mSupportsMultipleSecureCodecs = true;
356 mService->mSupportsSecureWithNonSecureCodec = true;
357
358 Vector<MediaResource> resources;
359 resources.push_back(MediaResource(String8(kResourceSecureCodec), 1));
360
Ronghua Wu05d89f12015-07-07 16:47:42 -0700361 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700362 // secure codec from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700363 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700364
365 // call again should reclaim another secure codec from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700366 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
367 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700368
Ronghua Wu05d89f12015-07-07 16:47:42 -0700369 // no more secure codec, non-secure codec will be reclaimed.
370 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
371 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700372 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700373 }
374
375 void testReclaimResourceNonSecure() {
376 Vector<MediaResource> resources;
377 resources.push_back(MediaResource(String8(kResourceNonSecureCodec), 1));
378 resources.push_back(MediaResource(String8(kResourceGraphicMemory), 150));
379
380 // ### secure codec can't coexist with non-secure codec ###
381 {
382 addResource();
383 mService->mSupportsSecureWithNonSecureCodec = false;
384
385 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700386 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
387 EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700388
389 // reclaim all secure codecs
Ronghua Wu05d89f12015-07-07 16:47:42 -0700390 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
391 verifyClients(true /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700392
393 // call again should reclaim one graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700394 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
395 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700396
397 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700398 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700399 }
400
401
402 // ### secure codec can coexist with non-secure codec ###
403 {
404 addResource();
405 mService->mSupportsSecureWithNonSecureCodec = true;
406
407 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700408 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700409
Ronghua Wu05d89f12015-07-07 16:47:42 -0700410 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700411 // one largest graphic memory from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700412 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700413
414 // call again should reclaim another graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700415 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
416 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700417
418 // call again should reclaim another graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700419 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
420 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700421
422 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700423 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700424 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700425
426 // ### secure codec can coexist with non-secure codec ###
427 {
428 addResource();
429 mService->mSupportsSecureWithNonSecureCodec = true;
430
431 Vector<MediaResource> resources;
432 resources.push_back(MediaResource(String8(kResourceNonSecureCodec), 1));
433
Ronghua Wu05d89f12015-07-07 16:47:42 -0700434 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700435 // one non secure codec from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700436 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700437
Ronghua Wu05d89f12015-07-07 16:47:42 -0700438 // no more non-secure codec, secure codec from lowest priority process will be reclaimed
439 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
440 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700441
Ronghua Wu05d89f12015-07-07 16:47:42 -0700442 // clean up client 3 which still left
Ronghua Wu37c89242015-07-15 12:23:48 -0700443 mService->removeResource(kTestPid2, getId(mTestClient3));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700444 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700445 }
446
447 void testGetLowestPriorityBiggestClient() {
448 String8 type = String8(kResourceGraphicMemory);
449 sp<IResourceManagerClient> client;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700450 EXPECT_FALSE(mService->getLowestPriorityBiggestClient_l(kHighPriorityPid, type, &client));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700451
452 addResource();
453
Ronghua Wu05d89f12015-07-07 16:47:42 -0700454 EXPECT_FALSE(mService->getLowestPriorityBiggestClient_l(kLowPriorityPid, type, &client));
455 EXPECT_TRUE(mService->getLowestPriorityBiggestClient_l(kHighPriorityPid, type, &client));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700456
457 // kTestPid1 is the lowest priority process with kResourceGraphicMemory.
458 // mTestClient1 has the largest kResourceGraphicMemory within kTestPid1.
459 EXPECT_EQ(mTestClient1, client);
460 }
461
462 void testGetLowestPriorityPid() {
463 int pid;
464 int priority;
465 TestProcessInfo processInfo;
466
467 String8 type = String8(kResourceGraphicMemory);
468 EXPECT_FALSE(mService->getLowestPriorityPid_l(type, &pid, &priority));
469
470 addResource();
471
472 EXPECT_TRUE(mService->getLowestPriorityPid_l(type, &pid, &priority));
473 EXPECT_EQ(kTestPid1, pid);
474 int priority1;
475 processInfo.getPriority(kTestPid1, &priority1);
476 EXPECT_EQ(priority1, priority);
477
478 type = String8(kResourceNonSecureCodec);
479 EXPECT_TRUE(mService->getLowestPriorityPid_l(type, &pid, &priority));
480 EXPECT_EQ(kTestPid2, pid);
481 int priority2;
482 processInfo.getPriority(kTestPid2, &priority2);
483 EXPECT_EQ(priority2, priority);
484 }
485
486 void testGetBiggestClient() {
487 String8 type = String8(kResourceGraphicMemory);
488 sp<IResourceManagerClient> client;
489 EXPECT_FALSE(mService->getBiggestClient_l(kTestPid2, type, &client));
490
491 addResource();
492
493 EXPECT_TRUE(mService->getBiggestClient_l(kTestPid2, type, &client));
494 EXPECT_EQ(mTestClient2, client);
495 }
496
497 void testIsCallingPriorityHigher() {
498 EXPECT_FALSE(mService->isCallingPriorityHigher_l(101, 100));
499 EXPECT_FALSE(mService->isCallingPriorityHigher_l(100, 100));
500 EXPECT_TRUE(mService->isCallingPriorityHigher_l(99, 100));
501 }
502
503 sp<ResourceManagerService> mService;
504 sp<IResourceManagerClient> mTestClient1;
505 sp<IResourceManagerClient> mTestClient2;
506 sp<IResourceManagerClient> mTestClient3;
507};
508
509TEST_F(ResourceManagerServiceTest, config) {
510 testConfig();
511}
512
513TEST_F(ResourceManagerServiceTest, addResource) {
514 addResource();
515}
516
517TEST_F(ResourceManagerServiceTest, removeResource) {
518 testRemoveResource();
519}
520
521TEST_F(ResourceManagerServiceTest, reclaimResource) {
522 testReclaimResourceSecure();
523 testReclaimResourceNonSecure();
524}
525
526TEST_F(ResourceManagerServiceTest, getAllClients_l) {
527 testGetAllClients();
528}
529
530TEST_F(ResourceManagerServiceTest, getLowestPriorityBiggestClient_l) {
531 testGetLowestPriorityBiggestClient();
532}
533
534TEST_F(ResourceManagerServiceTest, getLowestPriorityPid_l) {
535 testGetLowestPriorityPid();
536}
537
538TEST_F(ResourceManagerServiceTest, getBiggestClient_l) {
539 testGetBiggestClient();
540}
541
542TEST_F(ResourceManagerServiceTest, isCallingPriorityHigher_l) {
543 testIsCallingPriorityHigher();
544}
545
546} // namespace android