1810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase/* 2810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * Copyright (C) 2012 The Android Open Source Project 3810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * 4810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * Licensed under the Apache License, Version 2.0 (the "License"); 5810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * you may not use this file except in compliance with the License. 6810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * You may obtain a copy of the License at 7810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * 8810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * http://www.apache.org/licenses/LICENSE-2.0 9810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * 10810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * Unless required by applicable law or agreed to in writing, software 11810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * distributed under the License is distributed on an "AS IS" BASIS, 12810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * See the License for the specific language governing permissions and 14810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * limitations under the License. 15810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase */ 16810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 17810a8676df1d504da17bad80c7bd6638bdd97711Chet Haasepackage com.android.test.hwui; 18810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 19810a8676df1d504da17bad80c7bd6638bdd97711Chet Haaseimport android.app.Activity; 20810a8676df1d504da17bad80c7bd6638bdd97711Chet Haaseimport android.graphics.Color; 21810a8676df1d504da17bad80c7bd6638bdd97711Chet Haaseimport android.os.Bundle; 22810a8676df1d504da17bad80c7bd6638bdd97711Chet Haaseimport android.view.View; 23810a8676df1d504da17bad80c7bd6638bdd97711Chet Haaseimport android.view.ViewGroup; 24810a8676df1d504da17bad80c7bd6638bdd97711Chet Haaseimport android.widget.LinearLayout; 25810a8676df1d504da17bad80c7bd6638bdd97711Chet Haaseimport android.widget.TextView; 26810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 27810a8676df1d504da17bad80c7bd6638bdd97711Chet Haaseimport java.util.ArrayList; 28810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 29810a8676df1d504da17bad80c7bd6638bdd97711Chet Haasepublic class ViewLayerInvalidationActivity extends Activity { 30810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 31810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase int currentColor = Color.WHITE; 32810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase boolean nestedLayersOn = false; 33810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase ArrayList<LinearLayout> linearLayouts = new ArrayList<LinearLayout>(); 34810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase ArrayList<LinearLayout> topLayouts = new ArrayList<LinearLayout>(); 35810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase ArrayList<TextView> textViews = new ArrayList<TextView>(); 36810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase LinearLayout container = null; 37810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase boolean randomInvalidates = false; 38810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase TextView nestedStatusTV, invalidateStatusTV; 39810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase static final String NO_NESTING = "Nested Layer: NO "; 40810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase static final String NESTING = "Nested Layers: YES "; 41810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase static final String NO_INVALIDATING = "Random Invalidating: NO "; 42810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase static final String INVALIDATING = "Random Invalidating: YES "; 43810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase static final int TEXT_COLOR_INTERVAL = 400; 44810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase static final int INVALIDATING_INTERVAL = 1000; 45810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase static final int NESTING_INTERVAL = 2000; 46810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 47810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase @Override 48810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase public void onCreate(Bundle savedInstanceState) { 49810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase super.onCreate(savedInstanceState); 50810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase setContentView(R.layout.view_layer_invalidation); 51810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 52810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase container = (LinearLayout) findViewById(R.id.container); 53810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase final LinearLayout container1 = (LinearLayout) findViewById(R.id.container1); 54810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase final LinearLayout container2 = (LinearLayout) findViewById(R.id.container2); 55810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase final LinearLayout container3 = (LinearLayout) findViewById(R.id.container3); 56810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase nestedStatusTV = (TextView) findViewById(R.id.nestedStatus); 57810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase invalidateStatusTV = (TextView) findViewById(R.id.invalidateStatus); 58810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase final TextView tva = (TextView) findViewById(R.id.textviewa); 59810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 60810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase topLayouts.add(container1); 61810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase topLayouts.add(container2); 62810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase topLayouts.add(container3); 63810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 64810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase collectLinearLayouts(container); 65810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase collectTextViews(container); 66810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 67810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase nestedStatusTV.setText(NO_NESTING); 68810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase invalidateStatusTV.setText(NO_INVALIDATING); 69810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 70810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase tva.setLayerType(View.LAYER_TYPE_HARDWARE, null); 71810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase container1.setLayerType(View.LAYER_TYPE_HARDWARE, null); 72810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase container2.setLayerType(View.LAYER_TYPE_HARDWARE, null); 73810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase container3.setLayerType(View.LAYER_TYPE_HARDWARE, null); 74810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 75810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase container.postDelayed(textColorSetter, TEXT_COLOR_INTERVAL); 76810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase container.postDelayed(nestedLayerSetter, NESTING_INTERVAL); 77810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase container.postDelayed(randomInvalidatesSetter, INVALIDATING_INTERVAL); 78810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 79810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 80810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase private Runnable textColorSetter = new Runnable() { 81810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase @Override 82810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase public void run() { 83810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase currentColor = (currentColor == Color.WHITE) ? Color.RED : Color.WHITE; 84810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase for (TextView tv : textViews) { 85810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase tv.setTextColor(currentColor); 86810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 87810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase if (randomInvalidates) { 88810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase randomInvalidator(container); 89810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 90810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase container.postDelayed(textColorSetter, TEXT_COLOR_INTERVAL); 91810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 92810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase }; 93810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 94810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase private Runnable randomInvalidatesSetter = new Runnable() { 95810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase @Override 96810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase public void run() { 97810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase randomInvalidates = !randomInvalidates; 98810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase invalidateStatusTV.setText(randomInvalidates ? INVALIDATING : NO_INVALIDATING); 99810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase container.postDelayed(randomInvalidatesSetter, INVALIDATING_INTERVAL); 100810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 101810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase }; 102810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 103810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase private Runnable nestedLayerSetter = new Runnable() { 104810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase @Override 105810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase public void run() { 106810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase nestedLayersOn = !nestedLayersOn; 107810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase nestedStatusTV.setText(nestedLayersOn ? NESTING : NO_NESTING); 108810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase for (LinearLayout layout : linearLayouts) { 109810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase layout.setLayerType(nestedLayersOn ? 110810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase View.LAYER_TYPE_HARDWARE : View.LAYER_TYPE_NONE, null); 111810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 112810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase if (!nestedLayersOn) { 113810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase for (LinearLayout layout : topLayouts) { 114810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase layout.setLayerType(View.LAYER_TYPE_HARDWARE, null); 115810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 116810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 117810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase container.postDelayed(nestedLayerSetter, NESTING_INTERVAL); 118810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 119810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase }; 120810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 121810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase /** 122810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * Invalidates views based on random chance (50%). This is meant to test 123810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * invalidating several items in the hierarchy at the same time, which can cause artifacts 124810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase * if our invalidation-propagation logic is not sound. 125810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase */ 126810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase private void randomInvalidator(ViewGroup parent) { 127810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase for (int i = 0; i < parent.getChildCount(); ++i) { 128810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase View child = parent.getChildAt(i); 129810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase if (Math.random() < .5) { 130810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase child.invalidate(); 131810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 132810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase if (child instanceof ViewGroup) { 133810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase randomInvalidator((ViewGroup) child); 134810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 135810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 136810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 137810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 138810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase private void collectLinearLayouts(View view) { 139810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase if (!(view instanceof LinearLayout)) { 140810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase return; 141810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 142810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase LinearLayout parent = (LinearLayout) view; 143810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase linearLayouts.add(parent); 144810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase for (int i = 0; i < parent.getChildCount(); ++i) { 145810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase collectLinearLayouts(parent.getChildAt(i)); 146810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 147810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 148810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase 149810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase private void collectTextViews(View view) { 150810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase if (view instanceof TextView) { 151810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase textViews.add((TextView) view); 152810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase return; 153810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 154810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase if (!(view instanceof ViewGroup)) { 155810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase return; 156810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 157810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase ViewGroup parent = (ViewGroup) view; 158810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase for (int i = 0; i < parent.getChildCount(); ++i) { 159810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase collectTextViews(parent.getChildAt(i)); 160810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 161810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase } 162810a8676df1d504da17bad80c7bd6638bdd97711Chet Haase} 163