1 #pragma once
2 
3 #include <chrono>
4 
5 #include <gtest/gtest.h>
6 
7 namespace android::hardware::health::test_utils {
8 
9 using testing::AssertionFailure;
10 using testing::AssertionResult;
11 using testing::AssertionSuccess;
12 using std::chrono_literals::operator""s;
13 
14 // Needs to be called repeatedly within a period of time to ensure values are initialized.
15 template <typename BatteryStatusType, typename BatteryStatusToStringFn>
IsBatteryCurrentSignCorrect(const BatteryStatusType & status,int32_t current,bool acceptZeroCurrentAsUnknown,const BatteryStatusToStringFn & toString)16 inline AssertionResult IsBatteryCurrentSignCorrect(const BatteryStatusType& status, int32_t current,
17                                                    bool acceptZeroCurrentAsUnknown,
18                                                    const BatteryStatusToStringFn& toString) {
19     // For IHealth.getCurrentNow/Average, if current is not available, it is expected that
20     // the error code is NOT_SUPPORTED, which is checked above. Hence, zero current is
21     // not treated as unknown values.
22     // For IHealth.getHealthInfo, if current is not available, health_info.current_* == 0.
23     // Caller of this function provides current.result == Result::SUCCESS. Hence, just skip the
24     // check.
25     if (current == 0 && acceptZeroCurrentAsUnknown) {
26         return AssertionSuccess()
27                << "current is 0, which indicates the value may not be available. Skipping.";
28     }
29 
30     switch (status) {
31         case BatteryStatusType::UNKNOWN:
32             if (current != 0) {
33                 // BatteryStatus may be UNKNOWN initially with a non-zero current value, but
34                 // after it is initialized, it should be known.
35                 return AssertionFailure()
36                        << "BatteryStatus is UNKNOWN but current is not 0. Actual: " << current;
37             }
38             break;
39         case BatteryStatusType::CHARGING:
40             if (current <= 0) {
41                 return AssertionFailure()
42                        << "BatteryStatus is CHARGING but current is not positive. Actual: "
43                        << current;
44             }
45             break;
46         case BatteryStatusType::NOT_CHARGING:
47             if (current > 0) {
48                 return AssertionFailure() << "BatteryStatus is " << toString(status)
49                                           << " but current is positive. Actual: " << current;
50             }
51             break;
52         case BatteryStatusType::DISCHARGING:
53             if (current >= 0) {
54                 return AssertionFailure() << "BatteryStatus is " << toString(status)
55                                           << " but current is not negative. Actual: " << current;
56             }
57             break;
58         case BatteryStatusType::FULL:
59             // Battery current may be positive or negative depending on the load.
60             break;
61         default:
62             return AssertionFailure() << "Unknown BatteryStatus " << toString(status);
63     }
64 
65     return AssertionSuccess() << "BatteryStatus is " << toString(status)
66                               << " and current has the correct sign: " << current;
67 }
68 
IsValueSimilar(int32_t dividend,int32_t divisor,double factor)69 inline AssertionResult IsValueSimilar(int32_t dividend, int32_t divisor, double factor) {
70     auto difference = abs(dividend - divisor);
71     if (difference > factor * abs(divisor)) {
72         return AssertionFailure() << dividend << " and " << divisor
73                                   << " are not similar (factor = " << factor << ")";
74     }
75     return AssertionSuccess() << dividend << " and " << divisor
76                               << " are similar (factor = " << factor << ")";
77 }
78 
IsBatteryCurrentSimilar(int32_t currentNow,int32_t currentAverage,double currentCompareFactor)79 inline AssertionResult IsBatteryCurrentSimilar(int32_t currentNow, int32_t currentAverage,
80                                                double currentCompareFactor) {
81     // Check that the two values are similar. Note that the two tests uses a different
82     // divisor to ensure that they are actually pretty similar. For example,
83     // IsValueSimilar(5,10,0.4) returns true, but IsValueSimlar(10,5,0.4) returns false.
84     auto res = IsValueSimilar(currentNow, currentAverage, currentCompareFactor)
85                << " for now vs. average. Check units.";
86     if (!res) return res;
87     res = IsValueSimilar(currentAverage, currentNow, currentCompareFactor)
88           << " for average vs. now. Check units.";
89     if (!res) return res;
90     return AssertionSuccess() << "currentNow = " << currentNow
91                               << " and currentAverage = " << currentAverage
92                               << " are considered similar.";
93 }
94 
95 // Test that f() returns AssertionSuccess() once in a given period of time.
96 template <typename Duration, typename Function>
SucceedOnce(Duration d,Function f)97 inline AssertionResult SucceedOnce(Duration d, Function f) {
98     AssertionResult result = AssertionFailure() << "Function is never evaluated.";
99     auto end = std::chrono::system_clock::now() + d;
100     while (std::chrono::system_clock::now() <= end) {
101         result = f();
102         if (result) {
103             return result;
104         }
105         std::this_thread::sleep_for(2s);
106     }
107     return result;
108 }
109 
110 template <typename BatteryStatusType, typename BatteryInfoType, typename BatteryStatusToStringFn>
IsBatteryStatusCorrect(const BatteryStatusType & status,const BatteryInfoType & batteryInfo,const BatteryStatusToStringFn & toString)111 AssertionResult IsBatteryStatusCorrect(const BatteryStatusType& status,
112                                        const BatteryInfoType& batteryInfo,
113                                        const BatteryStatusToStringFn& toString) {
114     bool isConnected = batteryInfo.chargerAcOnline || batteryInfo.chargerUsbOnline ||
115                        batteryInfo.chargerWirelessOnline;
116 
117     std::stringstream message;
118     message << "BatteryStatus is " << toString(status) << " and " << (isConnected ? "" : "no ")
119             << "power source is connected: ac=" << batteryInfo.chargerAcOnline
120             << ", usb=" << batteryInfo.chargerUsbOnline
121             << ", wireless=" << batteryInfo.chargerWirelessOnline;
122 
123     switch (status) {
124         case BatteryStatusType::UNKNOWN: {
125             // Don't enforce anything on isConnected on unknown battery status.
126             // Battery-less devices must report UNKNOWN battery status, but may report true
127             // or false on isConnected.
128         } break;
129         case BatteryStatusType::CHARGING:
130         case BatteryStatusType::NOT_CHARGING:
131         case BatteryStatusType::FULL: {
132             if (!isConnected) {
133                 return AssertionFailure() << message.str();
134             }
135         } break;
136         case BatteryStatusType::DISCHARGING: {
137             if (isConnected) {
138                 return AssertionFailure() << message.str();
139             }
140         } break;
141         default: {
142             return AssertionFailure() << "Unknown battery status value " << toString(status);
143         } break;
144     }
145 
146     return AssertionSuccess() << message.str();
147 }
148 
149 }  // namespace android::hardware::health::test_utils
150