1 /* 2 * Copyright (C) 2025 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 android.content.Context 20 import android.os.StrictMode 21 import android.view.SurfaceControl 22 import android.window.ConfigurationChangeSetting 23 import com.android.server.DisplayThread 24 import com.android.server.LocalServices 25 import com.android.server.input.InputManagerService 26 import com.android.server.policy.WindowManagerPolicy 27 import com.android.window.flags.Flags 28 29 /** 30 * Provides support for tests that require a [WindowManagerService]. 31 * 32 * It provides functionalities for setting up and tearing down the service with proper dependencies, 33 * which can be used across different test modules. 34 */ 35 object WindowManagerServiceTestSupport { 36 37 /** 38 * Sets up and initializes a [WindowManagerService] instance with the provided dependencies. 39 * 40 * This method constructs a [WindowManagerService] using the provided dependencies for testing. 41 * It's marked as `internal` due to the package-private classes [DisplayWindowSettingsProvider] 42 * and [AppCompatConfiguration]. The `@JvmName` annotation is used to bypass name mangling and 43 * allow access from Java via `WindowManagerServiceTestSupport.setUpService`. 44 * 45 * **Important:** Before calling this method, ensure that any previous [WindowManagerService] 46 * instance and its related services are properly torn down. In your test's setup, it is 47 * recommended to call [tearDownService] before calling [setUpService] to handle cases where a 48 * previous test might have crashed and left services in an inconsistent state. This is crucial 49 * for test reliability. 50 * 51 * Example usage in a test's `setUp()` method: 52 * ``` 53 * @Before 54 * fun setUp() { 55 * WindowManagerServiceTestSupport.tearDownService() // Clean up before setup. 56 * mWindowManagerService = WindowManagerServiceTestSupport.setUpService(...) 57 * // ... rest of your setup logic ... 58 * } 59 * ``` 60 * 61 * @param context the [Context] for the service. 62 * @param im the [InputManagerService] to use. 63 * @param policy the [WindowManagerPolicy] to use. 64 * @param atm the [ActivityTaskManagerService] to use. 65 * @param displayWindowSettingsProvider the [DisplayWindowSettingsProvider] to use. 66 * @param surfaceControlTransaction the [SurfaceControl.Transaction] instance to use. 67 * @param surfaceControlBuilder the [SurfaceControl.Builder] instance to use. 68 * @param appCompat the [AppCompatConfiguration] to use. 69 * @return the created [WindowManagerService] instance. 70 */ 71 @JvmStatic 72 @JvmName("setUpService") setUpServicenull73 internal fun setUpService( 74 context: Context, 75 im: InputManagerService, 76 policy: WindowManagerPolicy, 77 atm: ActivityTaskManagerService, 78 displayWindowSettingsProvider: DisplayWindowSettingsProvider, 79 surfaceControlTransaction: SurfaceControl.Transaction, 80 surfaceControlBuilder: SurfaceControl.Builder, 81 appCompat: AppCompatConfiguration, 82 ): WindowManagerService { 83 // Suppress StrictMode violation (DisplayWindowSettings) to avoid log flood. 84 DisplayThread.getHandler().post { StrictMode.allowThreadDiskWritesMask() } 85 86 return WindowManagerService.main( 87 context, 88 im, 89 false, /* showBootMsgs */ 90 policy, 91 atm, 92 displayWindowSettingsProvider, 93 { surfaceControlTransaction }, 94 { surfaceControlBuilder }, 95 appCompat, 96 ) 97 } 98 99 /** Tears down the [WindowManagerService] and removes related local services. */ 100 @JvmStatic tearDownServicenull101 fun tearDownService() { 102 LocalServices.removeServiceForTest(WindowManagerPolicy::class.java) 103 LocalServices.removeServiceForTest(WindowManagerInternal::class.java) 104 LocalServices.removeServiceForTest(ImeTargetVisibilityPolicy::class.java) 105 106 if (Flags.condenseConfigurationChangeForSimpleMode()) { 107 LocalServices.removeServiceForTest( 108 ConfigurationChangeSetting.ConfigurationChangeSettingInternal::class.java, 109 ) 110 } 111 } 112 } 113