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