blob: 4085810a213d56368fd0098a96b10214019e3363 [file] [log] [blame]
Vishnu Nair8248b7c2018-08-01 10:13:36 -07001/*
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
17package com.android.server.wm.flicker;
18
19import static com.google.common.truth.Truth.assertAbout;
20import static com.google.common.truth.Truth.assertWithMessage;
21
22import android.annotation.Nullable;
23import android.graphics.Rect;
24
25import com.android.server.wm.flicker.Assertions.Result;
26import com.android.server.wm.flicker.LayersTrace.Entry;
27import com.android.server.wm.flicker.TransitionRunner.TransitionResult;
28
Colin Crossb6cf22d2019-04-12 14:34:58 -070029import com.google.common.truth.FailureMetadata;
Vishnu Nair8248b7c2018-08-01 10:13:36 -070030import com.google.common.truth.Subject;
Vishnu Nair8248b7c2018-08-01 10:13:36 -070031
32import java.util.List;
33import java.util.stream.Collectors;
34
35/**
36 * Truth subject for {@link LayersTrace} objects.
37 */
38public class LayersTraceSubject extends Subject<LayersTraceSubject, LayersTrace> {
39 // Boiler-plate Subject.Factory for LayersTraceSubject
Colin Crossb6cf22d2019-04-12 14:34:58 -070040 private static final Subject.Factory<LayersTraceSubject, LayersTrace> FACTORY =
41 new Subject.Factory<LayersTraceSubject, LayersTrace>() {
Vishnu Nair8248b7c2018-08-01 10:13:36 -070042 @Override
Colin Crossb6cf22d2019-04-12 14:34:58 -070043 public LayersTraceSubject createSubject(
44 FailureMetadata fm, @Nullable LayersTrace target) {
45 return new LayersTraceSubject(fm, target);
Vishnu Nair8248b7c2018-08-01 10:13:36 -070046 }
47 };
48
49 private AssertionsChecker<Entry> mChecker = new AssertionsChecker<>();
50
Colin Crossb6cf22d2019-04-12 14:34:58 -070051 private LayersTraceSubject(FailureMetadata fm, @Nullable LayersTrace subject) {
52 super(fm, subject);
Vishnu Nair8248b7c2018-08-01 10:13:36 -070053 }
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 Crossb6cf22d2019-04-12 14:34:58 -070068 public static Subject.Factory<LayersTraceSubject, LayersTrace> entries() {
Vishnu Nair8248b7c2018-08-01 10:13:36 -070069 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}