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.google.common.truth.Truth.assertAbout; |
| 20 | import static com.google.common.truth.Truth.assertWithMessage; |
| 21 | |
| 22 | import android.annotation.Nullable; |
| 23 | import android.graphics.Rect; |
| 24 | |
| 25 | import com.android.server.wm.flicker.Assertions.Result; |
| 26 | import com.android.server.wm.flicker.LayersTrace.Entry; |
| 27 | import com.android.server.wm.flicker.TransitionRunner.TransitionResult; |
| 28 | |
Colin Cross | b6cf22d | 2019-04-12 14:34:58 -0700 | [diff] [blame] | 29 | import com.google.common.truth.FailureMetadata; |
Vishnu Nair | 8248b7c | 2018-08-01 10:13:36 -0700 | [diff] [blame] | 30 | import com.google.common.truth.Subject; |
Vishnu Nair | 8248b7c | 2018-08-01 10:13:36 -0700 | [diff] [blame] | 31 | |
| 32 | import java.util.List; |
| 33 | import java.util.stream.Collectors; |
| 34 | |
| 35 | /** |
| 36 | * Truth subject for {@link LayersTrace} objects. |
| 37 | */ |
| 38 | public class LayersTraceSubject extends Subject<LayersTraceSubject, LayersTrace> { |
| 39 | // Boiler-plate Subject.Factory for LayersTraceSubject |
Colin Cross | b6cf22d | 2019-04-12 14:34:58 -0700 | [diff] [blame] | 40 | private static final Subject.Factory<LayersTraceSubject, LayersTrace> FACTORY = |
| 41 | new Subject.Factory<LayersTraceSubject, LayersTrace>() { |
Vishnu Nair | 8248b7c | 2018-08-01 10:13:36 -0700 | [diff] [blame] | 42 | @Override |
Colin Cross | b6cf22d | 2019-04-12 14:34:58 -0700 | [diff] [blame] | 43 | public LayersTraceSubject createSubject( |
| 44 | FailureMetadata fm, @Nullable LayersTrace target) { |
| 45 | return new LayersTraceSubject(fm, target); |
Vishnu Nair | 8248b7c | 2018-08-01 10:13:36 -0700 | [diff] [blame] | 46 | } |
| 47 | }; |
| 48 | |
| 49 | private AssertionsChecker<Entry> mChecker = new AssertionsChecker<>(); |
| 50 | |
Colin Cross | b6cf22d | 2019-04-12 14:34:58 -0700 | [diff] [blame] | 51 | private LayersTraceSubject(FailureMetadata fm, @Nullable LayersTrace subject) { |
| 52 | super(fm, subject); |
Vishnu Nair | 8248b7c | 2018-08-01 10:13:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // User-defined entry point |
| 56 | public static LayersTraceSubject assertThat(@Nullable LayersTrace entry) { |
| 57 | return assertAbout(FACTORY).that(entry); |
| 58 | } |
| 59 | |
| 60 | // User-defined entry point |
| 61 | public static LayersTraceSubject assertThat(@Nullable TransitionResult result) { |
| 62 | LayersTrace entries = LayersTrace.parseFrom(result.getLayersTrace(), |
| 63 | result.getLayersTracePath()); |
| 64 | return assertWithMessage(result.toString()).about(FACTORY).that(entries); |
| 65 | } |
| 66 | |
| 67 | // Static method for getting the subject factory (for use with assertAbout()) |
Colin Cross | b6cf22d | 2019-04-12 14:34:58 -0700 | [diff] [blame] | 68 | public static Subject.Factory<LayersTraceSubject, LayersTrace> entries() { |
Vishnu Nair | 8248b7c | 2018-08-01 10:13:36 -0700 | [diff] [blame] | 69 | return FACTORY; |
| 70 | } |
| 71 | |
| 72 | public void forAllEntries() { |
| 73 | test(); |
| 74 | } |
| 75 | |
| 76 | public void forRange(long startTime, long endTime) { |
| 77 | mChecker.filterByRange(startTime, endTime); |
| 78 | test(); |
| 79 | } |
| 80 | |
| 81 | public LayersTraceSubject then() { |
| 82 | mChecker.checkChangingAssertions(); |
| 83 | return this; |
| 84 | } |
| 85 | |
| 86 | public void inTheBeginning() { |
| 87 | if (getSubject().getEntries().isEmpty()) { |
| 88 | fail("No entries found."); |
| 89 | } |
| 90 | mChecker.checkFirstEntry(); |
| 91 | test(); |
| 92 | } |
| 93 | |
| 94 | public void atTheEnd() { |
| 95 | if (getSubject().getEntries().isEmpty()) { |
| 96 | fail("No entries found."); |
| 97 | } |
| 98 | mChecker.checkLastEntry(); |
| 99 | test(); |
| 100 | } |
| 101 | |
| 102 | private void test() { |
| 103 | List<Result> failures = mChecker.test(getSubject().getEntries()); |
| 104 | if (!failures.isEmpty()) { |
| 105 | String failureLogs = failures.stream().map(Result::toString) |
| 106 | .collect(Collectors.joining("\n")); |
| 107 | String tracePath = ""; |
| 108 | if (getSubject().getSource().isPresent()) { |
| 109 | tracePath = "\nLayers Trace can be found in: " |
| 110 | + getSubject().getSource().get().toAbsolutePath() + "\n"; |
| 111 | } |
| 112 | fail(tracePath + failureLogs); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | public LayersTraceSubject coversRegion(Rect rect) { |
| 117 | mChecker.add(entry -> entry.coversRegion(rect), |
| 118 | "coversRegion(" + rect + ")"); |
| 119 | return this; |
| 120 | } |
| 121 | |
| 122 | public LayersTraceSubject hasVisibleRegion(String layerName, Rect size) { |
| 123 | mChecker.add(entry -> entry.hasVisibleRegion(layerName, size), |
| 124 | "hasVisibleRegion(" + layerName + size + ")"); |
| 125 | return this; |
| 126 | } |
| 127 | |
| 128 | public LayersTraceSubject showsLayer(String layerName) { |
| 129 | mChecker.add(entry -> entry.isVisible(layerName), |
| 130 | "showsLayer(" + layerName + ")"); |
| 131 | return this; |
| 132 | } |
| 133 | |
| 134 | public LayersTraceSubject hidesLayer(String layerName) { |
| 135 | mChecker.add(entry -> entry.isVisible(layerName).negate(), |
| 136 | "hidesLayer(" + layerName + ")"); |
| 137 | return this; |
| 138 | } |
| 139 | } |