blob: 1c6016e16f3956e92e0d673ca99f3f54b9b16987 [file] [log] [blame]
Jason Monkde850bb2017-02-01 19:26:30 -05001/*
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
Jason Monk340b0e52017-03-08 14:57:56 -050015package android.testing;
Jason Monkde850bb2017-02-01 19:26:30 -050016
Jason Monk9ec0f1e2017-02-23 11:33:54 -050017import android.content.pm.ApplicationInfo;
Jason Monkde850bb2017-02-01 19:26:30 -050018import android.graphics.PixelFormat;
Jason Monk340b0e52017-03-08 14:57:56 -050019import android.support.test.InstrumentationRegistry;
Jason Monkde850bb2017-02-01 19:26:30 -050020import android.view.View;
21import android.view.WindowManager;
22import android.view.WindowManager.LayoutParams;
Jason Monkde850bb2017-02-01 19:26:30 -050023
Jason Monk0c408002017-05-03 15:43:52 -040024/**
25 * Utilities to make testing views easier.
26 */
Jason Monkde850bb2017-02-01 19:26:30 -050027public class ViewUtils {
28
Jason Monk0c408002017-05-03 15:43:52 -040029 /**
30 * Causes the view (and its children) to have {@link View#onAttachedToWindow()} called.
31 *
32 * This is currently done by adding the view to a window.
33 */
Jason Monkde850bb2017-02-01 19:26:30 -050034 public static void attachView(View view) {
Jason Monk9ec0f1e2017-02-23 11:33:54 -050035 // Make sure hardware acceleration isn't turned on.
36 view.getContext().getApplicationInfo().flags &=
37 ~(ApplicationInfo.FLAG_HARDWARE_ACCELERATED);
Jason Monkde850bb2017-02-01 19:26:30 -050038 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
39 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
Jason Monk7d6fa832017-02-03 15:16:21 -050040 LayoutParams.TYPE_APPLICATION_OVERLAY,
Jason Monkde850bb2017-02-01 19:26:30 -050041 0, PixelFormat.TRANSLUCENT);
Jason Monk893f0bd2017-06-01 11:21:14 -040042 view.getContext().getSystemService(WindowManager.class).addView(view, lp);
Jason Monkde850bb2017-02-01 19:26:30 -050043 }
44
Jason Monk0c408002017-05-03 15:43:52 -040045 /**
46 * Causes the view (and its children) to have {@link View#onDetachedFromWindow()} called.
47 *
48 * This is currently done by removing the view from a window.
49 */
Jason Monkde850bb2017-02-01 19:26:30 -050050 public static void detachView(View view) {
Jason Monk893f0bd2017-06-01 11:21:14 -040051 view.getContext().getSystemService(WindowManager.class).removeViewImmediate(view);
Jason Monkde850bb2017-02-01 19:26:30 -050052 }
53}