blob: 84c320dbc2ba1fd1dd682a66e780e5ec440bfbf4 [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
62private:
63 DISALLOW_EVIL_CONSTRUCTORS(TestProcessInfo);
64};
65
66struct TestSystemCallback :
67 public ResourceManagerService::SystemCallbackInterface {
68 TestSystemCallback() :
69 mLastEvent({EventType::INVALID, 0}), mEventCount(0) {}
70
71 enum EventType {
72 INVALID = -1,
73 VIDEO_ON = 0,
74 VIDEO_OFF = 1,
75 VIDEO_RESET = 2,
76 CPUSET_ENABLE = 3,
77 CPUSET_DISABLE = 4,
78 };
79
80 struct EventEntry {
81 EventType type;
82 int arg;
83 };
84
85 virtual void noteStartVideo(int uid) override {
86 mLastEvent = {EventType::VIDEO_ON, uid};
87 mEventCount++;
88 }
89
90 virtual void noteStopVideo(int uid) override {
91 mLastEvent = {EventType::VIDEO_OFF, uid};
92 mEventCount++;
93 }
94
95 virtual void noteResetVideo() override {
96 mLastEvent = {EventType::VIDEO_RESET, 0};
97 mEventCount++;
98 }
99
100 virtual bool requestCpusetBoost(bool enable) override {
101 mLastEvent = {enable ? EventType::CPUSET_ENABLE : EventType::CPUSET_DISABLE, 0};
102 mEventCount++;
103 return true;
104 }
105
106 size_t eventCount() { return mEventCount; }
107 EventType lastEventType() { return mLastEvent.type; }
108 EventEntry lastEvent() { return mLastEvent; }
109
110protected:
111 virtual ~TestSystemCallback() {}
112
113private:
114 EventEntry mLastEvent;
115 size_t mEventCount;
116
117 DISALLOW_EVIL_CONSTRUCTORS(TestSystemCallback);
118};
119
120
121struct TestClient : public BnResourceManagerClient {
122 TestClient(int pid, const std::shared_ptr<ResourceManagerService> &service)
123 : mReclaimed(false), mPid(pid), mService(service) {}
124
125 Status reclaimResource(bool* _aidl_return) override {
126 mService->removeClient(mPid, getId(ref<TestClient>()));
127 mReclaimed = true;
128 *_aidl_return = true;
129 return Status::ok();
130 }
131
132 Status getName(::std::string* _aidl_return) override {
133 *_aidl_return = "test_client";
134 return Status::ok();
135 }
136
137 bool reclaimed() const {
138 return mReclaimed;
139 }
140
141 void reset() {
142 mReclaimed = false;
143 }
144
145 virtual ~TestClient() {}
146
147private:
148 bool mReclaimed;
149 int mPid;
150 std::shared_ptr<ResourceManagerService> mService;
151 DISALLOW_EVIL_CONSTRUCTORS(TestClient);
152};
153
154static const int kTestPid1 = 30;
155static const int kTestUid1 = 1010;
156
157static const int kTestPid2 = 20;
158static const int kTestUid2 = 1011;
159
160static const int kLowPriorityPid = 40;
161static const int kMidPriorityPid = 25;
162static const int kHighPriorityPid = 10;
163
164using EventType = TestSystemCallback::EventType;
165using EventEntry = TestSystemCallback::EventEntry;
166bool operator== (const EventEntry& lhs, const EventEntry& rhs) {
167 return lhs.type == rhs.type && lhs.arg == rhs.arg;
168}
169
170#define CHECK_STATUS_TRUE(condition) \
171 EXPECT_TRUE((condition).isOk() && (result))
172
173#define CHECK_STATUS_FALSE(condition) \
174 EXPECT_TRUE((condition).isOk() && !(result))
175
176class ResourceManagerServiceTestBase : public ::testing::Test {
177public:
178 ResourceManagerServiceTestBase()
179 : mSystemCB(new TestSystemCallback()),
180 mService(::ndk::SharedRefBase::make<ResourceManagerService>(
181 new TestProcessInfo, mSystemCB)),
182 mTestClient1(::ndk::SharedRefBase::make<TestClient>(kTestPid1, mService)),
183 mTestClient2(::ndk::SharedRefBase::make<TestClient>(kTestPid2, mService)),
184 mTestClient3(::ndk::SharedRefBase::make<TestClient>(kTestPid2, mService)) {
185 }
186
187 sp<TestSystemCallback> mSystemCB;
188 std::shared_ptr<ResourceManagerService> mService;
189 std::shared_ptr<IResourceManagerClient> mTestClient1;
190 std::shared_ptr<IResourceManagerClient> mTestClient2;
191 std::shared_ptr<IResourceManagerClient> mTestClient3;
192
193protected:
194 static bool isEqualResources(const std::vector<MediaResourceParcel> &resources1,
195 const ResourceList &resources2) {
196 // convert resource1 to ResourceList
197 ResourceList r1;
198 for (size_t i = 0; i < resources1.size(); ++i) {
199 const auto &res = resources1[i];
200 const auto resType = std::tuple(res.type, res.subType, res.id);
201 r1[resType] = res;
202 }
203 return r1 == resources2;
204 }
205
206 static void expectEqResourceInfo(const ResourceInfo &info,
207 int uid,
208 std::shared_ptr<IResourceManagerClient> client,
209 const std::vector<MediaResourceParcel> &resources) {
210 EXPECT_EQ(uid, info.uid);
211 EXPECT_EQ(client, info.client);
212 EXPECT_TRUE(isEqualResources(resources, info.resources));
213 }
214};
215
216} // namespace android