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