• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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.common.flicker.subject.events
18 
19 import android.tools.common.Timestamps
20 import android.tools.common.flicker.subject.FlickerSubject
21 import android.tools.common.flicker.subject.exceptions.ExceptionMessageBuilder
22 import android.tools.common.flicker.subject.exceptions.IncorrectFocusException
23 import android.tools.common.io.Reader
24 import android.tools.common.traces.events.EventLog
25 import android.tools.common.traces.events.FocusEvent
26 
27 /** Truth subject for [FocusEvent] objects. */
28 class EventLogSubject(val eventLog: EventLog, override val reader: Reader) : FlickerSubject() {
29     override val timestamp = Timestamps.empty()
30 
31     private val _focusChanges by lazy {
32         val focusList = mutableListOf<String>()
33         eventLog.focusEvents.firstOrNull { !it.hasFocus() }?.let { focusList.add(it.window) }
34         focusList + eventLog.focusEvents.filter { it.hasFocus() }.map { it.window }
35     }
36 
37     private val exceptionMessageBuilder
38         get() = ExceptionMessageBuilder().forSubject(this)
39 
40     fun focusChanges(vararg windows: String) = apply {
41         if (windows.isNotEmpty()) {
42             val focusChanges =
43                 _focusChanges.dropWhile { !it.contains(windows.first()) }.take(windows.size)
44 
45             val builder =
46                 exceptionMessageBuilder.setExpected(windows.joinToString()).setActual(focusChanges)
47 
48             if (focusChanges.isEmpty()) {
49                 val errorMsgBuilder = builder.setMessage("Focus did not change")
50                 throw IncorrectFocusException(errorMsgBuilder)
51             }
52 
53             val success =
54                 windows.size <= focusChanges.size &&
55                     focusChanges.zip(windows).all { (focus, search) -> focus.contains(search) }
56 
57             if (!success) {
58                 val errorMsgBuilder = builder.setMessage("Incorrect focus change")
59                 throw IncorrectFocusException(errorMsgBuilder)
60             }
61         }
62     }
63 
64     fun focusDoesNotChange() = apply {
65         if (_focusChanges.isNotEmpty()) {
66             val errorMsgBuilder =
67                 exceptionMessageBuilder
68                     .setMessage("Focus changes")
69                     .setExpected("")
70                     .setActual(_focusChanges)
71             throw IncorrectFocusException(errorMsgBuilder)
72         }
73     }
74 }
75