Vishnu Nair | 8248b7c | 2018-08-01 10:13:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 com.android.server.wm.flicker; |
| 18 | |
| 19 | import static com.android.server.wm.flicker.AutomationUtils.setDefaultWait; |
| 20 | |
| 21 | import static com.google.common.truth.Truth.assertWithMessage; |
| 22 | |
| 23 | import android.platform.helpers.IAppHelper; |
| 24 | import android.support.test.InstrumentationRegistry; |
| 25 | import android.support.test.uiautomator.UiDevice; |
| 26 | import android.util.Log; |
| 27 | |
| 28 | import com.android.server.wm.flicker.TransitionRunner.TransitionResult; |
| 29 | |
| 30 | import org.junit.After; |
| 31 | import org.junit.AfterClass; |
| 32 | |
| 33 | import java.util.HashMap; |
| 34 | import java.util.List; |
| 35 | import java.util.function.Consumer; |
| 36 | |
| 37 | /** |
| 38 | * Base class of all Flicker test that performs common functions for all flicker tests: |
| 39 | * <p> |
| 40 | * - Caches transitions so that a transition is run once and the transition results are used by |
| 41 | * tests multiple times. This is needed for parameterized tests which call the BeforeClass methods |
| 42 | * multiple times. |
| 43 | * - Keeps track of all test artifacts and deletes ones which do not need to be reviewed. |
| 44 | * - Fails tests if results are not available for any test due to jank. |
| 45 | */ |
| 46 | public class FlickerTestBase { |
| 47 | public static final String TAG = "FLICKER"; |
| 48 | static final String NAVIGATION_BAR_WINDOW_TITLE = "NavigationBar"; |
| 49 | static final String STATUS_BAR_WINDOW_TITLE = "StatusBar"; |
| 50 | static final String DOCKED_STACK_DIVIDER = "DockedStackDivider"; |
| 51 | private static HashMap<String, List<TransitionResult>> transitionResults = |
| 52 | new HashMap<>(); |
| 53 | IAppHelper testApp; |
| 54 | UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); |
| 55 | private List<TransitionResult> results; |
| 56 | private TransitionResult lastResult = null; |
| 57 | |
| 58 | /** |
| 59 | * Teardown any system settings and clean up test artifacts from the file system. |
| 60 | * |
| 61 | * Note: test artifacts for failed tests will remain on the device. |
| 62 | */ |
| 63 | @AfterClass |
| 64 | public static void teardown() { |
| 65 | setDefaultWait(); |
| 66 | transitionResults.values().stream() |
| 67 | .flatMap(List::stream) |
| 68 | .forEach(result -> { |
| 69 | if (result.canDelete()) { |
| 70 | result.delete(); |
| 71 | } else { |
| 72 | if (result.layersTraceExists()) { |
| 73 | Log.e(TAG, "Layers trace saved to " + result.getLayersTracePath()); |
| 74 | } |
| 75 | if (result.windowManagerTraceExists()) { |
| 76 | Log.e(TAG, "WindowManager trace saved to " + result |
| 77 | .getWindowManagerTracePath |
| 78 | ()); |
| 79 | } |
| 80 | if (result.screenCaptureVideoExists()) { |
| 81 | Log.e(TAG, "Screen capture video saved to " + result |
| 82 | .screenCaptureVideo.toString()); |
| 83 | } |
| 84 | } |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Runs a transition, returns a cached result if the transition has run before. |
| 90 | */ |
| 91 | void runTransition(TransitionRunner transition) { |
| 92 | if (transitionResults.containsKey(transition.getTestTag())) { |
| 93 | results = transitionResults.get(transition.getTestTag()); |
| 94 | return; |
| 95 | } |
| 96 | results = transition.run().getResults(); |
| 97 | /* Fail if we don't have any results due to jank */ |
| 98 | assertWithMessage("No results to test because all transition runs were invalid because " |
| 99 | + "of Jank").that(results).isNotEmpty(); |
| 100 | transitionResults.put(transition.getTestTag(), results); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Goes through a list of transition results and checks assertions on each result. |
| 105 | */ |
| 106 | void checkResults(Consumer<TransitionResult> assertion) { |
| 107 | |
| 108 | for (TransitionResult result : results) { |
| 109 | lastResult = result; |
| 110 | assertion.accept(result); |
| 111 | } |
| 112 | lastResult = null; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Kludge to mark a file for saving. If {@code checkResults} fails, the last result is not |
| 117 | * cleared. This indicates the assertion failed for the result, so mark it for saving. |
| 118 | */ |
| 119 | @After |
| 120 | public void markArtifactsForSaving() { |
| 121 | if (lastResult != null) { |
| 122 | lastResult.flagForSaving(); |
| 123 | } |
| 124 | } |
| 125 | } |