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 com.android.server.wm;
18 
19 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
20 
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.atLeast;
26 
27 import android.content.Intent;
28 import android.platform.test.annotations.Presubmit;
29 import android.window.WindowContainerToken;
30 import android.window.WindowContainerTransaction;
31 
32 import androidx.annotation.NonNull;
33 import androidx.test.filters.SmallTest;
34 
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 
38 /**
39  * Test class for {@link WindowContainerTransaction}.
40  *
41  * Build/Install/Run:
42  *  atest WmTests:WindowContainerTransactionTests
43  */
44 @SmallTest
45 @Presubmit
46 @RunWith(WindowTestRunner.class)
47 public class WindowContainerTransactionTests extends WindowTestsBase {
48 
49     @Test
testRemoveTask()50     public void testRemoveTask() {
51         final Task rootTask = createTask(mDisplayContent);
52         final Task task = createTaskInRootTask(rootTask, 0 /* userId */);
53         final ActivityRecord activity = createActivityRecord(mDisplayContent, task);
54 
55         WindowContainerTransaction wct = new WindowContainerTransaction();
56         WindowContainerToken token = task.getTaskInfo().token;
57         wct.removeTask(token);
58         applyTransaction(wct);
59 
60         // There is still an activity to be destroyed, so the task is not removed immediately.
61         assertNotNull(task.getParent());
62         assertTrue(rootTask.hasChild());
63         assertTrue(task.hasChild());
64         assertTrue(activity.finishing);
65 
66         activity.destroyed("testRemoveContainer");
67         // Assert that the container was removed after the activity is destroyed.
68         assertNull(task.getParent());
69         assertEquals(0, task.getChildCount());
70         assertNull(activity.getParent());
71         verify(mAtm.getLockTaskController(), atLeast(1)).clearLockedTask(task);
72         verify(mAtm.getLockTaskController(), atLeast(1)).clearLockedTask(rootTask);
73     }
74 
createTask(int taskId)75     private Task createTask(int taskId) {
76         return new Task.Builder(mAtm)
77                 .setTaskId(taskId)
78                 .setIntent(new Intent())
79                 .setRealActivity(ActivityBuilder.getDefaultComponent())
80                 .setEffectiveUid(10050)
81                 .buildInner();
82     }
83 
applyTransaction(@onNull WindowContainerTransaction t)84     private void applyTransaction(@NonNull WindowContainerTransaction t) {
85         if (!t.isEmpty()) {
86             mWm.mAtmService.mWindowOrganizerController.applyTransaction(t);
87         }
88     }
89 }
90