blob: a71aca53ca2610d48c6d2a65258d97e90fa537f8 [file] [log] [blame]
Jason Monk74f5e362017-12-06 08:56:33 -05001package com.android.server.slice;
2
Jason Monke1c0c2c2018-07-03 11:08:32 -04003import static android.testing.TestableContentResolver.UNSTABLE;
4
Jason Monk74f5e362017-12-06 08:56:33 -05005import static org.junit.Assert.assertArrayEquals;
6import static org.junit.Assert.assertEquals;
7import static org.junit.Assert.assertFalse;
8import static org.junit.Assert.assertNull;
9import static org.junit.Assert.assertTrue;
Jason Monk9e3b8642018-01-21 20:54:00 -050010import static org.mockito.ArgumentMatchers.anyInt;
Jason Monk74f5e362017-12-06 08:56:33 -050011import static org.mockito.ArgumentMatchers.anyString;
12import static org.mockito.ArgumentMatchers.argThat;
13import static org.mockito.ArgumentMatchers.eq;
Jason Monk74f5e362017-12-06 08:56:33 -050014import static org.mockito.Mockito.mock;
15import static org.mockito.Mockito.verify;
16import static org.mockito.Mockito.when;
17
18import android.app.slice.ISliceListener;
Jason Monk74f5e362017-12-06 08:56:33 -050019import android.app.slice.SliceProvider;
20import android.app.slice.SliceSpec;
21import android.content.ContentProvider;
22import android.content.IContentProvider;
23import android.net.Uri;
Jason Monk9e3b8642018-01-21 20:54:00 -050024import android.os.Binder;
Jason Monk74f5e362017-12-06 08:56:33 -050025import android.os.Handler;
Jason Monk9e3b8642018-01-21 20:54:00 -050026import android.os.IBinder;
27import android.os.IBinder.DeathRecipient;
Jason Monk74f5e362017-12-06 08:56:33 -050028import android.os.RemoteException;
29import android.support.test.filters.SmallTest;
30import android.testing.AndroidTestingRunner;
31import android.testing.TestableLooper;
32import android.testing.TestableLooper.RunWithLooper;
33
34import com.android.server.UiServiceTestCase;
35
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
Jason Monk9e3b8642018-01-21 20:54:00 -050039import org.mockito.ArgumentCaptor;
Jason Monk74f5e362017-12-06 08:56:33 -050040
41@SmallTest
42@RunWith(AndroidTestingRunner.class)
43@RunWithLooper
44public 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 Monk38df2802018-02-22 19:28:12 -050067 private IBinder mToken = new Binder();
Jason Monk74f5e362017-12-06 08:56:33 -050068
69 @Before
70 public void setup() {
71 mSliceService = mock(SliceManagerService.class);
Jason Monk74f5e362017-12-06 08:56:33 -050072 when(mSliceService.getContext()).thenReturn(mContext);
Jason Monk3a1d2e972018-01-29 16:58:11 -050073 when(mSliceService.getLock()).thenReturn(new Object());
Jason Monke1c0c2c2018-07-03 11:08:32 -040074 when(mSliceService.getHandler()).thenReturn(
75 new Handler(TestableLooper.get(this).getLooper()));
Jason Monk74f5e362017-12-06 08:56:33 -050076 mContentProvider = mock(ContentProvider.class);
77 mIContentProvider = mock(IContentProvider.class);
78 when(mContentProvider.getIContentProvider()).thenReturn(mIContentProvider);
Jason Monke1c0c2c2018-07-03 11:08:32 -040079 mContext.getContentResolver().addProvider(AUTH, mContentProvider, UNSTABLE);
Jason Monkf88d25e2018-03-06 20:13:24 -050080 mPinnedSliceManager = new PinnedSliceState(mSliceService, TEST_URI, "pkg");
Jason Monk74f5e362017-12-06 08:56:33 -050081 }
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 Monk3a1d2e972018-01-29 16:58:11 -0500102 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 Monk38df2802018-02-22 19:28:12 -0500106 mPinnedSliceManager.pin("pkg", FIRST_SPECS, mToken);
Jason Monk74f5e362017-12-06 08:56:33 -0500107 TestableLooper.get(this).processAllMessages();
108
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700109 verify(mIContentProvider).call(anyString(), anyString(), eq(SliceProvider.METHOD_PIN),
110 eq(null), argThat(b -> {
Jason Monk74f5e362017-12-06 08:56:33 -0500111 assertEquals(TEST_URI, b.getParcelable(SliceProvider.EXTRA_BIND_URI));
112 return true;
113 }));
114 }
115
116 @Test
Jason Monk74f5e362017-12-06 08:56:33 -0500117 public void testPkgPin() {
Jason Monk3a1d2e972018-01-29 16:58:11 -0500118 assertFalse(mPinnedSliceManager.hasPinOrListener());
Jason Monk74f5e362017-12-06 08:56:33 -0500119
Jason Monk38df2802018-02-22 19:28:12 -0500120 mPinnedSliceManager.pin("pkg", FIRST_SPECS, mToken);
Jason Monk3a1d2e972018-01-29 16:58:11 -0500121 assertTrue(mPinnedSliceManager.hasPinOrListener());
Jason Monk74f5e362017-12-06 08:56:33 -0500122
Jason Monk38df2802018-02-22 19:28:12 -0500123 assertTrue(mPinnedSliceManager.unpin("pkg", mToken));
Jason Monk3a1d2e972018-01-29 16:58:11 -0500124 assertFalse(mPinnedSliceManager.hasPinOrListener());
Jason Monk74f5e362017-12-06 08:56:33 -0500125 }
126
127 @Test
128 public void testMultiPkgPin() {
Jason Monk38df2802018-02-22 19:28:12 -0500129 IBinder t2 = new Binder();
Jason Monk3a1d2e972018-01-29 16:58:11 -0500130 assertFalse(mPinnedSliceManager.hasPinOrListener());
Jason Monk74f5e362017-12-06 08:56:33 -0500131
Jason Monk38df2802018-02-22 19:28:12 -0500132 mPinnedSliceManager.pin("pkg", FIRST_SPECS, mToken);
Jason Monk3a1d2e972018-01-29 16:58:11 -0500133 assertTrue(mPinnedSliceManager.hasPinOrListener());
Jason Monk38df2802018-02-22 19:28:12 -0500134 mPinnedSliceManager.pin("pkg2", FIRST_SPECS, t2);
Jason Monk74f5e362017-12-06 08:56:33 -0500135
Jason Monk38df2802018-02-22 19:28:12 -0500136 assertFalse(mPinnedSliceManager.unpin("pkg", mToken));
137 assertTrue(mPinnedSliceManager.unpin("pkg2", t2));
Jason Monk3a1d2e972018-01-29 16:58:11 -0500138 assertFalse(mPinnedSliceManager.hasPinOrListener());
Jason Monk74f5e362017-12-06 08:56:33 -0500139 }
140
141 @Test
Jason Monk9e3b8642018-01-21 20:54:00 -0500142 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 Monk3a1d2e972018-01-29 16:58:11 -0500147 assertFalse(mPinnedSliceManager.hasPinOrListener());
Jason Monk9e3b8642018-01-21 20:54:00 -0500148
Jason Monk38df2802018-02-22 19:28:12 -0500149 mPinnedSliceManager.pin(mContext.getPackageName(), FIRST_SPECS, binder);
Jason Monk3a1d2e972018-01-29 16:58:11 -0500150 assertTrue(mPinnedSliceManager.hasPinOrListener());
Jason Monk9e3b8642018-01-21 20:54:00 -0500151
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 Monk3a1d2e972018-01-29 16:58:11 -0500159 assertFalse(mPinnedSliceManager.hasPinOrListener());
Jason Monk9e3b8642018-01-21 20:54:00 -0500160 }
Jason Monkf88d25e2018-03-06 20:13:24 -0500161}