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 | |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 23 | import android.support.test.filters.SmallTest; |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 24 | import android.text.TextUtils; |
| 25 | import android.view.accessibility.AccessibilityEvent; |
| 26 | import android.view.accessibility.AccessibilityNodeInfo; |
| 27 | import android.widget.TextView; |
| 28 | |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 29 | import com.android.systemui.SysuiTestCase; |
| 30 | |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 31 | import org.junit.Before; |
| 32 | import org.junit.Test; |
| 33 | |
| 34 | import java.util.List; |
| 35 | |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 36 | @SmallTest |
| 37 | public class KeyguardClockAccessibilityDelegateTest extends SysuiTestCase { |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 38 | |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 39 | private TextView mView; |
Adrian Roos | b670f4b | 2017-07-20 20:06:54 +0200 | [diff] [blame] | 40 | private String m12HoursFormat; |
| 41 | private String m24HoursFormat; |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 42 | |
| 43 | @Before |
| 44 | public void setUp() throws Exception { |
Adrian Roos | b670f4b | 2017-07-20 20:06:54 +0200 | [diff] [blame] | 45 | m12HoursFormat = mContext.getString(R.string.keyguard_widget_12_hours_format); |
| 46 | m24HoursFormat = mContext.getString(R.string.keyguard_widget_24_hours_format); |
| 47 | |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 48 | mView = new TextView(mContext); |
Adrian Roos | b670f4b | 2017-07-20 20:06:54 +0200 | [diff] [blame] | 49 | mView.setText(m12HoursFormat); |
| 50 | mView.setContentDescription(m12HoursFormat); |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 51 | mView.setAccessibilityDelegate(new KeyguardClockAccessibilityDelegate(mContext)); |
| 52 | } |
| 53 | |
| 54 | @Test |
| 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 |
| 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 |
| 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 | |
Adrian Roos | b670f4b | 2017-07-20 20:06:54 +0200 | [diff] [blame] | 86 | @Test |
| 87 | public void isNeeded_returnsTrueIfDateFormatsContainNonAscii() { |
| 88 | if (!isAscii(m12HoursFormat) || !isAscii(m24HoursFormat)) { |
| 89 | assertTrue(KeyguardClockAccessibilityDelegate.isNeeded(mContext)); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | @Test |
| 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 | |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 101 | private boolean isAscii(CharSequence text) { |
| 102 | return text.chars().allMatch((i) -> i < 128); |
| 103 | } |
| 104 | |
| 105 | private boolean isAscii(List<CharSequence> texts) { |
| 106 | return texts.stream().allMatch(this::isAscii); |
| 107 | } |
| 108 | |
| 109 | private boolean isEmpty(List<CharSequence> texts) { |
| 110 | return texts.stream().allMatch(TextUtils::isEmpty); |
| 111 | } |
| 112 | } |