Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 1 | package com.android.server.slice; |
| 2 | |
Jason Monk | e1c0c2c | 2018-07-03 11:08:32 -0400 | [diff] [blame] | 3 | import static android.testing.TestableContentResolver.UNSTABLE; |
| 4 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 5 | import static org.junit.Assert.assertArrayEquals; |
| 6 | import static org.junit.Assert.assertEquals; |
| 7 | import static org.junit.Assert.assertFalse; |
| 8 | import static org.junit.Assert.assertNull; |
| 9 | import static org.junit.Assert.assertTrue; |
Jason Monk | 9e3b864 | 2018-01-21 20:54:00 -0500 | [diff] [blame] | 10 | import static org.mockito.ArgumentMatchers.anyInt; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 11 | import static org.mockito.ArgumentMatchers.anyString; |
| 12 | import static org.mockito.ArgumentMatchers.argThat; |
| 13 | import static org.mockito.ArgumentMatchers.eq; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 14 | import static org.mockito.Mockito.mock; |
| 15 | import static org.mockito.Mockito.verify; |
| 16 | import static org.mockito.Mockito.when; |
| 17 | |
| 18 | import android.app.slice.ISliceListener; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 19 | import android.app.slice.SliceProvider; |
| 20 | import android.app.slice.SliceSpec; |
| 21 | import android.content.ContentProvider; |
| 22 | import android.content.IContentProvider; |
| 23 | import android.net.Uri; |
Jason Monk | 9e3b864 | 2018-01-21 20:54:00 -0500 | [diff] [blame] | 24 | import android.os.Binder; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 25 | import android.os.Handler; |
Jason Monk | 9e3b864 | 2018-01-21 20:54:00 -0500 | [diff] [blame] | 26 | import android.os.IBinder; |
| 27 | import android.os.IBinder.DeathRecipient; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 28 | import android.os.RemoteException; |
| 29 | import android.support.test.filters.SmallTest; |
| 30 | import android.testing.AndroidTestingRunner; |
| 31 | import android.testing.TestableLooper; |
| 32 | import android.testing.TestableLooper.RunWithLooper; |
| 33 | |
| 34 | import com.android.server.UiServiceTestCase; |
| 35 | |
| 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | import org.junit.runner.RunWith; |
Jason Monk | 9e3b864 | 2018-01-21 20:54:00 -0500 | [diff] [blame] | 39 | import org.mockito.ArgumentCaptor; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 40 | |
| 41 | @SmallTest |
| 42 | @RunWith(AndroidTestingRunner.class) |
| 43 | @RunWithLooper |
| 44 | public class PinnedSliceStateTest extends UiServiceTestCase { |
| 45 | |
| 46 | private static final String AUTH = "my.authority"; |
| 47 | private static final Uri TEST_URI = Uri.parse("content://" + AUTH + "/path"); |
| 48 | |
| 49 | private static final SliceSpec[] FIRST_SPECS = new SliceSpec[]{ |
| 50 | new SliceSpec("spec1", 3), |
| 51 | new SliceSpec("spec2", 3), |
| 52 | new SliceSpec("spec3", 2), |
| 53 | new SliceSpec("spec4", 1), |
| 54 | }; |
| 55 | |
| 56 | private static final SliceSpec[] SECOND_SPECS = new SliceSpec[]{ |
| 57 | new SliceSpec("spec2", 1), |
| 58 | new SliceSpec("spec3", 2), |
| 59 | new SliceSpec("spec4", 3), |
| 60 | new SliceSpec("spec5", 4), |
| 61 | }; |
| 62 | |
| 63 | private SliceManagerService mSliceService; |
| 64 | private PinnedSliceState mPinnedSliceManager; |
| 65 | private IContentProvider mIContentProvider; |
| 66 | private ContentProvider mContentProvider; |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 67 | private IBinder mToken = new Binder(); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 68 | |
| 69 | @Before |
| 70 | public void setup() { |
| 71 | mSliceService = mock(SliceManagerService.class); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 72 | when(mSliceService.getContext()).thenReturn(mContext); |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 73 | when(mSliceService.getLock()).thenReturn(new Object()); |
Jason Monk | e1c0c2c | 2018-07-03 11:08:32 -0400 | [diff] [blame] | 74 | when(mSliceService.getHandler()).thenReturn( |
| 75 | new Handler(TestableLooper.get(this).getLooper())); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 76 | mContentProvider = mock(ContentProvider.class); |
| 77 | mIContentProvider = mock(IContentProvider.class); |
| 78 | when(mContentProvider.getIContentProvider()).thenReturn(mIContentProvider); |
Jason Monk | e1c0c2c | 2018-07-03 11:08:32 -0400 | [diff] [blame] | 79 | mContext.getContentResolver().addProvider(AUTH, mContentProvider, UNSTABLE); |
Jason Monk | f88d25e | 2018-03-06 20:13:24 -0500 | [diff] [blame] | 80 | mPinnedSliceManager = new PinnedSliceState(mSliceService, TEST_URI, "pkg"); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testMergeSpecs() { |
| 85 | // No annotations to start. |
| 86 | assertNull(mPinnedSliceManager.getSpecs()); |
| 87 | |
| 88 | mPinnedSliceManager.mergeSpecs(FIRST_SPECS); |
| 89 | assertArrayEquals(FIRST_SPECS, mPinnedSliceManager.getSpecs()); |
| 90 | |
| 91 | mPinnedSliceManager.mergeSpecs(SECOND_SPECS); |
| 92 | assertArrayEquals(new SliceSpec[]{ |
| 93 | // spec1 is gone because it's not in the second set. |
| 94 | new SliceSpec("spec2", 1), // spec2 is 1 because it's smaller in the second set. |
| 95 | new SliceSpec("spec3", 2), // spec3 is the same in both sets |
| 96 | new SliceSpec("spec4", 1), // spec4 is 1 because it's smaller in the first set. |
| 97 | // spec5 is gone because it's not in the first set. |
| 98 | }, mPinnedSliceManager.getSpecs()); |
| 99 | } |
| 100 | |
| 101 | @Test |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 102 | public void testSendPinnedOnPin() throws RemoteException { |
| 103 | TestableLooper.get(this).processAllMessages(); |
| 104 | |
| 105 | // When pinned for the first time, a pinned message should be sent. |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 106 | mPinnedSliceManager.pin("pkg", FIRST_SPECS, mToken); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 107 | TestableLooper.get(this).processAllMessages(); |
| 108 | |
Jeff Sharkey | 633a13e | 2018-12-07 12:00:45 -0700 | [diff] [blame^] | 109 | verify(mIContentProvider).call(anyString(), anyString(), eq(SliceProvider.METHOD_PIN), |
| 110 | eq(null), argThat(b -> { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 111 | assertEquals(TEST_URI, b.getParcelable(SliceProvider.EXTRA_BIND_URI)); |
| 112 | return true; |
| 113 | })); |
| 114 | } |
| 115 | |
| 116 | @Test |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 117 | public void testPkgPin() { |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 118 | assertFalse(mPinnedSliceManager.hasPinOrListener()); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 119 | |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 120 | mPinnedSliceManager.pin("pkg", FIRST_SPECS, mToken); |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 121 | assertTrue(mPinnedSliceManager.hasPinOrListener()); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 122 | |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 123 | assertTrue(mPinnedSliceManager.unpin("pkg", mToken)); |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 124 | assertFalse(mPinnedSliceManager.hasPinOrListener()); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void testMultiPkgPin() { |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 129 | IBinder t2 = new Binder(); |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 130 | assertFalse(mPinnedSliceManager.hasPinOrListener()); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 131 | |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 132 | mPinnedSliceManager.pin("pkg", FIRST_SPECS, mToken); |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 133 | assertTrue(mPinnedSliceManager.hasPinOrListener()); |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 134 | mPinnedSliceManager.pin("pkg2", FIRST_SPECS, t2); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 135 | |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 136 | assertFalse(mPinnedSliceManager.unpin("pkg", mToken)); |
| 137 | assertTrue(mPinnedSliceManager.unpin("pkg2", t2)); |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 138 | assertFalse(mPinnedSliceManager.hasPinOrListener()); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Test |
Jason Monk | 9e3b864 | 2018-01-21 20:54:00 -0500 | [diff] [blame] | 142 | public void testListenerDeath() throws RemoteException { |
| 143 | ISliceListener listener = mock(ISliceListener.class); |
| 144 | IBinder binder = mock(IBinder.class); |
| 145 | when(binder.isBinderAlive()).thenReturn(true); |
| 146 | when(listener.asBinder()).thenReturn(binder); |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 147 | assertFalse(mPinnedSliceManager.hasPinOrListener()); |
Jason Monk | 9e3b864 | 2018-01-21 20:54:00 -0500 | [diff] [blame] | 148 | |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 149 | mPinnedSliceManager.pin(mContext.getPackageName(), FIRST_SPECS, binder); |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 150 | assertTrue(mPinnedSliceManager.hasPinOrListener()); |
Jason Monk | 9e3b864 | 2018-01-21 20:54:00 -0500 | [diff] [blame] | 151 | |
| 152 | ArgumentCaptor<DeathRecipient> arg = ArgumentCaptor.forClass(DeathRecipient.class); |
| 153 | verify(binder).linkToDeath(arg.capture(), anyInt()); |
| 154 | |
| 155 | when(binder.isBinderAlive()).thenReturn(false); |
| 156 | arg.getValue().binderDied(); |
| 157 | |
| 158 | verify(mSliceService).removePinnedSlice(eq(TEST_URI)); |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 159 | assertFalse(mPinnedSliceManager.hasPinOrListener()); |
Jason Monk | 9e3b864 | 2018-01-21 20:54:00 -0500 | [diff] [blame] | 160 | } |
Jason Monk | f88d25e | 2018-03-06 20:13:24 -0500 | [diff] [blame] | 161 | } |