Home | History | Annotate | Download | only in am
      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 
     17 package com.android.server.am;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertNull;
     21 
     22 import android.content.ComponentName;
     23 import android.platform.test.annotations.Presubmit;
     24 import android.support.test.filters.MediumTest;
     25 import android.support.test.runner.AndroidJUnit4;
     26 
     27 import org.junit.runner.RunWith;
     28 import org.junit.Test;
     29 
     30 /**
     31  * Tests for the {@link ActivityRecord} class.
     32  *
     33  * Build/Install/Run:
     34  *  bit FrameworksServicesTests:com.android.server.am.ActivityRecordTests
     35  */
     36 @MediumTest
     37 @Presubmit
     38 @RunWith(AndroidJUnit4.class)
     39 public class ActivityRecordTests extends ActivityTestsBase {
     40     private static final int TEST_STACK_ID = 100;
     41 
     42     private final ComponentName testActivityComponent =
     43             ComponentName.unflattenFromString("com.foo/.BarActivity");
     44     @Test
     45     public void testStackCleanupOnClearingTask() throws Exception {
     46         final ActivityManagerService service = createActivityManagerService();
     47         final TaskRecord task = createTask(service, testActivityComponent, TEST_STACK_ID);
     48         final ActivityRecord record = createActivity(service, testActivityComponent, task);
     49 
     50         record.setTask(null);
     51         assertEquals(getActivityRemovedFromStackCount(service, TEST_STACK_ID), 1);
     52     }
     53 
     54     @Test
     55     public void testStackCleanupOnActivityRemoval() throws Exception {
     56         final ActivityManagerService service = createActivityManagerService();
     57         final TaskRecord task = createTask(service, testActivityComponent, TEST_STACK_ID);
     58         final ActivityRecord record = createActivity(service, testActivityComponent, task);
     59 
     60         task.removeActivity(record);
     61         assertEquals(getActivityRemovedFromStackCount(service, TEST_STACK_ID),  1);
     62     }
     63 
     64     @Test
     65     public void testStackCleanupOnTaskRemoval() throws Exception {
     66         final ActivityManagerService service = createActivityManagerService();
     67         final TaskRecord task = createTask(service, testActivityComponent, TEST_STACK_ID);
     68         final ActivityRecord record = createActivity(service, testActivityComponent, task);
     69 
     70         service.mStackSupervisor.getStack(TEST_STACK_ID)
     71                 .removeTask(task, null /*reason*/, ActivityStack.REMOVE_TASK_MODE_MOVING);
     72 
     73         // Stack should be gone on task removal.
     74         assertNull(service.mStackSupervisor.getStack(TEST_STACK_ID));
     75     }
     76 
     77     @Test
     78     public void testNoCleanupMovingActivityInSameStack() throws Exception {
     79         final ActivityManagerService service = createActivityManagerService();
     80         final TaskRecord oldTask = createTask(service, testActivityComponent, TEST_STACK_ID);
     81         final ActivityRecord record = createActivity(service, testActivityComponent, oldTask);
     82         final TaskRecord newTask = createTask(service, testActivityComponent, TEST_STACK_ID);
     83 
     84         record.reparent(newTask, 0, null /*reason*/);
     85         assertEquals(getActivityRemovedFromStackCount(service, TEST_STACK_ID), 0);
     86     }
     87 
     88     private static int getActivityRemovedFromStackCount(ActivityManagerService service,
     89             int stackId) {
     90         final ActivityStack stack = service.mStackSupervisor.getStack(stackId);
     91         if (stack instanceof ActivityStackReporter) {
     92             return ((ActivityStackReporter) stack).onActivityRemovedFromStackInvocationCount();
     93         }
     94 
     95         return -1;
     96     }
     97 }
     98