1 // Copyright 2019 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.components.browser_ui.widget.highlight; 6 7 import android.graphics.drawable.Drawable; 8 import android.graphics.drawable.LayerDrawable; 9 import android.view.View; 10 11 import org.chromium.base.test.util.CriteriaHelper; 12 13 /** 14 * Allows for testing of views which are highlightable via ViewHighlighter. 15 */ 16 public class ViewHighlighterTestUtils { 17 /** 18 * Returns true if the provided view is currently being highlighted. 19 * Please note that this function may not be the same as !checkHighlightOff. 20 * 21 * @param view The view which you'd like to check for highlighting. 22 * @return True if the view is currently being highlighted. 23 */ checkHighlightOn(View view)24 public static boolean checkHighlightOn(View view) { 25 if (!(view.getBackground() instanceof LayerDrawable)) return false; 26 LayerDrawable layerDrawable = (LayerDrawable) view.getBackground(); 27 Drawable drawable = layerDrawable.getDrawable(layerDrawable.getNumberOfLayers() - 1); 28 if (!(drawable instanceof PulseDrawable)) return false; 29 PulseDrawable pulse = (PulseDrawable) drawable; 30 return pulse.isRunning() && pulse.isVisible(); 31 } 32 33 /** 34 * Returns true if the provided view is not currently being highlighted. 35 * Please note that this function may not be the same as !checkHighlightOn. 36 * 37 * @param view The view which you'd like to check for highlighting. 38 * @return True if view is not currently being highlighted. 39 */ checkHighlightOff(View view)40 public static boolean checkHighlightOff(View view) { 41 return !(view.getBackground() instanceof LayerDrawable); 42 } 43 44 /** 45 * Checks that the view is highlighted with a pulse highlight. 46 * 47 * @param view The view of interest. 48 * @param timeoutDuration The timeout duration (should be set depending on the number of pulses 49 * and the pulse duration). 50 * @return True iff the view was highlighted, and then turned off. 51 */ checkHighlightPulse(View view, long timeoutDuration)52 public static boolean checkHighlightPulse(View view, long timeoutDuration) { 53 try { 54 CriteriaHelper.pollUiThread(() 55 -> checkHighlightOn(view), 56 "Expected highlight to pulse on!", timeoutDuration, 57 CriteriaHelper.DEFAULT_POLLING_INTERVAL); 58 CriteriaHelper.pollUiThread(() 59 -> checkHighlightOff(view), 60 "Expected highlight to turn off!", timeoutDuration, 61 CriteriaHelper.DEFAULT_POLLING_INTERVAL); 62 } catch (AssertionError e) { 63 e.printStackTrace(); 64 return false; 65 } 66 return true; 67 } 68 69 /** 70 * Checks that the view is highlighted with a pulse highlight. 71 * 72 * @param view The view of interest. 73 * @return True iff the view was highlighted, and then turned off. 74 */ checkHighlightPulse(View view)75 public static boolean checkHighlightPulse(View view) { 76 return checkHighlightPulse(view, CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL); 77 } 78 } 79