1/*
2 * Copyright (C) 2018 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 */
16package com.android.keyguard;
17
18import android.graphics.Color;
19import android.net.Uri;
20import android.test.suitebuilder.annotation.SmallTest;
21import android.testing.AndroidTestingRunner;
22import android.testing.TestableLooper.RunWithLooper;
23import android.view.LayoutInflater;
24
25import com.android.systemui.SysuiTestCase;
26import com.android.systemui.keyguard.KeyguardSliceProvider;
27
28import org.junit.Assert;
29import org.junit.Before;
30import org.junit.Test;
31import org.junit.runner.RunWith;
32
33import java.util.Collections;
34import java.util.HashSet;
35import java.util.concurrent.atomic.AtomicBoolean;
36
37import androidx.slice.SliceProvider;
38import androidx.slice.SliceSpecs;
39import androidx.slice.builders.ListBuilder;
40
41@SmallTest
42@RunWithLooper(setAsMainLooper = true)
43@RunWith(AndroidTestingRunner.class)
44public class KeyguardSliceViewTest extends SysuiTestCase {
45    private KeyguardSliceView mKeyguardSliceView;
46    private Uri mSliceUri;
47
48    @Before
49    public void setUp() throws Exception {
50        mKeyguardSliceView = (KeyguardSliceView) LayoutInflater.from(getContext())
51                .inflate(R.layout.keyguard_status_area, null);
52        mSliceUri = Uri.parse(KeyguardSliceProvider.KEYGUARD_SLICE_URI);
53        SliceProvider.setSpecs(new HashSet<>(Collections.singletonList(SliceSpecs.LIST)));
54    }
55
56    @Test
57    public void showSlice_notifiesListener() {
58        ListBuilder builder = new ListBuilder(getContext(), mSliceUri);
59        AtomicBoolean notified = new AtomicBoolean();
60        mKeyguardSliceView.setContentChangeListener(()-> notified.set(true));
61        mKeyguardSliceView.onChanged(builder.build());
62        Assert.assertTrue("Listener should be notified about slice changes.",
63                notified.get());
64    }
65
66    @Test
67    public void showSlice_emptySliceNotifiesListener() {
68        AtomicBoolean notified = new AtomicBoolean();
69        mKeyguardSliceView.setContentChangeListener(()-> notified.set(true));
70        mKeyguardSliceView.onChanged(null);
71        Assert.assertTrue("Listener should be notified about slice changes.",
72                notified.get());
73    }
74
75    @Test
76    public void hasHeader_readsSliceData() {
77        ListBuilder builder = new ListBuilder(getContext(), mSliceUri);
78        mKeyguardSliceView.onChanged(builder.build());
79        Assert.assertFalse("View should not have a header", mKeyguardSliceView.hasHeader());
80
81        builder.setHeader((ListBuilder.HeaderBuilder headerBuilder) -> {
82            headerBuilder.setTitle("header title!");
83        });
84        mKeyguardSliceView.onChanged(builder.build());
85        Assert.assertTrue("View should have a header", mKeyguardSliceView.hasHeader());
86    }
87
88    @Test
89    public void refresh_replacesSliceContentAndNotifiesListener() {
90        AtomicBoolean notified = new AtomicBoolean();
91        mKeyguardSliceView.setContentChangeListener(()-> notified.set(true));
92        mKeyguardSliceView.refresh();
93        Assert.assertTrue("Listener should be notified about slice changes.",
94                notified.get());
95    }
96
97    @Test
98    public void getTextColor_whiteTextWhenAOD() {
99        // Set text color to red since the default is white and test would always pass
100        mKeyguardSliceView.setTextColor(Color.RED);
101        mKeyguardSliceView.setDarkAmount(0);
102        Assert.assertEquals("Should be using regular text color", Color.RED,
103                mKeyguardSliceView.getTextColor());
104        mKeyguardSliceView.setDarkAmount(1);
105        Assert.assertEquals("Should be using AOD text color", Color.WHITE,
106                mKeyguardSliceView.getTextColor());
107    }
108}