1/* 2 * Copyright (C) 2017 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 17package com.android.server.wm; 18 19import android.platform.test.annotations.Presubmit; 20import android.support.test.filters.SmallTest; 21import android.support.test.runner.AndroidJUnit4; 22import org.junit.Test; 23import org.junit.runner.RunWith; 24 25import static org.junit.Assert.assertEquals; 26import static org.junit.Assert.assertNotNull; 27import static org.junit.Assert.assertNull; 28import static org.junit.Assert.assertTrue; 29 30/** 31 * Test class for {@link TaskWindowContainerController}. 32 * 33 * Build/Install/Run: 34 * bit FrameworksServicesTests:com.android.server.wm.TaskWindowContainerControllerTests 35 */ 36@SmallTest 37@Presubmit 38@RunWith(AndroidJUnit4.class) 39public class TaskWindowContainerControllerTests extends WindowTestsBase { 40 41 @Test 42 public void testRemoveContainer() throws Exception { 43 final WindowTestUtils.TestTaskWindowContainerController taskController = 44 new WindowTestUtils.TestTaskWindowContainerController(this); 45 final WindowTestUtils.TestAppWindowContainerController appController = 46 new WindowTestUtils.TestAppWindowContainerController(taskController); 47 48 taskController.removeContainer(); 49 // Assert that the container was removed. 50 assertNull(taskController.mContainer); 51 assertNull(appController.mContainer); 52 } 53 54 @Test 55 public void testRemoveContainer_deferRemoval() throws Exception { 56 final WindowTestUtils.TestTaskWindowContainerController taskController = 57 new WindowTestUtils.TestTaskWindowContainerController(this); 58 final WindowTestUtils.TestAppWindowContainerController appController = 59 new WindowTestUtils.TestAppWindowContainerController(taskController); 60 61 final WindowTestUtils.TestTask task = (WindowTestUtils.TestTask) taskController.mContainer; 62 final AppWindowToken app = appController.mContainer; 63 task.mShouldDeferRemoval = true; 64 65 taskController.removeContainer(); 66 // For the case of deferred removal the task controller will no longer be connected to the 67 // container, but the app controller will still be connected to the its container until 68 // the task window container is removed. 69 assertNull(taskController.mContainer); 70 assertNull(task.getController()); 71 assertNotNull(appController.mContainer); 72 assertNotNull(app.getController()); 73 74 task.removeImmediately(); 75 assertNull(appController.mContainer); 76 assertNull(app.getController()); 77 } 78 79 @Test 80 public void testReparent() throws Exception { 81 final StackWindowController stackController1 = 82 createStackControllerOnDisplay(mDisplayContent); 83 final WindowTestUtils.TestTaskWindowContainerController taskController = 84 new WindowTestUtils.TestTaskWindowContainerController(stackController1); 85 final StackWindowController stackController2 = 86 createStackControllerOnDisplay(mDisplayContent); 87 final WindowTestUtils.TestTaskWindowContainerController taskController2 = 88 new WindowTestUtils.TestTaskWindowContainerController(stackController2); 89 90 boolean gotException = false; 91 try { 92 taskController.reparent(stackController1, 0, false/* moveParents */); 93 } catch (IllegalArgumentException e) { 94 gotException = true; 95 } 96 assertTrue("Should not be able to reparent to the same parent", gotException); 97 98 final StackWindowController stackController3 = 99 createStackControllerOnDisplay(mDisplayContent); 100 stackController3.setContainer(null); 101 gotException = false; 102 try { 103 taskController.reparent(stackController3, 0, false/* moveParents */); 104 } catch (IllegalArgumentException e) { 105 gotException = true; 106 } 107 assertTrue("Should not be able to reparent to a stack that doesn't have a container", 108 gotException); 109 110 taskController.reparent(stackController2, 0, false/* moveParents */); 111 assertEquals(stackController2.mContainer, taskController.mContainer.getParent()); 112 assertEquals(0, ((WindowTestUtils.TestTask) taskController.mContainer).positionInParent()); 113 assertEquals(1, ((WindowTestUtils.TestTask) taskController2.mContainer).positionInParent()); 114 } 115 116 @Test 117 public void testReparent_BetweenDisplays() throws Exception { 118 // Create first stack on primary display. 119 final StackWindowController stack1Controller = 120 createStackControllerOnDisplay(mDisplayContent); 121 final TaskStack stack1 = stack1Controller.mContainer; 122 final WindowTestUtils.TestTaskWindowContainerController taskController = 123 new WindowTestUtils.TestTaskWindowContainerController(stack1Controller); 124 final WindowTestUtils.TestTask task1 = (WindowTestUtils.TestTask) taskController.mContainer; 125 task1.mOnDisplayChangedCalled = false; 126 assertEquals(mDisplayContent, stack1.getDisplayContent()); 127 128 // Create second display and put second stack on it. 129 final DisplayContent dc = createNewDisplay(); 130 final StackWindowController stack2Controller = createStackControllerOnDisplay(dc); 131 final TaskStack stack2 = stack2Controller.mContainer; 132 final WindowTestUtils.TestTaskWindowContainerController taskController2 = 133 new WindowTestUtils.TestTaskWindowContainerController(stack2Controller); 134 final WindowTestUtils.TestTask task2 = 135 (WindowTestUtils.TestTask) taskController2.mContainer; 136 137 // Reparent and check state 138 taskController.reparent(stack2Controller, 0, false /* moveParents */); 139 assertEquals(stack2, task1.getParent()); 140 assertEquals(0, task1.positionInParent()); 141 assertEquals(1, task2.positionInParent()); 142 assertTrue(task1.mOnDisplayChangedCalled); 143 } 144} 145