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 android.server.wm.jetpack.utils; 18 19 import static android.server.wm.jetpack.utils.ExtensionUtil.assumeExtensionSupportedDevice; 20 import static android.server.wm.jetpack.utils.ExtensionUtil.getExtensionWindowAreaComponent; 21 import static android.server.wm.jetpack.utils.ExtensionUtil.getExtensionWindowLayoutComponent; 22 23 import static org.junit.Assume.assumeNotNull; 24 25 import androidx.window.extensions.area.WindowAreaComponent; 26 import androidx.window.extensions.layout.WindowLayoutComponent; 27 28 import org.junit.rules.TestRule; 29 import org.junit.runner.Description; 30 import org.junit.runners.model.Statement; 31 32 public class WindowExtensionTestRule implements TestRule { 33 34 private final Class<?> mComponentToFetch; 35 private Object mComponent; 36 getExtensionComponent()37 public Object getExtensionComponent() { 38 return mComponent; 39 } 40 41 @Override apply(Statement base, Description description)42 public Statement apply(Statement base, Description description) { 43 return new Statement() { 44 @Override 45 public void evaluate() throws Throwable { 46 assumeExtensionSupportedDevice(); 47 mComponent = getComponent(mComponentToFetch); 48 assumeNotNull(mComponent); 49 base.evaluate(); 50 } 51 }; 52 } 53 54 public WindowExtensionTestRule(Class<?> componentType) { 55 mComponentToFetch = componentType; 56 } 57 58 public Object getComponent(Class<?> componentToFetch) { 59 if (componentToFetch == WindowAreaComponent.class) { 60 return getExtensionWindowAreaComponent(); 61 } else if (componentToFetch == WindowLayoutComponent.class) { 62 return getExtensionWindowLayoutComponent(); 63 } 64 return null; 65 } 66 } 67