blob: 683e8f4a2511a4d307140be8b8e4a638b5298cac [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
Brett Chabot84151d92019-02-27 15:37:59 -080018import static junit.framework.Assert.assertEquals;
19import static junit.framework.Assert.assertFalse;
20import static junit.framework.Assert.assertTrue;
21
Jason Monkfe8f6822015-12-21 15:12:01 -050022import android.content.ComponentName;
Jason Monkd5a204f2015-12-21 08:50:01 -050023import android.os.Handler;
24import android.os.HandlerThread;
Jason Monk0c6e0992016-03-29 15:49:02 -040025import android.test.suitebuilder.annotation.SmallTest;
Brett Chabot84151d92019-02-27 15:37:59 -080026
27import androidx.test.runner.AndroidJUnit4;
28
Jason Monkd5a204f2015-12-21 08:50:01 -050029import com.android.systemui.SysuiTestCase;
Brett Chabot84151d92019-02-27 15:37:59 -080030
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040031import org.junit.After;
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
Jason Monkd5a204f2015-12-21 08:50:01 -050035import org.mockito.ArgumentCaptor;
36import org.mockito.Mockito;
37
Jason Monk0c6e0992016-03-29 15:49:02 -040038@SmallTest
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040039@RunWith(AndroidJUnit4.class)
Geoffrey Pitschfc2b64e2016-09-02 09:05:25 -040040public class TileServiceManagerTest extends SysuiTestCase {
Jason Monkd5a204f2015-12-21 08:50:01 -050041
42 private TileServices mTileServices;
43 private TileLifecycleManager mTileLifecycle;
44 private HandlerThread mThread;
45 private Handler mHandler;
46 private TileServiceManager mTileServiceManager;
47
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040048 @Before
49 public void setUp() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -050050 mThread = new HandlerThread("TestThread");
51 mThread.start();
Jason Monk6dceace2018-05-15 20:24:07 -040052 mHandler = Handler.createAsync(mThread.getLooper());
Jason Monkd5a204f2015-12-21 08:50:01 -050053 mTileServices = Mockito.mock(TileServices.class);
Jason Monkfe8f6822015-12-21 15:12:01 -050054 Mockito.when(mTileServices.getContext()).thenReturn(mContext);
Jason Monkd5a204f2015-12-21 08:50:01 -050055 mTileLifecycle = Mockito.mock(TileLifecycleManager.class);
Jason Monk97d22722016-04-07 11:41:47 -040056 Mockito.when(mTileLifecycle.isActiveTile()).thenReturn(false);
Jason Monkfe8f6822015-12-21 15:12:01 -050057 ComponentName componentName = new ComponentName(mContext,
Geoffrey Pitschfc2b64e2016-09-02 09:05:25 -040058 TileServiceManagerTest.class);
Jason Monkfe8f6822015-12-21 15:12:01 -050059 Mockito.when(mTileLifecycle.getComponent()).thenReturn(componentName);
Jason Monkd5a204f2015-12-21 08:50:01 -050060 mTileServiceManager = new TileServiceManager(mTileServices, mHandler, mTileLifecycle);
61 }
62
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040063 @After
64 public void tearDown() throws Exception {
Jason Monkd5a204f2015-12-21 08:50:01 -050065 mThread.quit();
66 }
67
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040068 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -050069 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 Pitsch2c403db2016-08-26 09:09:39 -040087 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -050088 public void testPendingClickPriority() {
89 Mockito.when(mTileLifecycle.hasPendingClick()).thenReturn(true);
90 mTileServiceManager.calculateBindPriority(0);
91 assertEquals(Integer.MAX_VALUE, mTileServiceManager.getBindPriority());
92 }
93
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040094 @Test
Jason Monkd5a204f2015-12-21 08:50:01 -050095 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 Monk34a5cef2016-01-29 11:28:44 -0500108 assertEquals(Integer.MAX_VALUE - 2, mTileServiceManager.getBindPriority());
Jason Monkd5a204f2015-12-21 08:50:01 -0500109
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}