• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2022 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.app.motiontool
18 
19 import android.content.Intent
20 import android.testing.AndroidTestingRunner
21 import android.view.Choreographer
22 import android.view.View
23 import android.view.WindowManagerGlobal
24 import androidx.test.ext.junit.rules.ActivityScenarioRule
25 import androidx.test.filters.SmallTest
26 import androidx.test.platform.app.InstrumentationRegistry
27 import com.android.app.motiontool.util.TestActivity
28 import junit.framework.Assert.assertEquals
29 import junit.framework.Assert.assertTrue
30 import org.junit.Rule
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 
34 @SmallTest
35 @RunWith(AndroidTestingRunner::class)
36 class MotionToolManagerTest {
37 
38     private val windowManagerGlobal = WindowManagerGlobal.getInstance()
39     private val motionToolManager = MotionToolManager.getInstance(windowManagerGlobal)
40 
41     private val activityIntent =
42         Intent(InstrumentationRegistry.getInstrumentation().context, TestActivity::class.java)
43 
44     @get:Rule
45     val activityScenarioRule = ActivityScenarioRule<TestActivity>(activityIntent)
46 
47     @Test(expected = UnknownTraceIdException::class)
48     fun testEndTraceThrowsWithoutPrecedingBeginTrace() {
49         motionToolManager.endTrace(0)
50     }
51 
52     @Test(expected = UnknownTraceIdException::class)
53     fun testPollTraceThrowsWithoutPrecedingBeginTrace() {
54         motionToolManager.pollTrace(0)
55     }
56 
57     @Test(expected = UnknownTraceIdException::class)
58     fun testEndTraceThrowsWithInvalidTraceId() {
59         val traceId = motionToolManager.beginTrace(getActivityViewRootId())
60         motionToolManager.endTrace(traceId + 1)
61     }
62 
63     @Test(expected = UnknownTraceIdException::class)
64     fun testPollTraceThrowsWithInvalidTraceId() {
65         val traceId = motionToolManager.beginTrace(getActivityViewRootId())
66         motionToolManager.pollTrace(traceId + 1)
67     }
68 
69     @Test(expected = WindowNotFoundException::class)
70     fun testBeginTraceThrowsWithInvalidWindowId() {
71         motionToolManager.beginTrace("InvalidWindowId")
72     }
73 
74     @Test
75     fun testNoOnDrawCallReturnsEmptyResponse() {
76         activityScenarioRule.scenario.onActivity {
77             val traceId = motionToolManager.beginTrace(getActivityViewRootId())
78             val result = motionToolManager.endTrace(traceId)
79             assertTrue(result.frameDataList.isEmpty())
80         }
81     }
82 
83     @Test
84     fun testOneOnDrawCallReturnsOneFrameResponse() {
85         activityScenarioRule.scenario.onActivity { activity ->
86             val traceId = motionToolManager.beginTrace(getActivityViewRootId())
87             Choreographer.getInstance().postFrameCallback {
88                 activity.findViewById<View>(android.R.id.content).viewTreeObserver.dispatchOnDraw()
89 
90                 val polledData = motionToolManager.pollTrace(traceId)
91                 assertEquals(1, polledData.frameDataList.size)
92 
93                 // Verify that frameData is only included once and is not returned again
94                 val endData = motionToolManager.endTrace(traceId)
95                 assertEquals(0, endData.frameDataList.size)
96             }
97         }
98     }
99 
100     private fun getActivityViewRootId(): String {
101         var activityViewRootId = ""
102         activityScenarioRule.scenario.onActivity {
103             activityViewRootId = WindowManagerGlobal.getInstance().viewRootNames.first()
104         }
105         return activityViewRootId
106     }
107 }
108