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.SysuiTestCase;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 
35 import java.util.List;
36 
37 @SmallTest
38 public class KeyguardClockAccessibilityDelegateTest extends SysuiTestCase {
39 
40     private TextView mView;
41     private String m12HoursFormat;
42     private String m24HoursFormat;
43 
44     @Before
setUp()45     public void setUp() throws Exception {
46         m12HoursFormat = mContext.getString(R.string.keyguard_widget_12_hours_format);
47         m24HoursFormat = mContext.getString(R.string.keyguard_widget_24_hours_format);
48 
49         mView = new TextView(mContext);
50         mView.setText(m12HoursFormat);
51         mView.setContentDescription(m12HoursFormat);
52         mView.setAccessibilityDelegate(new KeyguardClockAccessibilityDelegate(mContext));
53     }
54 
55     @Test
onInitializeAccessibilityEvent_producesNonEmptyAsciiContentDesc()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
onPopulateAccessibilityEvent_producesNonEmptyAsciiText()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
onInitializeAccessibilityNodeInfo_producesNonEmptyAsciiText()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 
87     @Test
isNeeded_returnsTrueIfDateFormatsContainNonAscii()88     public void isNeeded_returnsTrueIfDateFormatsContainNonAscii() {
89         if (!isAscii(m12HoursFormat) || !isAscii(m24HoursFormat)) {
90             assertTrue(KeyguardClockAccessibilityDelegate.isNeeded(mContext));
91         }
92     }
93 
94     @Test
isNeeded_returnsWhetherFancyColonExists()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 
isAscii(CharSequence text)102     private boolean isAscii(CharSequence text) {
103         return text.chars().allMatch((i) -> i < 128);
104     }
105 
isAscii(List<CharSequence> texts)106     private boolean isAscii(List<CharSequence> texts) {
107         return texts.stream().allMatch(this::isAscii);
108     }
109 
isEmpty(List<CharSequence> texts)110     private boolean isEmpty(List<CharSequence> texts) {
111         return texts.stream().allMatch(TextUtils::isEmpty);
112     }
113 }