1 /* 2 * Copyright (C) 2020 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 package android.location.cts.common; 18 19 import android.annotation.Nullable; 20 import android.util.Log; 21 22 import junit.framework.Assert; 23 24 import java.util.ArrayList; 25 import java.util.List; 26 27 /** 28 * Custom Assertion class. This is useful for doing multiple validations together 29 * without failing the test. Tests don’t stop running even if an assertion condition fails, 30 * but the test itself is marked as a failed test to indicate the right result 31 * at the end of the test. 32 */ 33 public class SoftAssert { 34 35 List<String> mErrorList; 36 private String mTag; 37 SoftAssert(String source)38 public SoftAssert(String source) { 39 mErrorList = new ArrayList<>(); 40 mTag = source; 41 } 42 43 /** 44 * Check if condition is true 45 * 46 * @param message test message 47 * @param eventTimeInNs the time at which the condition occurred 48 * @param expectedResult expected value 49 * @param actualResult actual value 50 * @param condition condition for test 51 */ assertTrue(String message, Long eventTimeInNs, String expectedResult, String actualResult, boolean condition)52 public void assertTrue(String message, Long eventTimeInNs, String expectedResult, 53 String actualResult, boolean condition) { 54 if (condition) { 55 Log.i(mTag, message + ", (Test: PASS, actual : " + 56 actualResult + ", expected: " + expectedResult + ")"); 57 } else { 58 String errorMessage = ""; 59 if (eventTimeInNs != null) { 60 errorMessage = "At time = " + eventTimeInNs + " ns, "; 61 } 62 errorMessage += message + 63 " (Test: FAIL, actual :" + actualResult + ", " + 64 "expected: " + expectedResult + ")"; 65 Log.e(mTag, errorMessage); 66 mErrorList.add(errorMessage); 67 } 68 } 69 70 /** 71 * assertTrue without eventTime 72 * 73 * @param message test message 74 * @param expectedResult expected value 75 * @param actualResult actual value 76 * @param condition condition for test 77 */ assertTrue(String message, String expectedResult, String actualResult, boolean condition)78 public void assertTrue(String message, String expectedResult, 79 String actualResult, boolean condition) { 80 assertTrue(message, null, expectedResult, actualResult, condition); 81 } 82 83 /** 84 * Check if a condition is true. 85 * NOTE: A failure is downgraded to a warning. 86 * 87 * @param message the message associated with the condition 88 * @param eventTimeInNs the time at which the condition occurred 89 * @param expectedResult the expected result of the condition 90 * @param actualResult the actual result of the condition 91 * @param condition the condition status 92 */ assertTrueAsWarning( String message, long eventTimeInNs, String expectedResult, String actualResult, boolean condition)93 public void assertTrueAsWarning( 94 String message, 95 long eventTimeInNs, 96 String expectedResult, 97 String actualResult, 98 boolean condition) { 99 if (condition) { 100 String formattedMessage = String.format( 101 "%s, (Test: PASS, actual : %s, expected : %s)", 102 message, 103 actualResult, 104 expectedResult); 105 Log.i(mTag, formattedMessage); 106 } else { 107 String formattedMessage = String.format( 108 "At time = %d ns, %s (Test: WARNING, actual : %s, expected : %s).", 109 eventTimeInNs, 110 message, 111 actualResult, 112 expectedResult); 113 failAsWarning(mTag, formattedMessage); 114 } 115 } 116 117 /** 118 * Check if condition is true 119 * 120 * @param message test message 121 * @param condition condition for test 122 */ assertTrue(String message, boolean condition)123 public void assertTrue(String message, boolean condition) { 124 assertOrWarnTrue(true, message, condition); 125 } 126 127 /** 128 * Check if condition is true 129 * 130 * @param strict if true, add this to the failure list, else, log a warning message 131 * @param message the message associated with the condition 132 * @param eventTimeInNs the time at which the condition occurred 133 * @param expectedResult the expected result of the condition 134 * @param actualResult the actual result of the condition 135 * @param condition condition for test 136 */ assertOrWarnTrue( boolean strict, String message, @Nullable Long eventTimeInNs, String expectedResult, String actualResult, boolean condition)137 public void assertOrWarnTrue( 138 boolean strict, 139 String message, 140 @Nullable Long eventTimeInNs, 141 String expectedResult, 142 String actualResult, 143 boolean condition) { 144 String formattedMessage; 145 if (eventTimeInNs != null) { 146 formattedMessage = 147 String.format( 148 "At time = %d ns, %s (actual : %s, expected : %s).", 149 eventTimeInNs, 150 message, 151 actualResult, 152 expectedResult); 153 } else { 154 formattedMessage = 155 String.format( 156 "%s (actual : %s, expected : %s).", 157 message, 158 actualResult, 159 expectedResult); 160 } 161 assertOrWarnTrue(strict, formattedMessage, condition); 162 } 163 164 /** 165 * Check if condition is true 166 * 167 * @param strict if true, add this to the failure list, else, log a warning message 168 * @param message message to describe the test - output on pass or fail 169 * @param condition condition for test 170 */ assertOrWarnTrue(boolean strict, String message, boolean condition)171 public void assertOrWarnTrue(boolean strict, String message, boolean condition) { 172 if (condition) { 173 Log.i(mTag, "(Test: PASS) " + message); 174 } else { 175 String errorMessage = "(Test: FAIL) " + message; 176 Log.i(mTag, errorMessage); 177 if (strict) { 178 mErrorList.add(errorMessage); 179 } else { 180 failAsWarning(mTag, errorMessage); 181 } 182 } 183 } 184 185 /** 186 * Assert all conditions together. This method collates all the failures and decides 187 * whether to fail the test or not at the end. This must be called at the end of the test. 188 */ assertAll()189 public void assertAll() { 190 if (mErrorList.isEmpty()) { 191 Log.i(mTag, "All test pass."); 192 // Test pass if there are no error message in errorMessageSet 193 Assert.assertTrue(true); 194 } else { 195 StringBuilder message = new StringBuilder(); 196 for (String msg : mErrorList) { 197 message.append(msg + "\n"); 198 } 199 Log.e(mTag, "Failing tests are: \n" + message); 200 Assert.fail("Failing tests are: \n" + message); 201 } 202 } 203 204 /** 205 * A hard or soft failure, depending on the setting. 206 * TODO - make this cleaner - e.g. refactor this out completely: get rid of static methods, 207 * and make a class (say TestVerification) the only entry point for verifications, 208 * so strict vs not can be abstracted away test implementations. 209 */ failOrWarning(boolean testIsStrict, String message, boolean condition)210 public static void failOrWarning(boolean testIsStrict, String message, boolean condition) { 211 if (testIsStrict) { 212 Assert.assertTrue(message, condition); 213 } else { 214 if (!condition) { 215 failAsWarning("", message); 216 } 217 } 218 } 219 220 /** 221 * A soft failure. In the current state of the tests, it will only log a warning and let the 222 * test be reported as 'pass'. 223 */ failAsWarning(String tag, String message)224 public static void failAsWarning(String tag, String message) { 225 Log.w(tag, message + " [NOTE: in a future release this feature might become mandatory, and" 226 + " this warning will fail the test]."); 227 } 228 } 229