Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 26 | namespace aidl { |
| 27 | namespace android { |
| 28 | namespace media { |
| 29 | bool 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 | |
| 35 | namespace android { |
| 36 | |
| 37 | using Status = ::ndk::ScopedAStatus; |
| 38 | using ::aidl::android::media::BnResourceManagerClient; |
| 39 | using ::aidl::android::media::IResourceManagerService; |
| 40 | using ::aidl::android::media::IResourceManagerClient; |
| 41 | using ::aidl::android::media::MediaResourceParcel; |
| 42 | |
| 43 | static int64_t getId(const std::shared_ptr<IResourceManagerClient>& client) { |
| 44 | return (int64_t) client.get(); |
| 45 | } |
| 46 | |
| 47 | struct 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 Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 62 | virtual bool overrideProcessInfo( |
| 63 | int /* pid */, int /* procState */, int /* oomScore */) { |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | virtual void removeProcessInfoOverride(int /* pid */) { |
| 68 | } |
| 69 | |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 70 | private: |
| 71 | DISALLOW_EVIL_CONSTRUCTORS(TestProcessInfo); |
| 72 | }; |
| 73 | |
| 74 | struct 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 | |
| 118 | protected: |
| 119 | virtual ~TestSystemCallback() {} |
| 120 | |
| 121 | private: |
| 122 | EventEntry mLastEvent; |
| 123 | size_t mEventCount; |
| 124 | |
| 125 | DISALLOW_EVIL_CONSTRUCTORS(TestSystemCallback); |
| 126 | }; |
| 127 | |
| 128 | |
| 129 | struct 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 | |
| 155 | private: |
| 156 | bool mReclaimed; |
| 157 | int mPid; |
| 158 | std::shared_ptr<ResourceManagerService> mService; |
| 159 | DISALLOW_EVIL_CONSTRUCTORS(TestClient); |
| 160 | }; |
| 161 | |
| 162 | static const int kTestPid1 = 30; |
| 163 | static const int kTestUid1 = 1010; |
| 164 | |
| 165 | static const int kTestPid2 = 20; |
| 166 | static const int kTestUid2 = 1011; |
| 167 | |
| 168 | static const int kLowPriorityPid = 40; |
| 169 | static const int kMidPriorityPid = 25; |
| 170 | static const int kHighPriorityPid = 10; |
| 171 | |
| 172 | using EventType = TestSystemCallback::EventType; |
| 173 | using EventEntry = TestSystemCallback::EventEntry; |
| 174 | bool 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 | |
| 184 | class ResourceManagerServiceTestBase : public ::testing::Test { |
| 185 | public: |
| 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 | |
| 201 | protected: |
| 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 |