1/*
2 * Copyright 2018 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 */
16
17package androidx.slice.compat;
18
19import static androidx.core.content.PermissionChecker.PERMISSION_DENIED;
20import static androidx.core.content.PermissionChecker.PERMISSION_GRANTED;
21import static androidx.slice.compat.SliceProviderCompat.EXTRA_BIND_URI;
22import static androidx.slice.compat.SliceProviderCompat.EXTRA_SLICE;
23
24import static org.junit.Assert.assertEquals;
25import static org.junit.Assert.assertNotEquals;
26import static org.mockito.ArgumentMatchers.any;
27import static org.mockito.ArgumentMatchers.anyInt;
28import static org.mockito.ArgumentMatchers.eq;
29import static org.mockito.Mockito.mock;
30import static org.mockito.Mockito.spy;
31import static org.mockito.Mockito.when;
32
33import android.content.ContentResolver;
34import android.content.Context;
35import android.net.Uri;
36import android.os.Bundle;
37import android.support.test.InstrumentationRegistry;
38import android.support.test.filters.SmallTest;
39import android.support.test.runner.AndroidJUnit4;
40
41import androidx.slice.Slice;
42import androidx.slice.SliceProvider;
43import androidx.slice.SliceSpec;
44
45import org.junit.Test;
46import org.junit.runner.RunWith;
47
48import java.util.Collections;
49
50@RunWith(AndroidJUnit4.class)
51@SmallTest
52public class SliceProviderCompatTest {
53
54    private final Context mContext = InstrumentationRegistry.getContext();
55
56    @Test
57    public void testBindWithPermission() {
58        Uri uri = new Uri.Builder()
59                .scheme(ContentResolver.SCHEME_CONTENT)
60                .authority("my.authority")
61                .path("my_path")
62                .build();
63        Slice s = new Slice.Builder(uri)
64                .addText("", null)
65                .build();
66
67        SliceProvider provider = spy(new SliceProviderImpl());
68        CompatPermissionManager permissions = mock(CompatPermissionManager.class);
69        when(permissions.checkSlicePermission(any(Uri.class), anyInt(), anyInt()))
70                .thenReturn(PERMISSION_GRANTED);
71
72        when(provider.onBindSlice(eq(uri))).thenReturn(s);
73        SliceProviderCompat compat = new SliceProviderCompat(provider, permissions,
74                mContext) {
75            @Override
76            public String getCallingPackage() {
77                return mContext.getPackageName();
78            }
79        };
80
81        Bundle b = new Bundle();
82        b.putParcelable(EXTRA_BIND_URI, uri);
83        SliceProviderCompat.addSpecs(b, Collections.<SliceSpec>emptySet());
84
85        Bundle result = compat.call(SliceProviderCompat.METHOD_SLICE, null, b);
86        assertEquals(s.toString(), new Slice(result.getBundle(EXTRA_SLICE)).toString());
87    }
88
89    @Test
90    public void testBindWithoutPermission() {
91        Uri uri = new Uri.Builder()
92                .scheme(ContentResolver.SCHEME_CONTENT)
93                .authority("my.authority")
94                .path("my_path")
95                .build();
96        Slice s = new Slice.Builder(uri)
97                .addText("", null)
98                .build();
99
100        SliceProvider provider = spy(new SliceProviderImpl());
101        CompatPermissionManager permissions = mock(CompatPermissionManager.class);
102        when(permissions.checkSlicePermission(any(Uri.class), anyInt(), anyInt()))
103                .thenReturn(PERMISSION_DENIED);
104
105        when(provider.onBindSlice(eq(uri))).thenReturn(s);
106        SliceProviderCompat compat = new SliceProviderCompat(provider, permissions,
107                mContext) {
108            @Override
109            public String getCallingPackage() {
110                return mContext.getPackageName();
111            }
112        };
113
114        Bundle b = new Bundle();
115        b.putParcelable(EXTRA_BIND_URI, uri);
116        SliceProviderCompat.addSpecs(b, Collections.<SliceSpec>emptySet());
117
118        Bundle result = compat.call(SliceProviderCompat.METHOD_SLICE, null, b);
119        assertNotEquals(s.toString(), new Slice(result.getBundle(EXTRA_SLICE)).toString());
120    }
121
122    public static class SliceProviderImpl extends SliceProvider {
123
124        @Override
125        public boolean onCreateSliceProvider() {
126            return true;
127        }
128
129        @Override
130        public Slice onBindSlice(Uri sliceUri) {
131            return null;
132        }
133    }
134}
135