1 /*
2  * Copyright (c) 2021, 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 #pragma once
18 
19 #include "IoOveruseConfigs.h"
20 
21 #include <aidl/android/automotive/watchdog/PerStateBytes.h>
22 #include <aidl/android/automotive/watchdog/internal/ApplicationCategoryType.h>
23 #include <aidl/android/automotive/watchdog/internal/ComponentType.h>
24 #include <aidl/android/automotive/watchdog/internal/PackageInfo.h>
25 #include <aidl/android/automotive/watchdog/internal/ResourceOveruseConfiguration.h>
26 #include <android-base/result.h>
27 #include <gmock/gmock.h>
28 
29 namespace android {
30 namespace automotive {
31 namespace watchdog {
32 
33 class MockIoOveruseConfigs : public IoOveruseConfigsInterface {
34 public:
MockIoOveruseConfigs()35     MockIoOveruseConfigs() {}
~MockIoOveruseConfigs()36     ~MockIoOveruseConfigs() {}
37     MOCK_METHOD(
38             android::base::Result<void>, update,
39             (const std::vector<
40                     aidl::android::automotive::watchdog::internal::ResourceOveruseConfiguration>&),
41             (override));
42 
43     MOCK_METHOD(
44             void, get,
45             (std::vector<
46                     aidl::android::automotive::watchdog::internal::ResourceOveruseConfiguration>*),
47             (const, override));
48 
49     MOCK_METHOD(android::base::Result<void>, writeToDisk, (), (override));
50 
51     MOCK_METHOD((const std::unordered_set<std::string>&), vendorPackagePrefixes, (), (override));
52 
53     MOCK_METHOD((const std::unordered_map<
54                         std::string,
55                         aidl::android::automotive::watchdog::internal::ApplicationCategoryType>&),
56                 packagesToAppCategories, (), (override));
57 
58     MOCK_METHOD(aidl::android::automotive::watchdog::PerStateBytes, fetchThreshold,
59                 (const aidl::android::automotive::watchdog::internal::PackageInfo&),
60                 (const, override));
61 
62     MOCK_METHOD(bool, isSafeToKill,
63                 (const aidl::android::automotive::watchdog::internal::PackageInfo&),
64                 (const, override));
65 
66     MOCK_METHOD((const IoOveruseAlertThresholdSet&), systemWideAlertThresholds, (), (override));
67 
68     struct PackageConfig {
69         aidl::android::automotive::watchdog::PerStateBytes threshold;
70         bool isSafeToKill = false;
71     };
72 
injectPackageConfigs(const std::unordered_map<std::string,PackageConfig> & perPackageConfig)73     void injectPackageConfigs(
74             const std::unordered_map<std::string, PackageConfig>& perPackageConfig) {
75         ON_CALL(*this, fetchThreshold(testing::_))
76                 .WillByDefault(
77                         [perPackageConfig = perPackageConfig](
78                                 const aidl::android::automotive::watchdog::internal::PackageInfo&
79                                         packageInfo) {
80                             const std::string packageName = packageInfo.packageIdentifier.name;
81                             if (const auto it = perPackageConfig.find(packageName);
82                                 it != perPackageConfig.end()) {
83                                 return it->second.threshold;
84                             }
85                             return defaultThreshold().perStateWriteBytes;
86                         });
87         ON_CALL(*this, isSafeToKill(testing::_))
88                 .WillByDefault(
89                         [perPackageConfig = perPackageConfig](
90                                 const aidl::android::automotive::watchdog::internal::PackageInfo&
91                                         packageInfo) {
92                             const std::string packageName = packageInfo.packageIdentifier.name;
93                             if (const auto it = perPackageConfig.find(packageName);
94                                 it != perPackageConfig.end()) {
95                                 return it->second.isSafeToKill;
96                             }
97                             return true;
98                         });
99     }
100 };
101 
102 }  // namespace watchdog
103 }  // namespace automotive
104 }  // namespace android
105