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 | |
| 24 | import com.android.server.wm.flicker.Assertions.Result; |
| 25 | import com.android.server.wm.flicker.TransitionRunner.TransitionResult; |
| 26 | |
Colin Cross | b6cf22d | 2019-04-12 14:34:58 -0700 | [diff] [blame] | 27 | import com.google.common.truth.FailureMetadata; |
Vishnu Nair | 8248b7c | 2018-08-01 10:13:36 -0700 | [diff] [blame] | 28 | import com.google.common.truth.Subject; |
Vishnu Nair | 8248b7c | 2018-08-01 10:13:36 -0700 | [diff] [blame] | 29 | |
| 30 | import java.nio.file.Path; |
| 31 | import java.util.List; |
| 32 | import java.util.Optional; |
| 33 | import java.util.stream.Collectors; |
| 34 | |
| 35 | /** |
| 36 | * Truth subject for {@link WindowManagerTrace} objects. |
| 37 | */ |
| 38 | public class WmTraceSubject extends Subject<WmTraceSubject, WindowManagerTrace> { |
| 39 | // Boiler-plate Subject.Factory for WmTraceSubject |
Colin Cross | b6cf22d | 2019-04-12 14:34:58 -0700 | [diff] [blame] | 40 | private static final Subject.Factory<WmTraceSubject, WindowManagerTrace> FACTORY = |
| 41 | new Subject.Factory<WmTraceSubject, WindowManagerTrace>() { |
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 WmTraceSubject createSubject( |
| 44 | FailureMetadata fm, @Nullable WindowManagerTrace target) { |
| 45 | return new WmTraceSubject(fm, target); |
Vishnu Nair | 8248b7c | 2018-08-01 10:13:36 -0700 | [diff] [blame] | 46 | } |
| 47 | }; |
| 48 | |
| 49 | private AssertionsChecker<WindowManagerTrace.Entry> mChecker = new AssertionsChecker<>(); |
| 50 | |
Colin Cross | b6cf22d | 2019-04-12 14:34:58 -0700 | [diff] [blame] | 51 | private WmTraceSubject(FailureMetadata fm, @Nullable WindowManagerTrace 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 WmTraceSubject assertThat(@Nullable WindowManagerTrace entry) { |
| 57 | return assertAbout(FACTORY).that(entry); |
| 58 | } |
| 59 | |
| 60 | // User-defined entry point |
| 61 | public static WmTraceSubject assertThat(@Nullable TransitionResult result) { |
| 62 | WindowManagerTrace entries = WindowManagerTrace.parseFrom(result.getWindowManagerTrace(), |
| 63 | result.getWindowManagerTracePath()); |
| 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<WmTraceSubject, WindowManagerTrace> 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 WmTraceSubject 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 | Optional<Path> failureTracePath = getSubject().getSource(); |
| 106 | String failureLogs = failures.stream().map(Result::toString) |
| 107 | .collect(Collectors.joining("\n")); |
| 108 | String tracePath = ""; |
| 109 | if (failureTracePath.isPresent()) { |
| 110 | tracePath = "\nWindowManager Trace can be found in: " |
| 111 | + failureTracePath.get().toAbsolutePath() + "\n"; |
| 112 | } |
| 113 | fail(tracePath + failureLogs); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | public WmTraceSubject showsAboveAppWindow(String partialWindowTitle) { |
| 118 | mChecker.add(entry -> entry.isAboveAppWindowVisible(partialWindowTitle), |
| 119 | "showsAboveAppWindow(" + partialWindowTitle + ")"); |
| 120 | return this; |
| 121 | } |
| 122 | |
| 123 | public WmTraceSubject hidesAboveAppWindow(String partialWindowTitle) { |
| 124 | mChecker.add(entry -> entry.isAboveAppWindowVisible(partialWindowTitle).negate(), |
| 125 | "hidesAboveAppWindow" + "(" + partialWindowTitle + ")"); |
| 126 | return this; |
| 127 | } |
| 128 | |
| 129 | public WmTraceSubject showsBelowAppWindow(String partialWindowTitle) { |
| 130 | mChecker.add(entry -> entry.isBelowAppWindowVisible(partialWindowTitle), |
| 131 | "showsBelowAppWindow(" + partialWindowTitle + ")"); |
| 132 | return this; |
| 133 | } |
| 134 | |
| 135 | public WmTraceSubject hidesBelowAppWindow(String partialWindowTitle) { |
| 136 | mChecker.add(entry -> entry.isBelowAppWindowVisible(partialWindowTitle).negate(), |
| 137 | "hidesBelowAppWindow" + "(" + partialWindowTitle + ")"); |
| 138 | return this; |
| 139 | } |
| 140 | |
| 141 | public WmTraceSubject showsImeWindow(String partialWindowTitle) { |
| 142 | mChecker.add(entry -> entry.isImeWindowVisible(partialWindowTitle), |
| 143 | "showsBelowAppWindow(" + partialWindowTitle + ")"); |
| 144 | return this; |
| 145 | } |
| 146 | |
| 147 | public WmTraceSubject hidesImeWindow(String partialWindowTitle) { |
| 148 | mChecker.add(entry -> entry.isImeWindowVisible(partialWindowTitle).negate(), |
| 149 | "hidesImeWindow" + "(" + partialWindowTitle + ")"); |
| 150 | return this; |
| 151 | } |
| 152 | |
| 153 | public WmTraceSubject showsAppWindowOnTop(String partialWindowTitle) { |
| 154 | mChecker.add( |
| 155 | entry -> { |
| 156 | Result result = entry.isAppWindowVisible(partialWindowTitle); |
| 157 | if (result.passed()) { |
| 158 | result = entry.isVisibleAppWindowOnTop(partialWindowTitle); |
| 159 | } |
| 160 | return result; |
| 161 | }, |
| 162 | "showsAppWindowOnTop(" + partialWindowTitle + ")" |
| 163 | ); |
| 164 | return this; |
| 165 | } |
| 166 | |
| 167 | public WmTraceSubject hidesAppWindowOnTop(String partialWindowTitle) { |
| 168 | mChecker.add( |
| 169 | entry -> { |
| 170 | Result result = entry.isAppWindowVisible(partialWindowTitle).negate(); |
| 171 | if (result.failed()) { |
| 172 | result = entry.isVisibleAppWindowOnTop(partialWindowTitle).negate(); |
| 173 | } |
| 174 | return result; |
| 175 | }, |
| 176 | "hidesAppWindowOnTop(" + partialWindowTitle + ")" |
| 177 | ); |
| 178 | return this; |
| 179 | } |
| 180 | |
| 181 | public WmTraceSubject showsAppWindow(String partialWindowTitle) { |
| 182 | mChecker.add(entry -> entry.isAppWindowVisible(partialWindowTitle), |
| 183 | "showsAppWindow(" + partialWindowTitle + ")"); |
| 184 | return this; |
| 185 | } |
| 186 | |
| 187 | public WmTraceSubject hidesAppWindow(String partialWindowTitle) { |
| 188 | mChecker.add(entry -> entry.isAppWindowVisible(partialWindowTitle).negate(), |
| 189 | "hidesAppWindow(" + partialWindowTitle + ")"); |
| 190 | return this; |
| 191 | } |
| 192 | } |