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