1 /*
2  * 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 android.view;
18 
19 import static android.view.ContentRecordingSession.RECORD_CONTENT_DISPLAY;
20 import static android.view.ContentRecordingSession.RECORD_CONTENT_TASK;
21 import static android.view.ContentRecordingSession.TASK_ID_UNKNOWN;
22 import static android.view.Display.DEFAULT_DISPLAY;
23 import static android.view.Display.INVALID_DISPLAY;
24 
25 import static com.google.common.truth.Truth.assertThat;
26 
27 import android.os.Binder;
28 import android.os.IBinder;
29 import android.os.Parcel;
30 import android.platform.test.annotations.Presubmit;
31 
32 import androidx.test.ext.junit.runners.AndroidJUnit4;
33 import androidx.test.filters.SmallTest;
34 
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 
38 /**
39  * Tests for {@link ContentRecordingSession} class.
40  *
41  * Build/Install/Run:
42  * atest FrameworksCoreTests:ContentRecordingSessionTest
43  */
44 @RunWith(AndroidJUnit4.class)
45 @SmallTest
46 @Presubmit
47 public class ContentRecordingSessionTest {
48     private static final int DISPLAY_ID = 1;
49     private static final int TASK_ID = 123;
50     private static final IBinder WINDOW_TOKEN = new Binder("DisplayContentWindowToken");
51 
52     @Test
testParcelable()53     public void testParcelable() {
54         ContentRecordingSession session = ContentRecordingSession.createTaskSession(WINDOW_TOKEN);
55         session.setVirtualDisplayId(DISPLAY_ID);
56 
57         Parcel parcel = Parcel.obtain();
58         session.writeToParcel(parcel, 0 /* flags */);
59         parcel.setDataPosition(0);
60         ContentRecordingSession session2 = ContentRecordingSession.CREATOR.createFromParcel(parcel);
61         assertThat(session).isEqualTo(session2);
62         parcel.recycle();
63     }
64 
65     @Test
testTaskConstructor()66     public void testTaskConstructor() {
67         ContentRecordingSession session = ContentRecordingSession.createTaskSession(WINDOW_TOKEN);
68         assertThat(session.getContentToRecord()).isEqualTo(RECORD_CONTENT_TASK);
69         assertThat(session.getTokenToRecord()).isEqualTo(WINDOW_TOKEN);
70         assertThat(session.getTaskId()).isEqualTo(TASK_ID_UNKNOWN);
71     }
72 
73     @Test
testSecondaryTaskConstructor()74     public void testSecondaryTaskConstructor() {
75         ContentRecordingSession session =
76                 ContentRecordingSession.createTaskSession(WINDOW_TOKEN, TASK_ID);
77         assertThat(session.getContentToRecord()).isEqualTo(RECORD_CONTENT_TASK);
78         assertThat(session.getTokenToRecord()).isEqualTo(WINDOW_TOKEN);
79         assertThat(session.getTaskId()).isEqualTo(TASK_ID);
80     }
81 
82     @Test
testDisplayConstructor()83     public void testDisplayConstructor() {
84         ContentRecordingSession session = ContentRecordingSession.createDisplaySession(
85                 DEFAULT_DISPLAY);
86         assertThat(session.getContentToRecord()).isEqualTo(RECORD_CONTENT_DISPLAY);
87         assertThat(session.getTokenToRecord()).isNull();
88         assertThat(session.getTaskId()).isEqualTo(TASK_ID_UNKNOWN);
89     }
90 
91     @Test
testIsValid_displaySession()92     public void testIsValid_displaySession() {
93         // Canonical display session.
94         ContentRecordingSession displaySession = ContentRecordingSession.createDisplaySession(
95                 DEFAULT_DISPLAY);
96         displaySession.setVirtualDisplayId(DEFAULT_DISPLAY);
97         assertThat(ContentRecordingSession.isValid(displaySession)).isTrue();
98 
99         // Virtual display id values.
100         ContentRecordingSession displaySession0 = ContentRecordingSession.createDisplaySession(
101                 DEFAULT_DISPLAY);
102         assertThat(ContentRecordingSession.isValid(displaySession0)).isFalse();
103 
104         ContentRecordingSession displaySession1 = ContentRecordingSession.createDisplaySession(
105                 DEFAULT_DISPLAY);
106         displaySession1.setVirtualDisplayId(INVALID_DISPLAY);
107         assertThat(ContentRecordingSession.isValid(displaySession1)).isFalse();
108 
109         // Display id values.
110         ContentRecordingSession displaySession2 = ContentRecordingSession.createDisplaySession(
111                 INVALID_DISPLAY);
112         displaySession2.setVirtualDisplayId(DEFAULT_DISPLAY);
113         assertThat(ContentRecordingSession.isValid(displaySession2)).isFalse();
114 
115         displaySession2.setDisplayToRecord(DEFAULT_DISPLAY);
116         assertThat(ContentRecordingSession.isValid(displaySession2)).isTrue();
117     }
118 
119     @Test
testIsValid_taskSession()120     public void testIsValid_taskSession() {
121         // Canonical task session.
122         ContentRecordingSession taskSession = ContentRecordingSession.createTaskSession(
123                 WINDOW_TOKEN);
124         taskSession.setVirtualDisplayId(DEFAULT_DISPLAY);
125         assertThat(ContentRecordingSession.isValid(taskSession)).isTrue();
126 
127         // Virtual display id values.
128         ContentRecordingSession taskSession0 = ContentRecordingSession.createTaskSession(
129                 WINDOW_TOKEN);
130         assertThat(ContentRecordingSession.isValid(taskSession0)).isFalse();
131 
132         ContentRecordingSession taskSession1 = ContentRecordingSession.createTaskSession(
133                 WINDOW_TOKEN);
134         taskSession1.setVirtualDisplayId(INVALID_DISPLAY);
135         assertThat(ContentRecordingSession.isValid(taskSession1)).isFalse();
136 
137         // Window container values.
138         ContentRecordingSession taskSession3 = ContentRecordingSession.createTaskSession(null);
139         taskSession3.setVirtualDisplayId(DEFAULT_DISPLAY);
140         assertThat(ContentRecordingSession.isValid(taskSession3)).isFalse();
141 
142         ContentRecordingSession taskSession4 = ContentRecordingSession.createTaskSession(
143                 WINDOW_TOKEN);
144         taskSession4.setVirtualDisplayId(DEFAULT_DISPLAY);
145         taskSession4.setTokenToRecord(null);
146         assertThat(ContentRecordingSession.isValid(taskSession4)).isFalse();
147     }
148 
149     @Test
testIsProjectionOnSameDisplay()150     public void testIsProjectionOnSameDisplay() {
151         assertThat(ContentRecordingSession.isProjectionOnSameDisplay(null, null)).isFalse();
152         ContentRecordingSession session = ContentRecordingSession.createDisplaySession(
153                 DEFAULT_DISPLAY);
154         session.setVirtualDisplayId(DEFAULT_DISPLAY);
155         assertThat(ContentRecordingSession.isProjectionOnSameDisplay(session, null)).isFalse();
156 
157         ContentRecordingSession incomingSession = ContentRecordingSession.createDisplaySession(
158                 DEFAULT_DISPLAY);
159         incomingSession.setVirtualDisplayId(DEFAULT_DISPLAY);
160         assertThat(ContentRecordingSession.isProjectionOnSameDisplay(session,
161                 incomingSession)).isTrue();
162 
163         incomingSession.setVirtualDisplayId(DEFAULT_DISPLAY + 1);
164         assertThat(ContentRecordingSession.isProjectionOnSameDisplay(session,
165                 incomingSession)).isFalse();
166     }
167 
168     @Test
testEquals()169     public void testEquals() {
170         ContentRecordingSession session = ContentRecordingSession.createTaskSession(WINDOW_TOKEN);
171         session.setVirtualDisplayId(DISPLAY_ID);
172 
173         ContentRecordingSession session2 = ContentRecordingSession.createTaskSession(WINDOW_TOKEN);
174         session2.setVirtualDisplayId(DISPLAY_ID);
175         assertThat(session).isEqualTo(session2);
176     }
177 }
178