blob: 8ae6a558a130978a0877cc82e20b2ceb06606ffe [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
32struct TestProcessInfo : public ProcessInfoInterface {
33 TestProcessInfo() {}
34 virtual ~TestProcessInfo() {}
35
36 virtual bool getPriority(int pid, int *priority) {
37 // For testing, use pid as priority.
38 // Lower the value higher the priority.
39 *priority = pid;
40 return true;
41 }
42
43private:
44 DISALLOW_EVIL_CONSTRUCTORS(TestProcessInfo);
45};
46
47struct TestClient : public BnResourceManagerClient {
48 TestClient(sp<ResourceManagerService> service)
49 : mReclaimed(false), mService(service) {}
50
51 virtual bool reclaimResource() {
52 sp<IResourceManagerClient> client(this);
53 mService->removeResource((int64_t) client.get());
54 mReclaimed = true;
55 return true;
56 }
57
Ronghua Wu8f9dd872015-04-23 15:24:25 -070058 virtual String8 getName() {
59 return String8("test_client");
60 }
61
Ronghua Wu231c3d12015-03-11 15:10:32 -070062 bool reclaimed() const {
63 return mReclaimed;
64 }
65
66 void reset() {
67 mReclaimed = false;
68 }
69
70protected:
71 virtual ~TestClient() {}
72
73private:
74 bool mReclaimed;
75 sp<ResourceManagerService> mService;
76 DISALLOW_EVIL_CONSTRUCTORS(TestClient);
77};
78
79static const int kTestPid1 = 30;
80static const int kTestPid2 = 20;
81
Ronghua Wu05d89f12015-07-07 16:47:42 -070082static const int kLowPriorityPid = 40;
83static const int kMidPriorityPid = 25;
84static const int kHighPriorityPid = 10;
85
Ronghua Wu231c3d12015-03-11 15:10:32 -070086class ResourceManagerServiceTest : public ::testing::Test {
87public:
88 ResourceManagerServiceTest()
89 : mService(new ResourceManagerService(new TestProcessInfo)),
90 mTestClient1(new TestClient(mService)),
91 mTestClient2(new TestClient(mService)),
92 mTestClient3(new TestClient(mService)) {
93 }
94
95protected:
96 static bool isEqualResources(const Vector<MediaResource> &resources1,
97 const Vector<MediaResource> &resources2) {
98 if (resources1.size() != resources2.size()) {
99 return false;
100 }
101 for (size_t i = 0; i < resources1.size(); ++i) {
102 if (resources1[i] != resources2[i]) {
103 return false;
104 }
105 }
106 return true;
107 }
108
109 static void expectEqResourceInfo(const ResourceInfo &info, sp<IResourceManagerClient> client,
110 const Vector<MediaResource> &resources) {
111 EXPECT_EQ(client, info.client);
112 EXPECT_TRUE(isEqualResources(resources, info.resources));
113 }
114
115 void verifyClients(bool c1, bool c2, bool c3) {
116 TestClient *client1 = static_cast<TestClient*>(mTestClient1.get());
117 TestClient *client2 = static_cast<TestClient*>(mTestClient2.get());
118 TestClient *client3 = static_cast<TestClient*>(mTestClient3.get());
119
120 EXPECT_EQ(c1, client1->reclaimed());
121 EXPECT_EQ(c2, client2->reclaimed());
122 EXPECT_EQ(c3, client3->reclaimed());
123
124 client1->reset();
125 client2->reset();
126 client3->reset();
127 }
128
Ronghua Wu67e7f542015-03-13 10:47:08 -0700129 // test set up
130 // ---------------------------------------------------------------------------------
131 // pid priority client type number
132 // ---------------------------------------------------------------------------------
133 // kTestPid1(30) 30 mTestClient1 secure codec 1
134 // graphic memory 200
135 // graphic memory 200
136 // ---------------------------------------------------------------------------------
137 // kTestPid2(20) 20 mTestClient2 non-secure codec 1
138 // graphic memory 300
139 // -------------------------------------------
140 // mTestClient3 secure codec 1
141 // graphic memory 100
142 // ---------------------------------------------------------------------------------
Ronghua Wu231c3d12015-03-11 15:10:32 -0700143 void addResource() {
144 // kTestPid1 mTestClient1
145 Vector<MediaResource> resources1;
146 resources1.push_back(MediaResource(String8(kResourceSecureCodec), 1));
147 mService->addResource(kTestPid1, (int64_t) mTestClient1.get(), mTestClient1, resources1);
148 resources1.push_back(MediaResource(String8(kResourceGraphicMemory), 200));
149 Vector<MediaResource> resources11;
150 resources11.push_back(MediaResource(String8(kResourceGraphicMemory), 200));
151 mService->addResource(kTestPid1, (int64_t) mTestClient1.get(), mTestClient1, resources11);
152
153 // kTestPid2 mTestClient2
154 Vector<MediaResource> resources2;
155 resources2.push_back(MediaResource(String8(kResourceNonSecureCodec), 1));
156 resources2.push_back(MediaResource(String8(kResourceGraphicMemory), 300));
157 mService->addResource(kTestPid2, (int64_t) mTestClient2.get(), mTestClient2, resources2);
158
159 // kTestPid2 mTestClient3
160 Vector<MediaResource> resources3;
161 mService->addResource(kTestPid2, (int64_t) mTestClient3.get(), mTestClient3, resources3);
162 resources3.push_back(MediaResource(String8(kResourceSecureCodec), 1));
163 resources3.push_back(MediaResource(String8(kResourceGraphicMemory), 100));
164 mService->addResource(kTestPid2, (int64_t) mTestClient3.get(), mTestClient3, resources3);
165
166 const PidResourceInfosMap &map = mService->mMap;
167 EXPECT_EQ(2u, map.size());
168 ssize_t index1 = map.indexOfKey(kTestPid1);
169 ASSERT_GE(index1, 0);
170 const ResourceInfos &infos1 = map[index1];
171 EXPECT_EQ(1u, infos1.size());
172 expectEqResourceInfo(infos1[0], mTestClient1, resources1);
173
174 ssize_t index2 = map.indexOfKey(kTestPid2);
175 ASSERT_GE(index2, 0);
176 const ResourceInfos &infos2 = map[index2];
177 EXPECT_EQ(2u, infos2.size());
178 expectEqResourceInfo(infos2[0], mTestClient2, resources2);
179 expectEqResourceInfo(infos2[1], mTestClient3, resources3);
180 }
181
182 void testConfig() {
183 EXPECT_TRUE(mService->mSupportsMultipleSecureCodecs);
184 EXPECT_TRUE(mService->mSupportsSecureWithNonSecureCodec);
185
186 Vector<MediaResourcePolicy> policies1;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700187 policies1.push_back(
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700188 MediaResourcePolicy(
189 String8(kPolicySupportsMultipleSecureCodecs),
190 String8("true")));
191 policies1.push_back(
192 MediaResourcePolicy(
193 String8(kPolicySupportsSecureWithNonSecureCodec),
194 String8("false")));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700195 mService->config(policies1);
196 EXPECT_TRUE(mService->mSupportsMultipleSecureCodecs);
197 EXPECT_FALSE(mService->mSupportsSecureWithNonSecureCodec);
198
199 Vector<MediaResourcePolicy> policies2;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700200 policies2.push_back(
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700201 MediaResourcePolicy(
202 String8(kPolicySupportsMultipleSecureCodecs),
203 String8("false")));
204 policies2.push_back(
205 MediaResourcePolicy(
206 String8(kPolicySupportsSecureWithNonSecureCodec),
207 String8("true")));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700208 mService->config(policies2);
209 EXPECT_FALSE(mService->mSupportsMultipleSecureCodecs);
210 EXPECT_TRUE(mService->mSupportsSecureWithNonSecureCodec);
211 }
212
213 void testRemoveResource() {
214 addResource();
215
216 mService->removeResource((int64_t) mTestClient2.get());
217
218 const PidResourceInfosMap &map = mService->mMap;
219 EXPECT_EQ(2u, map.size());
220 const ResourceInfos &infos1 = map.valueFor(kTestPid1);
221 const ResourceInfos &infos2 = map.valueFor(kTestPid2);
222 EXPECT_EQ(1u, infos1.size());
223 EXPECT_EQ(1u, infos2.size());
224 // mTestClient2 has been removed.
225 EXPECT_EQ(mTestClient3, infos2[0].client);
226 }
227
228 void testGetAllClients() {
229 addResource();
230
231 String8 type = String8(kResourceSecureCodec);
232 String8 unknowType = String8("unknowType");
233 Vector<sp<IResourceManagerClient> > clients;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700234 EXPECT_FALSE(mService->getAllClients_l(kLowPriorityPid, type, &clients));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700235 // some higher priority process (e.g. kTestPid2) owns the resource, so getAllClients_l
236 // will fail.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700237 EXPECT_FALSE(mService->getAllClients_l(kMidPriorityPid, type, &clients));
238 EXPECT_TRUE(mService->getAllClients_l(kHighPriorityPid, unknowType, &clients));
239 EXPECT_TRUE(mService->getAllClients_l(kHighPriorityPid, type, &clients));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700240
241 EXPECT_EQ(2u, clients.size());
242 EXPECT_EQ(mTestClient3, clients[0]);
243 EXPECT_EQ(mTestClient1, clients[1]);
244 }
245
246 void testReclaimResourceSecure() {
247 Vector<MediaResource> resources;
248 resources.push_back(MediaResource(String8(kResourceSecureCodec), 1));
249 resources.push_back(MediaResource(String8(kResourceGraphicMemory), 150));
250
251 // ### secure codec can't coexist and secure codec can coexist with non-secure codec ###
252 {
253 addResource();
254 mService->mSupportsMultipleSecureCodecs = false;
255 mService->mSupportsSecureWithNonSecureCodec = true;
256
257 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700258 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
259 EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700260
261 // reclaim all secure codecs
Ronghua Wu05d89f12015-07-07 16:47:42 -0700262 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
263 verifyClients(true /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700264
265 // call again should reclaim one largest graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700266 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
267 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700268
269 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700270 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700271 }
272
273 // ### secure codecs can't coexist and secure codec can't coexist with non-secure codec ###
274 {
275 addResource();
276 mService->mSupportsMultipleSecureCodecs = false;
277 mService->mSupportsSecureWithNonSecureCodec = false;
278
279 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700280 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
281 EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700282
283 // reclaim all secure and non-secure codecs
Ronghua Wu05d89f12015-07-07 16:47:42 -0700284 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
285 verifyClients(true /* c1 */, true /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700286
287 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700288 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700289 }
290
291
292 // ### secure codecs can coexist but secure codec can't coexist with non-secure codec ###
293 {
294 addResource();
295 mService->mSupportsMultipleSecureCodecs = true;
296 mService->mSupportsSecureWithNonSecureCodec = false;
297
298 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700299 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
300 EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700301
302 // reclaim all non-secure codecs
Ronghua Wu05d89f12015-07-07 16:47:42 -0700303 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
304 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700305
306 // call again should reclaim one largest graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700307 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
308 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700309
310 // call again should reclaim another largest graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700311 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
312 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700313
314 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700315 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700316 }
317
318 // ### secure codecs can coexist and secure codec can coexist with non-secure codec ###
319 {
320 addResource();
321 mService->mSupportsMultipleSecureCodecs = true;
322 mService->mSupportsSecureWithNonSecureCodec = true;
323
324 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700325 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700326
Ronghua Wu05d89f12015-07-07 16:47:42 -0700327 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700328 // one largest graphic memory from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700329 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700330
331 // call again should reclaim another graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700332 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
333 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700334
335 // call again should reclaim another graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700336 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
337 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700338
339 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700340 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700341 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700342
343 // ### secure codecs can coexist and secure codec can coexist with non-secure codec ###
344 {
345 addResource();
346 mService->mSupportsMultipleSecureCodecs = true;
347 mService->mSupportsSecureWithNonSecureCodec = true;
348
349 Vector<MediaResource> resources;
350 resources.push_back(MediaResource(String8(kResourceSecureCodec), 1));
351
Ronghua Wu05d89f12015-07-07 16:47:42 -0700352 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700353 // secure codec from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700354 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700355
356 // call again should reclaim another secure codec from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700357 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
358 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700359
Ronghua Wu05d89f12015-07-07 16:47:42 -0700360 // no more secure codec, non-secure codec will be reclaimed.
361 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
362 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700363 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700364 }
365
366 void testReclaimResourceNonSecure() {
367 Vector<MediaResource> resources;
368 resources.push_back(MediaResource(String8(kResourceNonSecureCodec), 1));
369 resources.push_back(MediaResource(String8(kResourceGraphicMemory), 150));
370
371 // ### secure codec can't coexist with non-secure codec ###
372 {
373 addResource();
374 mService->mSupportsSecureWithNonSecureCodec = false;
375
376 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700377 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
378 EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700379
380 // reclaim all secure codecs
Ronghua Wu05d89f12015-07-07 16:47:42 -0700381 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
382 verifyClients(true /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700383
384 // call again should reclaim one graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700385 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
386 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700387
388 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700389 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700390 }
391
392
393 // ### secure codec can coexist with non-secure codec ###
394 {
395 addResource();
396 mService->mSupportsSecureWithNonSecureCodec = true;
397
398 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700399 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700400
Ronghua Wu05d89f12015-07-07 16:47:42 -0700401 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700402 // one largest graphic memory from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700403 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700404
405 // call again should reclaim another graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700406 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
407 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700408
409 // call again should reclaim another graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700410 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
411 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700412
413 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700414 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700415 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700416
417 // ### secure codec can coexist with non-secure codec ###
418 {
419 addResource();
420 mService->mSupportsSecureWithNonSecureCodec = true;
421
422 Vector<MediaResource> resources;
423 resources.push_back(MediaResource(String8(kResourceNonSecureCodec), 1));
424
Ronghua Wu05d89f12015-07-07 16:47:42 -0700425 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700426 // one non secure codec from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700427 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700428
Ronghua Wu05d89f12015-07-07 16:47:42 -0700429 // no more non-secure codec, secure codec from lowest priority process will be reclaimed
430 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
431 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700432
Ronghua Wu05d89f12015-07-07 16:47:42 -0700433 // clean up client 3 which still left
Ronghua Wu67e7f542015-03-13 10:47:08 -0700434 mService->removeResource((int64_t) mTestClient3.get());
435 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700436 }
437
438 void testGetLowestPriorityBiggestClient() {
439 String8 type = String8(kResourceGraphicMemory);
440 sp<IResourceManagerClient> client;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700441 EXPECT_FALSE(mService->getLowestPriorityBiggestClient_l(kHighPriorityPid, type, &client));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700442
443 addResource();
444
Ronghua Wu05d89f12015-07-07 16:47:42 -0700445 EXPECT_FALSE(mService->getLowestPriorityBiggestClient_l(kLowPriorityPid, type, &client));
446 EXPECT_TRUE(mService->getLowestPriorityBiggestClient_l(kHighPriorityPid, type, &client));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700447
448 // kTestPid1 is the lowest priority process with kResourceGraphicMemory.
449 // mTestClient1 has the largest kResourceGraphicMemory within kTestPid1.
450 EXPECT_EQ(mTestClient1, client);
451 }
452
453 void testGetLowestPriorityPid() {
454 int pid;
455 int priority;
456 TestProcessInfo processInfo;
457
458 String8 type = String8(kResourceGraphicMemory);
459 EXPECT_FALSE(mService->getLowestPriorityPid_l(type, &pid, &priority));
460
461 addResource();
462
463 EXPECT_TRUE(mService->getLowestPriorityPid_l(type, &pid, &priority));
464 EXPECT_EQ(kTestPid1, pid);
465 int priority1;
466 processInfo.getPriority(kTestPid1, &priority1);
467 EXPECT_EQ(priority1, priority);
468
469 type = String8(kResourceNonSecureCodec);
470 EXPECT_TRUE(mService->getLowestPriorityPid_l(type, &pid, &priority));
471 EXPECT_EQ(kTestPid2, pid);
472 int priority2;
473 processInfo.getPriority(kTestPid2, &priority2);
474 EXPECT_EQ(priority2, priority);
475 }
476
477 void testGetBiggestClient() {
478 String8 type = String8(kResourceGraphicMemory);
479 sp<IResourceManagerClient> client;
480 EXPECT_FALSE(mService->getBiggestClient_l(kTestPid2, type, &client));
481
482 addResource();
483
484 EXPECT_TRUE(mService->getBiggestClient_l(kTestPid2, type, &client));
485 EXPECT_EQ(mTestClient2, client);
486 }
487
488 void testIsCallingPriorityHigher() {
489 EXPECT_FALSE(mService->isCallingPriorityHigher_l(101, 100));
490 EXPECT_FALSE(mService->isCallingPriorityHigher_l(100, 100));
491 EXPECT_TRUE(mService->isCallingPriorityHigher_l(99, 100));
492 }
493
494 sp<ResourceManagerService> mService;
495 sp<IResourceManagerClient> mTestClient1;
496 sp<IResourceManagerClient> mTestClient2;
497 sp<IResourceManagerClient> mTestClient3;
498};
499
500TEST_F(ResourceManagerServiceTest, config) {
501 testConfig();
502}
503
504TEST_F(ResourceManagerServiceTest, addResource) {
505 addResource();
506}
507
508TEST_F(ResourceManagerServiceTest, removeResource) {
509 testRemoveResource();
510}
511
512TEST_F(ResourceManagerServiceTest, reclaimResource) {
513 testReclaimResourceSecure();
514 testReclaimResourceNonSecure();
515}
516
517TEST_F(ResourceManagerServiceTest, getAllClients_l) {
518 testGetAllClients();
519}
520
521TEST_F(ResourceManagerServiceTest, getLowestPriorityBiggestClient_l) {
522 testGetLowestPriorityBiggestClient();
523}
524
525TEST_F(ResourceManagerServiceTest, getLowestPriorityPid_l) {
526 testGetLowestPriorityPid();
527}
528
529TEST_F(ResourceManagerServiceTest, getBiggestClient_l) {
530 testGetBiggestClient();
531}
532
533TEST_F(ResourceManagerServiceTest, isCallingPriorityHigher_l) {
534 testIsCallingPriorityHigher();
535}
536
537} // namespace android