1 /*
2  * Copyright (C) 2023 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 android.tools.flicker.subject.events
18 
19 import android.tools.Timestamps
20 import android.tools.flicker.assertions.SubjectsParser
21 import android.tools.testutils.CleanFlickerEnvironmentRule
22 import android.tools.testutils.ParsedTracesReader
23 import android.tools.testutils.TestArtifact
24 import android.tools.traces.events.EventLog
25 import android.tools.traces.events.FocusEvent
26 import org.junit.ClassRule
27 import org.junit.Test
28 
29 /**
30  * Contains [EventLogSubject] tests. To run this test: `atest FlickerLibTest:EventLogSubjectTest`
31  */
32 class EventLogSubjectTest {
33     @Test
canDetectFocusChangesnull34     fun canDetectFocusChanges() {
35         val reader =
36             ParsedTracesReader(
37                 artifact = TestArtifact.EMPTY,
38                 eventLog =
39                     EventLog(
40                         listOf(
41                             FocusEvent(
42                                 Timestamps.from(unixNanos = 0),
43                                 "WinB",
44                                 FocusEvent.Type.GAINED,
45                                 "test",
46                                 0,
47                                 "0",
48                                 0,
49                             ),
50                             FocusEvent(
51                                 Timestamps.from(unixNanos = 0),
52                                 "test WinA window",
53                                 FocusEvent.Type.LOST,
54                                 "test",
55                                 0,
56                                 "0",
57                                 0,
58                             ),
59                             FocusEvent(
60                                 Timestamps.from(unixNanos = 0),
61                                 "WinB",
62                                 FocusEvent.Type.LOST,
63                                 "test",
64                                 0,
65                                 "0",
66                                 0,
67                             ),
68                             FocusEvent(
69                                 Timestamps.from(unixNanos = 0),
70                                 "test WinC",
71                                 FocusEvent.Type.GAINED,
72                                 "test",
73                                 0,
74                                 "0",
75                                 0,
76                             ),
77                         )
78                     ),
79             )
80         val subjectsParser = SubjectsParser(reader)
81 
82         val subject = subjectsParser.eventLogSubject ?: error("Event log subject not built")
83         subject.focusChanges("WinA", "WinB", "WinC")
84         subject.focusChanges("WinA", "WinB")
85         subject.focusChanges("WinB", "WinC")
86         subject.focusChanges("WinA")
87         subject.focusChanges("WinB")
88         subject.focusChanges("WinC")
89     }
90 
91     @Test
canDetectFocusDoesNotChangenull92     fun canDetectFocusDoesNotChange() {
93         val reader =
94             ParsedTracesReader(artifact = TestArtifact.EMPTY, eventLog = EventLog(emptyList()))
95         val subjectsParser = SubjectsParser(reader)
96 
97         val subject = subjectsParser.eventLogSubject ?: error("Event log subject not built")
98         subject.focusDoesNotChange()
99     }
100 
101     companion object {
102         @ClassRule @JvmField val ENV_CLEANUP = CleanFlickerEnvironmentRule()
103     }
104 }
105