Home | History | Annotate | Download | only in external
      1 /*
      2  * Copyright (C) 2015 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.systemui.qs.external;
     17 
     18 import android.content.ComponentName;
     19 import android.os.Handler;
     20 import android.os.HandlerThread;
     21 import android.support.test.runner.AndroidJUnit4;
     22 import android.test.suitebuilder.annotation.SmallTest;
     23 import com.android.systemui.SysuiTestCase;
     24 import org.junit.After;
     25 import org.junit.Before;
     26 import org.junit.Test;
     27 import org.junit.runner.RunWith;
     28 import org.mockito.ArgumentCaptor;
     29 import org.mockito.Mockito;
     30 
     31 import static junit.framework.Assert.assertEquals;
     32 import static junit.framework.Assert.assertFalse;
     33 import static junit.framework.Assert.assertTrue;
     34 
     35 @SmallTest
     36 @RunWith(AndroidJUnit4.class)
     37 public class TileServiceManagerTest extends SysuiTestCase {
     38 
     39     private TileServices mTileServices;
     40     private TileLifecycleManager mTileLifecycle;
     41     private HandlerThread mThread;
     42     private Handler mHandler;
     43     private TileServiceManager mTileServiceManager;
     44 
     45     @Before
     46     public void setUp() throws Exception {
     47         mThread = new HandlerThread("TestThread");
     48         mThread.start();
     49         mHandler = Handler.createAsync(mThread.getLooper());
     50         mTileServices = Mockito.mock(TileServices.class);
     51         Mockito.when(mTileServices.getContext()).thenReturn(mContext);
     52         mTileLifecycle = Mockito.mock(TileLifecycleManager.class);
     53         Mockito.when(mTileLifecycle.isActiveTile()).thenReturn(false);
     54         ComponentName componentName = new ComponentName(mContext,
     55                 TileServiceManagerTest.class);
     56         Mockito.when(mTileLifecycle.getComponent()).thenReturn(componentName);
     57         mTileServiceManager = new TileServiceManager(mTileServices, mHandler, mTileLifecycle);
     58     }
     59 
     60     @After
     61     public void tearDown() throws Exception {
     62         mThread.quit();
     63     }
     64 
     65     @Test
     66     public void testSetBindRequested() {
     67         // Request binding.
     68         mTileServiceManager.setBindRequested(true);
     69         mTileServiceManager.setLastUpdate(0);
     70         mTileServiceManager.calculateBindPriority(5);
     71         Mockito.verify(mTileServices, Mockito.times(2)).recalculateBindAllowance();
     72         assertEquals(5, mTileServiceManager.getBindPriority());
     73 
     74         // Verify same state doesn't trigger recalculating for no reason.
     75         mTileServiceManager.setBindRequested(true);
     76         Mockito.verify(mTileServices, Mockito.times(2)).recalculateBindAllowance();
     77 
     78         mTileServiceManager.setBindRequested(false);
     79         mTileServiceManager.calculateBindPriority(5);
     80         Mockito.verify(mTileServices, Mockito.times(3)).recalculateBindAllowance();
     81         assertEquals(Integer.MIN_VALUE, mTileServiceManager.getBindPriority());
     82     }
     83 
     84     @Test
     85     public void testPendingClickPriority() {
     86         Mockito.when(mTileLifecycle.hasPendingClick()).thenReturn(true);
     87         mTileServiceManager.calculateBindPriority(0);
     88         assertEquals(Integer.MAX_VALUE, mTileServiceManager.getBindPriority());
     89     }
     90 
     91     @Test
     92     public void testBind() {
     93         // Trigger binding requested and allowed.
     94         mTileServiceManager.setBindRequested(true);
     95         mTileServiceManager.setBindAllowed(true);
     96 
     97         ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class);
     98         Mockito.verify(mTileLifecycle, Mockito.times(1)).setBindService(captor.capture());
     99         assertTrue((boolean) captor.getValue());
    100 
    101         mTileServiceManager.setBindRequested(false);
    102         mTileServiceManager.calculateBindPriority(0);
    103         // Priority shouldn't disappear after the request goes away if we just bound, instead
    104         // it sticks around to avoid thrashing a bunch of processes.
    105         assertEquals(Integer.MAX_VALUE - 2, mTileServiceManager.getBindPriority());
    106 
    107         mTileServiceManager.setBindAllowed(false);
    108         captor = ArgumentCaptor.forClass(Boolean.class);
    109         Mockito.verify(mTileLifecycle, Mockito.times(2)).setBindService(captor.capture());
    110         assertFalse((boolean) captor.getValue());
    111     }
    112 }
    113