1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package android.testing; 16 17 import android.content.pm.ApplicationInfo; 18 import android.graphics.PixelFormat; 19 import android.view.View; 20 import android.view.WindowManager; 21 import android.view.WindowManager.LayoutParams; 22 23 /** 24 * Utilities to make testing views easier. 25 */ 26 public class ViewUtils { 27 28 /** 29 * Causes the view (and its children) to have {@link View#onAttachedToWindow()} called. 30 * 31 * This is currently done by adding the view to a window. 32 */ attachView(View view)33 public static void attachView(View view) { 34 attachView(view, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 35 } 36 37 /** 38 * Causes the view (and its children) to have {@link View#onAttachedToWindow()} called. 39 * 40 * This is currently done by adding the view to a window. 41 */ attachView(View view, int width, int height)42 public static void attachView(View view, int width, int height) { 43 // Make sure hardware acceleration isn't turned on. 44 view.getContext().getApplicationInfo().flags &= 45 ~(ApplicationInfo.FLAG_HARDWARE_ACCELERATED); 46 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(width, height, 47 LayoutParams.TYPE_APPLICATION_OVERLAY, 0, PixelFormat.TRANSLUCENT); 48 view.getContext().getSystemService(WindowManager.class).addView(view, lp); 49 } 50 51 /** 52 * Causes the view (and its children) to have {@link View#onDetachedFromWindow()} called. 53 * 54 * This is currently done by removing the view from a window. 55 */ detachView(View view)56 public static void detachView(View view) { 57 view.getContext().getSystemService(WindowManager.class).removeViewImmediate(view); 58 } 59 } 60