1 /* 2 * Copyright (C) 2017 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 com.android.keyguard; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertTrue; 22 23 import android.support.test.filters.SmallTest; 24 import android.text.TextUtils; 25 import android.view.accessibility.AccessibilityEvent; 26 import android.view.accessibility.AccessibilityNodeInfo; 27 import android.widget.TextView; 28 29 import com.android.systemui.SysuiTestCase; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 34 import java.util.List; 35 36 @SmallTest 37 public class KeyguardClockAccessibilityDelegateTest extends SysuiTestCase { 38 39 private TextView mView; 40 private String m12HoursFormat; 41 private String m24HoursFormat; 42 43 @Before setUp()44 public void setUp() throws Exception { 45 m12HoursFormat = mContext.getString(R.string.keyguard_widget_12_hours_format); 46 m24HoursFormat = mContext.getString(R.string.keyguard_widget_24_hours_format); 47 48 mView = new TextView(mContext); 49 mView.setText(m12HoursFormat); 50 mView.setContentDescription(m12HoursFormat); 51 mView.setAccessibilityDelegate(new KeyguardClockAccessibilityDelegate(mContext)); 52 } 53 54 @Test onInitializeAccessibilityEvent_producesNonEmptyAsciiContentDesc()55 public void onInitializeAccessibilityEvent_producesNonEmptyAsciiContentDesc() throws Exception { 56 AccessibilityEvent ev = AccessibilityEvent.obtain(); 57 mView.onInitializeAccessibilityEvent(ev); 58 59 assertFalse(TextUtils.isEmpty(ev.getContentDescription())); 60 assertTrue(isAscii(ev.getContentDescription())); 61 } 62 63 @Test onPopulateAccessibilityEvent_producesNonEmptyAsciiText()64 public void onPopulateAccessibilityEvent_producesNonEmptyAsciiText() throws Exception { 65 AccessibilityEvent ev = AccessibilityEvent.obtain(); 66 mView.onPopulateAccessibilityEvent(ev); 67 68 assertFalse(isEmpty(ev.getText())); 69 assertTrue(isAscii(ev.getText())); 70 } 71 72 @Test onInitializeAccessibilityNodeInfo_producesNonEmptyAsciiText()73 public void onInitializeAccessibilityNodeInfo_producesNonEmptyAsciiText() throws Exception { 74 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(); 75 // Usually done in View.onInitializeAccessibilityNodeInfoInternal, but only when attached. 76 info.setContentDescription(mView.getContentDescription()); 77 mView.onInitializeAccessibilityNodeInfo(info); 78 79 assertFalse(TextUtils.isEmpty(info.getText())); 80 assertTrue(isAscii(info.getText())); 81 82 assertFalse(TextUtils.isEmpty(info.getContentDescription())); 83 assertTrue(isAscii(info.getContentDescription())); 84 } 85 86 @Test isNeeded_returnsTrueIfDateFormatsContainNonAscii()87 public void isNeeded_returnsTrueIfDateFormatsContainNonAscii() { 88 if (!isAscii(m12HoursFormat) || !isAscii(m24HoursFormat)) { 89 assertTrue(KeyguardClockAccessibilityDelegate.isNeeded(mContext)); 90 } 91 } 92 93 @Test isNeeded_returnsWhetherFancyColonExists()94 public void isNeeded_returnsWhetherFancyColonExists() { 95 boolean hasFancyColon = !TextUtils.isEmpty(mContext.getString( 96 R.string.keyguard_fancy_colon)); 97 98 assertEquals(hasFancyColon, KeyguardClockAccessibilityDelegate.isNeeded(mContext)); 99 } 100 isAscii(CharSequence text)101 private boolean isAscii(CharSequence text) { 102 return text.chars().allMatch((i) -> i < 128); 103 } 104 isAscii(List<CharSequence> texts)105 private boolean isAscii(List<CharSequence> texts) { 106 return texts.stream().allMatch(this::isAscii); 107 } 108 isEmpty(List<CharSequence> texts)109 private boolean isEmpty(List<CharSequence> texts) { 110 return texts.stream().allMatch(TextUtils::isEmpty); 111 } 112 }