Home | History | Annotate | Download | only in phone
      1 /*
      2  * Copyright (C) 2016 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.systemui.statusbar.phone;
     18 
     19 import android.content.Context;
     20 import android.os.PowerManager;
     21 import android.support.test.runner.AndroidJUnit4;
     22 import android.test.suitebuilder.annotation.SmallTest;
     23 
     24 import com.android.systemui.SysuiTestCase;
     25 import com.android.systemui.doze.DozeScreenState;
     26 import com.android.systemui.statusbar.phone.DozeParameters.IntInOutMatcher;
     27 
     28 import org.junit.Assert;
     29 import org.junit.Test;
     30 import org.junit.runner.RunWith;
     31 
     32 import static junit.framework.Assert.assertTrue;
     33 import static junit.framework.Assert.assertFalse;
     34 import static junit.framework.Assert.fail;
     35 
     36 import static org.mockito.ArgumentMatchers.any;
     37 import static org.mockito.ArgumentMatchers.eq;
     38 import static org.mockito.Mockito.mock;
     39 import static org.mockito.Mockito.never;
     40 import static org.mockito.Mockito.reset;
     41 import static org.mockito.Mockito.verify;
     42 import static org.mockito.Mockito.when;
     43 
     44 @SmallTest
     45 @RunWith(AndroidJUnit4.class)
     46 public class DozeParametersTest extends SysuiTestCase {
     47 
     48     @Test
     49     public void test_inOutMatcher_defaultIn() {
     50         IntInOutMatcher intInOutMatcher = new IntInOutMatcher("*");
     51 
     52         assertTrue(intInOutMatcher.isIn(1));
     53         assertTrue(intInOutMatcher.isIn(-1));
     54         assertTrue(intInOutMatcher.isIn(0));
     55     }
     56 
     57     @Test
     58     public void test_inOutMatcher_defaultOut() {
     59         IntInOutMatcher intInOutMatcher = new IntInOutMatcher("!*");
     60 
     61         assertFalse(intInOutMatcher.isIn(1));
     62         assertFalse(intInOutMatcher.isIn(-1));
     63         assertFalse(intInOutMatcher.isIn(0));
     64     }
     65 
     66     @Test
     67     public void test_inOutMatcher_someIn() {
     68         IntInOutMatcher intInOutMatcher = new IntInOutMatcher("1,2,3,!*");
     69 
     70         assertTrue(intInOutMatcher.isIn(1));
     71         assertTrue(intInOutMatcher.isIn(2));
     72         assertTrue(intInOutMatcher.isIn(3));
     73 
     74         assertFalse(intInOutMatcher.isIn(0));
     75         assertFalse(intInOutMatcher.isIn(4));
     76     }
     77 
     78     @Test
     79     public void test_inOutMatcher_someOut() {
     80         IntInOutMatcher intInOutMatcher = new IntInOutMatcher("!1,!2,!3,*");
     81 
     82         assertFalse(intInOutMatcher.isIn(1));
     83         assertFalse(intInOutMatcher.isIn(2));
     84         assertFalse(intInOutMatcher.isIn(3));
     85 
     86         assertTrue(intInOutMatcher.isIn(0));
     87         assertTrue(intInOutMatcher.isIn(4));
     88     }
     89 
     90     @Test
     91     public void test_inOutMatcher_mixed() {
     92         IntInOutMatcher intInOutMatcher = new IntInOutMatcher("!1,2,!3,*");
     93 
     94         assertFalse(intInOutMatcher.isIn(1));
     95         assertTrue(intInOutMatcher.isIn(2));
     96         assertFalse(intInOutMatcher.isIn(3));
     97 
     98         assertTrue(intInOutMatcher.isIn(0));
     99         assertTrue(intInOutMatcher.isIn(4));
    100     }
    101 
    102     @Test
    103     public void test_inOutMatcher_failEmpty() {
    104         try {
    105             new IntInOutMatcher("");
    106             fail("Expected IllegalArgumentException");
    107         } catch (IllegalArgumentException e) {
    108             // expected
    109         }
    110     }
    111 
    112     @Test
    113     public void test_inOutMatcher_failNull() {
    114         try {
    115             new IntInOutMatcher(null);
    116             fail("Expected IllegalArgumentException");
    117         } catch (IllegalArgumentException e) {
    118             // expected
    119         }
    120     }
    121 
    122     @Test
    123     public void test_inOutMatcher_failEmptyClause() {
    124         try {
    125             new IntInOutMatcher("!1,*,");
    126             fail("Expected IllegalArgumentException");
    127         } catch (IllegalArgumentException e) {
    128             // expected
    129         }
    130     }
    131 
    132     @Test
    133     public void test_inOutMatcher_failDuplicate() {
    134         try {
    135             new IntInOutMatcher("!1,*,!1");
    136             fail("Expected IllegalArgumentException");
    137         } catch (IllegalArgumentException e) {
    138             // expected
    139         }
    140     }
    141 
    142     @Test
    143     public void test_inOutMatcher_failDuplicateDefault() {
    144         try {
    145             new IntInOutMatcher("!1,*,*");
    146             fail("Expected IllegalArgumentException");
    147         } catch (IllegalArgumentException e) {
    148             // expected
    149         }
    150     }
    151 
    152     @Test
    153     public void test_inOutMatcher_failMalformedNot() {
    154         try {
    155             new IntInOutMatcher("!,*");
    156             fail("Expected IllegalArgumentException");
    157         } catch (IllegalArgumentException e) {
    158             // expected
    159         }
    160     }
    161 
    162     @Test
    163     public void test_inOutMatcher_failText() {
    164         try {
    165             new IntInOutMatcher("!abc,*");
    166             fail("Expected IllegalArgumentException");
    167         } catch (IllegalArgumentException e) {
    168             // expected
    169         }
    170     }
    171 
    172     @Test
    173     public void test_inOutMatcher_failContradiction() {
    174         try {
    175             new IntInOutMatcher("1,!1,*");
    176             fail("Expected IllegalArgumentException");
    177         } catch (IllegalArgumentException e) {
    178             // expected
    179         }
    180     }
    181 
    182     @Test
    183     public void test_inOutMatcher_failContradictionDefault() {
    184         try {
    185             new IntInOutMatcher("1,*,!*");
    186             fail("Expected IllegalArgumentException");
    187         } catch (IllegalArgumentException e) {
    188             // expected
    189         }
    190     }
    191 
    192     @Test
    193     public void test_inOutMatcher_failMissingDefault() {
    194         try {
    195             new IntInOutMatcher("1");
    196             fail("Expected IllegalArgumentException");
    197         } catch (IllegalArgumentException e) {
    198             // expected
    199         }
    200     }
    201 
    202     @Test
    203     public void test_setControlScreenOffAnimation_setsDozeAfterScreenOff_false() {
    204         TestableDozeParameters dozeParameters = new TestableDozeParameters(getContext());
    205         PowerManager mockedPowerManager = dozeParameters.getPowerManager();
    206         dozeParameters.setControlScreenOffAnimation(true);
    207         reset(mockedPowerManager);
    208         dozeParameters.setControlScreenOffAnimation(false);
    209         verify(mockedPowerManager).setDozeAfterScreenOff(eq(true));
    210     }
    211 
    212     @Test
    213     public void test_setControlScreenOffAnimation_setsDozeAfterScreenOff_true() {
    214         TestableDozeParameters dozeParameters = new TestableDozeParameters(getContext());
    215         PowerManager mockedPowerManager = dozeParameters.getPowerManager();
    216         dozeParameters.setControlScreenOffAnimation(false);
    217         reset(mockedPowerManager);
    218         dozeParameters.setControlScreenOffAnimation(true);
    219         verify(dozeParameters.getPowerManager()).setDozeAfterScreenOff(eq(false));
    220     }
    221 
    222     @Test
    223     public void test_getWallpaperAodDuration_when_shouldControlScreenOff() {
    224         TestableDozeParameters dozeParameters = new TestableDozeParameters(getContext());
    225         dozeParameters.setControlScreenOffAnimation(true);
    226         Assert.assertEquals("wallpaper hides faster when controlling screen off",
    227                 dozeParameters.getWallpaperAodDuration(),
    228                 DozeScreenState.ENTER_DOZE_HIDE_WALLPAPER_DELAY);
    229     }
    230 
    231     private class TestableDozeParameters extends DozeParameters {
    232         private PowerManager mPowerManager;
    233 
    234         TestableDozeParameters(Context context) {
    235             super(context);
    236             mPowerManager = mock(PowerManager.class);
    237         }
    238 
    239         @Override
    240         protected PowerManager getPowerManager() {
    241             return mPowerManager;
    242         }
    243     }
    244 
    245 }
    246