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 | |
| 27 | import com.google.common.truth.FailureStrategy; |
| 28 | import com.google.common.truth.Subject; |
| 29 | import com.google.common.truth.SubjectFactory; |
| 30 | |
| 31 | import java.nio.file.Path; |
| 32 | import java.util.List; |
| 33 | import java.util.Optional; |
| 34 | import java.util.stream.Collectors; |
| 35 | |
| 36 | /** |
| 37 | * Truth subject for {@link WindowManagerTrace} objects. |
| 38 | */ |
| 39 | public class WmTraceSubject extends Subject<WmTraceSubject, WindowManagerTrace> { |
| 40 | // Boiler-plate Subject.Factory for WmTraceSubject |
| 41 | private static final SubjectFactory<WmTraceSubject, WindowManagerTrace> FACTORY = |
| 42 | new SubjectFactory<WmTraceSubject, WindowManagerTrace>() { |
| 43 | @Override |
| 44 | public WmTraceSubject getSubject( |
| 45 | FailureStrategy fs, @Nullable WindowManagerTrace target) { |
| 46 | return new WmTraceSubject(fs, target); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | private AssertionsChecker<WindowManagerTrace.Entry> mChecker = new AssertionsChecker<>(); |
| 51 | |
| 52 | private WmTraceSubject(FailureStrategy fs, @Nullable WindowManagerTrace subject) { |
| 53 | super(fs, subject); |
| 54 | } |
| 55 | |
| 56 | // User-defined entry point |
| 57 | public static WmTraceSubject assertThat(@Nullable WindowManagerTrace entry) { |
| 58 | return assertAbout(FACTORY).that(entry); |
| 59 | } |
| 60 | |
| 61 | // User-defined entry point |
| 62 | public static WmTraceSubject assertThat(@Nullable TransitionResult result) { |
| 63 | WindowManagerTrace entries = WindowManagerTrace.parseFrom(result.getWindowManagerTrace(), |
| 64 | result.getWindowManagerTracePath()); |
| 65 | return assertWithMessage(result.toString()).about(FACTORY).that(entries); |
| 66 | } |
| 67 | |
| 68 | // Static method for getting the subject factory (for use with assertAbout()) |
| 69 | public static SubjectFactory<WmTraceSubject, WindowManagerTrace> entries() { |
| 70 | return FACTORY; |
| 71 | } |
| 72 | |
| 73 | public void forAllEntries() { |
| 74 | test(); |
| 75 | } |
| 76 | |
| 77 | public void forRange(long startTime, long endTime) { |
| 78 | mChecker.filterByRange(startTime, endTime); |
| 79 | test(); |
| 80 | } |
| 81 | |
| 82 | public WmTraceSubject then() { |
| 83 | mChecker.checkChangingAssertions(); |
| 84 | return this; |
| 85 | } |
| 86 | |
| 87 | public void inTheBeginning() { |
| 88 | if (getSubject().getEntries().isEmpty()) { |
| 89 | fail("No entries found."); |
| 90 | } |
| 91 | mChecker.checkFirstEntry(); |
| 92 | test(); |
| 93 | } |
| 94 | |
| 95 | public void atTheEnd() { |
| 96 | if (getSubject().getEntries().isEmpty()) { |
| 97 | fail("No entries found."); |
| 98 | } |
| 99 | mChecker.checkLastEntry(); |
| 100 | test(); |
| 101 | } |
| 102 | |
| 103 | private void test() { |
| 104 | List<Result> failures = mChecker.test(getSubject().getEntries()); |
| 105 | if (!failures.isEmpty()) { |
| 106 | Optional<Path> failureTracePath = getSubject().getSource(); |
| 107 | String failureLogs = failures.stream().map(Result::toString) |
| 108 | .collect(Collectors.joining("\n")); |
| 109 | String tracePath = ""; |
| 110 | if (failureTracePath.isPresent()) { |
| 111 | tracePath = "\nWindowManager Trace can be found in: " |
| 112 | + failureTracePath.get().toAbsolutePath() + "\n"; |
| 113 | } |
| 114 | fail(tracePath + failureLogs); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public WmTraceSubject showsAboveAppWindow(String partialWindowTitle) { |
| 119 | mChecker.add(entry -> entry.isAboveAppWindowVisible(partialWindowTitle), |
| 120 | "showsAboveAppWindow(" + partialWindowTitle + ")"); |
| 121 | return this; |
| 122 | } |
| 123 | |
| 124 | public WmTraceSubject hidesAboveAppWindow(String partialWindowTitle) { |
| 125 | mChecker.add(entry -> entry.isAboveAppWindowVisible(partialWindowTitle).negate(), |
| 126 | "hidesAboveAppWindow" + "(" + partialWindowTitle + ")"); |
| 127 | return this; |
| 128 | } |
| 129 | |
| 130 | public WmTraceSubject showsBelowAppWindow(String partialWindowTitle) { |
| 131 | mChecker.add(entry -> entry.isBelowAppWindowVisible(partialWindowTitle), |
| 132 | "showsBelowAppWindow(" + partialWindowTitle + ")"); |
| 133 | return this; |
| 134 | } |
| 135 | |
| 136 | public WmTraceSubject hidesBelowAppWindow(String partialWindowTitle) { |
| 137 | mChecker.add(entry -> entry.isBelowAppWindowVisible(partialWindowTitle).negate(), |
| 138 | "hidesBelowAppWindow" + "(" + partialWindowTitle + ")"); |
| 139 | return this; |
| 140 | } |
| 141 | |
| 142 | public WmTraceSubject showsImeWindow(String partialWindowTitle) { |
| 143 | mChecker.add(entry -> entry.isImeWindowVisible(partialWindowTitle), |
| 144 | "showsBelowAppWindow(" + partialWindowTitle + ")"); |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | public WmTraceSubject hidesImeWindow(String partialWindowTitle) { |
| 149 | mChecker.add(entry -> entry.isImeWindowVisible(partialWindowTitle).negate(), |
| 150 | "hidesImeWindow" + "(" + partialWindowTitle + ")"); |
| 151 | return this; |
| 152 | } |
| 153 | |
| 154 | public WmTraceSubject showsAppWindowOnTop(String partialWindowTitle) { |
| 155 | mChecker.add( |
| 156 | entry -> { |
| 157 | Result result = entry.isAppWindowVisible(partialWindowTitle); |
| 158 | if (result.passed()) { |
| 159 | result = entry.isVisibleAppWindowOnTop(partialWindowTitle); |
| 160 | } |
| 161 | return result; |
| 162 | }, |
| 163 | "showsAppWindowOnTop(" + partialWindowTitle + ")" |
| 164 | ); |
| 165 | return this; |
| 166 | } |
| 167 | |
| 168 | public WmTraceSubject hidesAppWindowOnTop(String partialWindowTitle) { |
| 169 | mChecker.add( |
| 170 | entry -> { |
| 171 | Result result = entry.isAppWindowVisible(partialWindowTitle).negate(); |
| 172 | if (result.failed()) { |
| 173 | result = entry.isVisibleAppWindowOnTop(partialWindowTitle).negate(); |
| 174 | } |
| 175 | return result; |
| 176 | }, |
| 177 | "hidesAppWindowOnTop(" + partialWindowTitle + ")" |
| 178 | ); |
| 179 | return this; |
| 180 | } |
| 181 | |
| 182 | public WmTraceSubject showsAppWindow(String partialWindowTitle) { |
| 183 | mChecker.add(entry -> entry.isAppWindowVisible(partialWindowTitle), |
| 184 | "showsAppWindow(" + partialWindowTitle + ")"); |
| 185 | return this; |
| 186 | } |
| 187 | |
| 188 | public WmTraceSubject hidesAppWindow(String partialWindowTitle) { |
| 189 | mChecker.add(entry -> entry.isAppWindowVisible(partialWindowTitle).negate(), |
| 190 | "hidesAppWindow(" + partialWindowTitle + ")"); |
| 191 | return this; |
| 192 | } |
| 193 | } |