1 /* 2 * Copyright (C) 2021 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.content; 18 19 import static android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import android.app.WindowConfiguration; 24 import android.content.res.Configuration; 25 import android.graphics.Rect; 26 27 import androidx.annotation.NonNull; 28 import androidx.test.ext.junit.runners.AndroidJUnit4; 29 import androidx.test.filters.SmallTest; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 /** 36 * Build/Install/Run: 37 * atest FrameworksCoreTests:ComponentCallbacksControllerTest 38 */ 39 @SmallTest 40 @RunWith(AndroidJUnit4.class) 41 public class ComponentCallbacksControllerTest { 42 private ComponentCallbacksController mController; 43 44 @Before setUp()45 public void setUp() { 46 mController = new ComponentCallbacksController(); 47 } 48 49 @Test testUnregisterCallbackWithoutRegistrationNoCrash()50 public void testUnregisterCallbackWithoutRegistrationNoCrash() { 51 mController.unregisterCallbacks(new FakeComponentCallbacks()); 52 } 53 54 @Test testDispatchWithEmptyCallbacksNoCrash()55 public void testDispatchWithEmptyCallbacksNoCrash() { 56 mController.dispatchConfigurationChanged(new Configuration()); 57 mController.dispatchLowMemory(); 58 mController.dispatchTrimMemory(TRIM_MEMORY_BACKGROUND); 59 } 60 61 @Test testClearCallbacksNoCrash()62 public void testClearCallbacksNoCrash() { 63 mController.clearCallbacks(); 64 } 65 66 @Test testDispatchTrimMemoryWithoutComponentCallbacks2NoCrash()67 public void testDispatchTrimMemoryWithoutComponentCallbacks2NoCrash() { 68 // Register a ComponentCallbacks instead of ComponentCallbacks2 69 mController.registerCallbacks(new FakeComponentCallbacks()); 70 71 mController.dispatchTrimMemory(TRIM_MEMORY_BACKGROUND); 72 } 73 74 @Test testDispatchConfigurationChanged()75 public void testDispatchConfigurationChanged() throws Exception { 76 final TestComponentCallbacks2 callback = new TestComponentCallbacks2(); 77 mController.registerCallbacks(callback); 78 79 final Configuration config = new Configuration(); 80 config.windowConfiguration.setWindowingMode(WindowConfiguration.WINDOWING_MODE_FREEFORM); 81 config.windowConfiguration.setBounds(new Rect(0, 0, 100, 100)); 82 83 mController.dispatchConfigurationChanged(config); 84 85 assertThat(callback.mConfiguration).isEqualTo(config); 86 87 mController.dispatchConfigurationChanged(Configuration.EMPTY); 88 89 assertThat(callback.mConfiguration).isEqualTo(Configuration.EMPTY); 90 } 91 92 @Test testDispatchLowMemory()93 public void testDispatchLowMemory() { 94 final TestComponentCallbacks2 callback = new TestComponentCallbacks2(); 95 mController.registerCallbacks(callback); 96 97 mController.dispatchLowMemory(); 98 99 assertThat(callback.mLowMemoryCalled).isTrue(); 100 } 101 102 @Test testDispatchTrimMemory()103 public void testDispatchTrimMemory() { 104 final TestComponentCallbacks2 callback = new TestComponentCallbacks2(); 105 mController.registerCallbacks(callback); 106 107 mController.dispatchTrimMemory(TRIM_MEMORY_BACKGROUND); 108 109 assertThat(callback.mLevel).isEqualTo(TRIM_MEMORY_BACKGROUND); 110 } 111 112 private static class FakeComponentCallbacks implements ComponentCallbacks { 113 @Override onConfigurationChanged(@onNull Configuration newConfig)114 public void onConfigurationChanged(@NonNull Configuration newConfig) {} 115 116 @Override onLowMemory()117 public void onLowMemory() {} 118 } 119 } 120