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