1 /*
2  * Copyright (C) 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  */
17 
18 package com.android.settings.slices;
19 
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.net.Uri;
27 import android.provider.SettingsSlicesContract;
28 
29 import androidx.slice.Slice;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.robolectric.RobolectricTestRunner;
35 import org.robolectric.RuntimeEnvironment;
36 
37 @RunWith(RobolectricTestRunner.class)
38 public class SpecialCaseSliceManagerTest {
39 
40     private final String FAKE_PARAMETER_KEY = "fake_parameter_key";
41     private final String FAKE_PARAMETER_VALUE = "fake_value";
42 
43     private Context mContext;
44 
45     @Before
setUp()46     public void setUp() {
47         mContext = RuntimeEnvironment.application;
48         CustomSliceRegistry.sUriToSlice.clear();
49         CustomSliceRegistry.sUriToSlice.put(FakeSliceable.URI, FakeSliceable.class);
50     }
51 
52     @Test
getSliceableFromUri_returnsCorrectObject()53     public void getSliceableFromUri_returnsCorrectObject() {
54         final CustomSliceable sliceable = CustomSliceable.createInstance(
55                 mContext, CustomSliceRegistry.getSliceClassByUri(FakeSliceable.URI));
56 
57         assertThat(sliceable).isInstanceOf(FakeSliceable.class);
58     }
59 
60     @Test
getSliceableFromUriWithParameter_returnsCorrectObject()61     public void getSliceableFromUriWithParameter_returnsCorrectObject() {
62         final Uri parameterUri = FakeSliceable.URI
63                 .buildUpon()
64                 .clearQuery()
65                 .appendQueryParameter(FAKE_PARAMETER_KEY, FAKE_PARAMETER_VALUE)
66                 .build();
67 
68         final CustomSliceable sliceable = CustomSliceable.createInstance(
69                 mContext, CustomSliceRegistry.getSliceClassByUri(parameterUri));
70 
71         assertThat(sliceable).isInstanceOf(FakeSliceable.class);
72     }
73 
74     @Test
isValidUri_validUri_returnsTrue()75     public void isValidUri_validUri_returnsTrue() {
76         final boolean isValidUri = CustomSliceRegistry.isValidUri(FakeSliceable.URI);
77 
78         assertThat(isValidUri).isTrue();
79     }
80 
81     @Test
isValidUri_invalidUri_returnsFalse()82     public void isValidUri_invalidUri_returnsFalse() {
83         final boolean isValidUri = CustomSliceRegistry.isValidUri(null);
84 
85         assertThat(isValidUri).isFalse();
86     }
87 
88     @Test
isValidAction_validActions_returnsTrue()89     public void isValidAction_validActions_returnsTrue() {
90         final boolean isValidAction =
91                 CustomSliceRegistry.isValidAction(FakeSliceable.URI.toString());
92 
93         assertThat(isValidAction).isTrue();
94     }
95 
96     @Test
isValidAction_invalidAction_returnsFalse()97     public void isValidAction_invalidAction_returnsFalse() {
98         final boolean isValidAction = CustomSliceRegistry.isValidAction("action");
99 
100         assertThat(isValidAction).isFalse();
101     }
102 
103     static class FakeSliceable implements CustomSliceable {
104 
105         static final String KEY = "magic key of khazad dum";
106 
107         static final Uri URI = new Uri.Builder()
108                 .scheme(ContentResolver.SCHEME_CONTENT)
109                 .authority(SettingsSliceProvider.SLICE_AUTHORITY)
110                 .appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
111                 .appendPath(KEY)
112                 .build();
113 
114         static final Slice SLICE = new Slice.Builder(URI).build();
115 
116         static boolean backingData = false;
117 
118         final Context mContext;
119 
FakeSliceable(Context context)120         public FakeSliceable(Context context) {
121             mContext = context;
122         }
123 
124         @Override
getSlice()125         public Slice getSlice() {
126             return SLICE;
127         }
128 
129         @Override
getUri()130         public Uri getUri() {
131             return URI;
132         }
133 
134         @Override
onNotifyChange(Intent intent)135         public void onNotifyChange(Intent intent) {
136             backingData = !backingData;
137         }
138 
139         @Override
getIntentFilter()140         public IntentFilter getIntentFilter() {
141             return new IntentFilter();
142         }
143 
144         @Override
getIntent()145         public Intent getIntent() {
146             return null;
147         }
148     }
149 }
150