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