1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui.qs.customize; 16 17 import static junit.framework.Assert.assertEquals; 18 import static org.mockito.Mockito.any; 19 import static org.mockito.Mockito.mock; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import com.android.systemui.Dependency; 24 import android.testing.AndroidTestingRunner; 25 import com.android.systemui.SysuiTestCase; 26 import com.android.systemui.plugins.qs.QSTile; 27 import com.android.systemui.plugins.qs.QSTile.State; 28 import com.android.systemui.qs.QSTileHost; 29 import android.testing.TestableLooper; 30 import android.testing.TestableLooper.RunWithLooper; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 import android.os.Message; 37 import android.test.suitebuilder.annotation.SmallTest; 38 39 @SmallTest 40 @RunWith(AndroidTestingRunner.class) 41 @RunWithLooper 42 public class TileQueryHelperTest extends SysuiTestCase { 43 private TestableLooper mBGLooper; 44 private Runnable mLastCallback; 45 46 @Before 47 public void setup() { 48 mBGLooper = TestableLooper.get(this); 49 mDependency.injectTestDependency(Dependency.BG_LOOPER, mBGLooper.getLooper()); 50 } 51 52 @Test 53 public void testCompletionCalled() { 54 QSTileHost mockHost = mock(QSTileHost.class); 55 TileAdapter mockAdapter = mock(TileAdapter.class); 56 Runnable mockCompletion = mock(Runnable.class); 57 new TileQueryHelper(mContext, mockHost, mockAdapter, mockCompletion); 58 mBGLooper.processAllMessages(); 59 verify(mockCompletion).run(); 60 } 61 62 @Test 63 public void testCompletionCalledAfterTilesFetched() { 64 QSTile mockTile = mock(QSTile.class); 65 State mockState = mock(State.class); 66 when(mockState.copy()).thenReturn(mockState); 67 when(mockTile.getState()).thenReturn(mockState); 68 when(mockTile.isAvailable()).thenReturn(true); 69 70 QSTileHost mockHost = mock(QSTileHost.class); 71 when(mockHost.createTile(any())).thenReturn(mockTile); 72 73 mBGLooper.setMessageHandler((Message m) -> { 74 mLastCallback = m.getCallback(); 75 return true; 76 }); 77 TileAdapter mockAdapter = mock(TileAdapter.class); 78 Runnable mockCompletion = mock(Runnable.class); 79 new TileQueryHelper(mContext, mockHost, mockAdapter, mockCompletion); 80 81 // Verify that the last thing in the queue was our callback 82 mBGLooper.processAllMessages(); 83 assertEquals(mockCompletion, mLastCallback); 84 } 85 } 86