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 com.android.systemui.screenshot
18 
19 import android.content.ComponentName
20 import android.graphics.Insets
21 import android.graphics.Rect
22 import android.os.UserHandle
23 import android.view.WindowManager
24 import com.android.internal.util.ScreenshotRequest
25 import com.google.common.truth.Truth.assertThat
26 import org.junit.Test
27 
28 class ScreenshotDataTest {
29     private val type = WindowManager.TAKE_SCREENSHOT_FULLSCREEN
30     private val source = WindowManager.ScreenshotSource.SCREENSHOT_KEY_OTHER
31     private val bounds = Rect(1, 2, 3, 4)
32     private val taskId = 123
33     private val userId = 1
34     private val insets = Insets.of(1, 2, 3, 4)
35     private val component = ComponentName("android.test", "android.test.Component")
36 
37     @Test
38     fun testConstruction() {
39         val request =
40             ScreenshotRequest.Builder(type, source)
41                 .setBoundsOnScreen(bounds)
42                 .setInsets(insets)
43                 .setTaskId(taskId)
44                 .setUserId(userId)
45                 .setTopComponent(component)
46                 .build()
47 
48         val data = ScreenshotData.fromRequest(request)
49 
50         assertThat(data.source).isEqualTo(source)
51         assertThat(data.type).isEqualTo(type)
52         assertThat(data.screenBounds).isEqualTo(bounds)
53         assertThat(data.insets).isEqualTo(insets)
54         assertThat(data.taskId).isEqualTo(taskId)
55         assertThat(data.userHandle).isEqualTo(UserHandle.of(userId))
56         assertThat(data.topComponent).isEqualTo(component)
57     }
58 
59     @Test
60     fun testNegativeUserId() {
61         val request = ScreenshotRequest.Builder(type, source).setUserId(-1).build()
62 
63         val data = ScreenshotData.fromRequest(request)
64 
65         assertThat(data.userHandle).isNull()
66     }
67 
68     @Test
69     fun testPackageNameAsString() {
70         val request = ScreenshotRequest.Builder(type, source).setTopComponent(component).build()
71 
72         val data = ScreenshotData.fromRequest(request)
73 
74         assertThat(data.packageNameString).isEqualTo("android.test")
75     }
76 
77     @Test
78     fun testPackageNameAsString_null() {
79         val request = ScreenshotRequest.Builder(type, source).build()
80 
81         val data = ScreenshotData.fromRequest(request)
82 
83         assertThat(data.packageNameString).isEqualTo("")
84     }
85 }
86