Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.server.slice; |
| 16 | |
| 17 | import static android.content.ContentProvider.maybeAddUserId; |
| 18 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
| 19 | |
| 20 | import static org.mockito.ArgumentMatchers.anyInt; |
| 21 | import static org.mockito.ArgumentMatchers.eq; |
| 22 | import static org.mockito.Mockito.doReturn; |
| 23 | import static org.mockito.Mockito.mock; |
| 24 | import static org.mockito.Mockito.never; |
| 25 | import static org.mockito.Mockito.spy; |
| 26 | import static org.mockito.Mockito.times; |
| 27 | import static org.mockito.Mockito.verify; |
| 28 | import static org.mockito.Mockito.when; |
| 29 | |
| 30 | import android.app.AppOpsManager; |
| 31 | import android.app.slice.ISliceListener; |
| 32 | import android.app.slice.SliceSpec; |
| 33 | import android.content.pm.PackageManagerInternal; |
| 34 | import android.net.Uri; |
| 35 | import android.os.RemoteException; |
| 36 | import android.support.test.filters.SmallTest; |
| 37 | import android.testing.AndroidTestingRunner; |
| 38 | import android.testing.TestableLooper; |
| 39 | import android.testing.TestableLooper.RunWithLooper; |
| 40 | |
| 41 | import com.android.server.LocalServices; |
| 42 | import com.android.server.UiServiceTestCase; |
| 43 | |
| 44 | import org.junit.After; |
| 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
| 48 | |
| 49 | @SmallTest |
| 50 | @RunWith(AndroidTestingRunner.class) |
| 51 | @RunWithLooper |
| 52 | public class SliceManagerServiceTest extends UiServiceTestCase { |
| 53 | |
| 54 | private static final String AUTH = "com.android.services.uitests"; |
| 55 | private static final Uri TEST_URI = maybeAddUserId(Uri.parse("content://" + AUTH + "/path"), 0); |
| 56 | |
| 57 | private static final SliceSpec[] EMPTY_SPECS = new SliceSpec[]{ |
| 58 | }; |
| 59 | |
| 60 | private SliceManagerService mService; |
| 61 | private PinnedSliceState mCreatedSliceState; |
| 62 | |
| 63 | @Before |
| 64 | public void setup() { |
| 65 | LocalServices.addService(PackageManagerInternal.class, mock(PackageManagerInternal.class)); |
| 66 | mContext.addMockSystemService(AppOpsManager.class, mock(AppOpsManager.class)); |
| 67 | mContext.getTestablePermissions().setPermission(TEST_URI, PERMISSION_GRANTED); |
| 68 | |
| 69 | mService = spy(new SliceManagerService(mContext, TestableLooper.get(this).getLooper())); |
| 70 | mCreatedSliceState = mock(PinnedSliceState.class); |
| 71 | doReturn(mCreatedSliceState).when(mService).createPinnedSlice(eq(TEST_URI)); |
| 72 | } |
| 73 | |
| 74 | @After |
| 75 | public void teardown() { |
| 76 | LocalServices.removeServiceForTest(PackageManagerInternal.class); |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | public void testAddListenerCreatesPinned() throws RemoteException { |
| 81 | mService.addSliceListener(TEST_URI, "pkg", mock(ISliceListener.class), EMPTY_SPECS); |
| 82 | verify(mService, times(1)).createPinnedSlice(eq(TEST_URI)); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testAddListenerCreatesOnePinned() throws RemoteException { |
| 87 | mService.addSliceListener(TEST_URI, "pkg", mock(ISliceListener.class), EMPTY_SPECS); |
| 88 | mService.addSliceListener(TEST_URI, "pkg", mock(ISliceListener.class), EMPTY_SPECS); |
| 89 | verify(mService, times(1)).createPinnedSlice(eq(TEST_URI)); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testRemoveListenerDestroysPinned() throws RemoteException { |
| 94 | ISliceListener listener = mock(ISliceListener.class); |
| 95 | mService.addSliceListener(TEST_URI, "pkg", listener, EMPTY_SPECS); |
| 96 | |
| 97 | when(mCreatedSliceState.removeSliceListener(eq(listener))).thenReturn(false); |
| 98 | mService.removeSliceListener(TEST_URI, "pkg", listener); |
| 99 | verify(mCreatedSliceState, never()).destroy(); |
| 100 | |
| 101 | when(mCreatedSliceState.removeSliceListener(eq(listener))).thenReturn(true); |
| 102 | mService.removeSliceListener(TEST_URI, "pkg", listener); |
| 103 | verify(mCreatedSliceState).destroy(); |
| 104 | } |
| 105 | |
| 106 | @Test(expected = IllegalStateException.class) |
| 107 | public void testUnrecognizedThrows() throws RemoteException { |
| 108 | mService.removeSliceListener(TEST_URI, "pkg", mock(ISliceListener.class)); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void testAddPinCreatesPinned() throws RemoteException { |
| 113 | doReturn("pkg").when(mService).getDefaultHome(anyInt()); |
| 114 | |
| 115 | mService.pinSlice("pkg", TEST_URI, EMPTY_SPECS); |
| 116 | mService.pinSlice("pkg", TEST_URI, EMPTY_SPECS); |
| 117 | verify(mService, times(1)).createPinnedSlice(eq(TEST_URI)); |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void testRemovePinDestroysPinned() throws RemoteException { |
| 122 | doReturn("pkg").when(mService).getDefaultHome(anyInt()); |
| 123 | |
| 124 | mService.pinSlice("pkg", TEST_URI, EMPTY_SPECS); |
| 125 | |
| 126 | when(mCreatedSliceState.unpin(eq("pkg"))).thenReturn(false); |
| 127 | mService.unpinSlice("pkg", TEST_URI); |
| 128 | verify(mCreatedSliceState, never()).destroy(); |
| 129 | |
| 130 | when(mCreatedSliceState.unpin(eq("pkg"))).thenReturn(true); |
| 131 | mService.unpinSlice("pkg", TEST_URI); |
| 132 | verify(mCreatedSliceState).destroy(); |
| 133 | } |
| 134 | |
| 135 | } |