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