1 /*
2  * Copyright 2021 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 androidx.compose.ui.test.util
18 
19 import android.view.InputEvent
20 import android.view.KeyEvent
21 import android.view.MotionEvent
22 
23 internal class InputEventRecorder {
24 
25     private val _events = mutableListOf<InputEvent>()
26     val events
27         get() = _events as List<InputEvent>
28 
disposeEventsnull29     fun disposeEvents() {
30         _events.removeAll {
31             if (it is MotionEvent) it.recycle()
32             true
33         }
34     }
35 
36     /** [InputEvent] recorder which can record events of type [MotionEvent] and [KeyEvent]. */
recordEventnull37     fun recordEvent(event: InputEvent) {
38         when (event) {
39             is KeyEvent -> _events.add(KeyEvent(event))
40             is MotionEvent -> _events.add(MotionEvent.obtain(event))
41             else ->
42                 IllegalArgumentException(
43                     "Given InputEvent must be a MotionEvent or KeyEvent" +
44                         " not ${event::class.simpleName}"
45                 )
46         }
47     }
48 }
49