blob: cc743245fc42fb90c2768e9bbcde31adacf0ed70 [file] [log] [blame]
Jason Monkd5a204f2015-12-21 08:50:01 -05001/*
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 */
16package com.android.systemui.qs.external;
17
Jason Monkfe8f6822015-12-21 15:12:01 -050018import android.content.ComponentName;
Jason Monkd5a204f2015-12-21 08:50:01 -050019import android.os.Handler;
20import android.os.HandlerThread;
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040021import android.support.test.runner.AndroidJUnit4;
Jason Monk0c6e0992016-03-29 15:49:02 -040022import android.test.suitebuilder.annotation.SmallTest;
Jason Monkd5a204f2015-12-21 08:50:01 -050023import com.android.systemui.SysuiTestCase;
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040024import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
27import org.junit.runner.RunWith;
Jason Monkd5a204f2015-12-21 08:50:01 -050028import org.mockito.ArgumentCaptor;
29import org.mockito.Mockito;
30
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040031import static junit.framework.Assert.assertEquals;
32import static junit.framework.Assert.assertFalse;
33import static junit.framework.Assert.assertTrue;
34
Jason Monk0c6e0992016-03-29 15:49:02 -040035@SmallTest
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040036@RunWith(AndroidJUnit4.class)
Geoffrey Pitschfc2b64e2016-09-02 09:05:25 -040037public class TileServiceManagerTest extends SysuiTestCase {
Jason Monkd5a204f2015-12-21 08:50:01 -050038
39 private TileServices mTileServices;
40 private TileLifecycleManager mTileLifecycle;
41 private HandlerThread mThread;
42 private Handler mHandler;
43 private TileServiceManager mTileServiceManager;
44
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040045 @Before
46 public void setUp() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -050047 mThread = new HandlerThread("TestThread");
48 mThread.start();
Jason Monk6dceace2018-05-15 20:24:07 -040049 mHandler = Handler.createAsync(mThread.getLooper());
Jason Monkd5a204f2015-12-21 08:50:01 -050050 mTileServices = Mockito.mock(TileServices.class);
Jason Monkfe8f6822015-12-21 15:12:01 -050051 Mockito.when(mTileServices.getContext()).thenReturn(mContext);
Jason Monkd5a204f2015-12-21 08:50:01 -050052 mTileLifecycle = Mockito.mock(TileLifecycleManager.class);
Jason Monk97d22722016-04-07 11:41:47 -040053 Mockito.when(mTileLifecycle.isActiveTile()).thenReturn(false);
Jason Monkfe8f6822015-12-21 15:12:01 -050054 ComponentName componentName = new ComponentName(mContext,
Geoffrey Pitschfc2b64e2016-09-02 09:05:25 -040055 TileServiceManagerTest.class);
Jason Monkfe8f6822015-12-21 15:12:01 -050056 Mockito.when(mTileLifecycle.getComponent()).thenReturn(componentName);
Jason Monkd5a204f2015-12-21 08:50:01 -050057 mTileServiceManager = new TileServiceManager(mTileServices, mHandler, mTileLifecycle);
58 }
59
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040060 @After
61 public void tearDown() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -050062 mThread.quit();
63 }
64
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040065 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -050066 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
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040084 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -050085 public void testPendingClickPriority() {
86 Mockito.when(mTileLifecycle.hasPendingClick()).thenReturn(true);
87 mTileServiceManager.calculateBindPriority(0);
88 assertEquals(Integer.MAX_VALUE, mTileServiceManager.getBindPriority());
89 }
90
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040091 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -050092 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.
Jason Monk34a5cef2016-01-29 11:28:44 -0500105 assertEquals(Integer.MAX_VALUE - 2, mTileServiceManager.getBindPriority());
Jason Monkd5a204f2015-12-21 08:50:01 -0500106
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}