Home | History | Annotate | Download | only in compat
      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 
     17 package androidx.slice.compat;
     18 
     19 import static android.content.Context.MODE_PRIVATE;
     20 
     21 import static junit.framework.Assert.assertEquals;
     22 import static junit.framework.Assert.assertFalse;
     23 import static junit.framework.Assert.assertTrue;
     24 
     25 import android.content.Context;
     26 import android.net.Uri;
     27 import android.support.test.InstrumentationRegistry;
     28 import android.support.test.filters.SmallTest;
     29 import android.support.test.runner.AndroidJUnit4;
     30 
     31 import androidx.collection.ArraySet;
     32 import androidx.slice.SliceSpec;
     33 
     34 import junit.framework.AssertionFailedError;
     35 
     36 import org.junit.After;
     37 import org.junit.Before;
     38 import org.junit.Test;
     39 import org.junit.runner.RunWith;
     40 
     41 import java.util.Arrays;
     42 import java.util.Collections;
     43 import java.util.Set;
     44 
     45 @RunWith(AndroidJUnit4.class)
     46 @SmallTest
     47 public class CompatPinnedListTest {
     48 
     49     private final Context mContext = InstrumentationRegistry.getContext();
     50     private CompatPinnedList mCompatPinnedList;
     51     private Set<SliceSpec> mSpecs;
     52 
     53     private static final SliceSpec[] FIRST_SPECS = new SliceSpec[]{
     54             new SliceSpec("spec1", 3),
     55             new SliceSpec("spec2", 3),
     56             new SliceSpec("spec3", 2),
     57             new SliceSpec("spec4", 1),
     58     };
     59 
     60     private static final SliceSpec[] SECOND_SPECS = new SliceSpec[]{
     61             new SliceSpec("spec2", 1),
     62             new SliceSpec("spec3", 2),
     63             new SliceSpec("spec4", 3),
     64             new SliceSpec("spec5", 4),
     65     };
     66 
     67     @Before
     68     public void setup() {
     69         mCompatPinnedList = new CompatPinnedList(mContext, "test_file");
     70         mSpecs = Collections.emptySet();
     71     }
     72 
     73     @After
     74     public void tearDown() {
     75         mContext.getSharedPreferences("test_file", MODE_PRIVATE).edit().clear().commit();
     76     }
     77 
     78     @Test
     79     public void testAddFirstPin() {
     80         assertTrue(mCompatPinnedList.addPin(Uri.parse("content://something/something"), "my_pkg",
     81                 mSpecs));
     82     }
     83 
     84     @Test
     85     public void testAddSecondPin() {
     86         assertTrue(mCompatPinnedList.addPin(Uri.parse("content://something/something"), "my_pkg",
     87                 mSpecs));
     88         assertFalse(mCompatPinnedList.addPin(Uri.parse("content://something/something"), "my_pkg2",
     89                 mSpecs));
     90     }
     91 
     92     @Test
     93     public void testAddMultipleUris() {
     94         assertTrue(mCompatPinnedList.addPin(Uri.parse("content://something/something"), "my_pkg",
     95                 mSpecs));
     96         assertTrue(mCompatPinnedList.addPin(Uri.parse("content://something/something2"), "my_pkg",
     97                 mSpecs));
     98     }
     99 
    100     @Test
    101     public void testRemovePin() {
    102         mCompatPinnedList.addPin(Uri.parse("content://something/something"), "my_pkg", mSpecs);
    103         mCompatPinnedList.addPin(Uri.parse("content://something/something"), "my_pkg2", mSpecs);
    104         assertFalse(mCompatPinnedList.removePin(Uri.parse("content://something/something"),
    105                     "my_pkg"));
    106         assertTrue(mCompatPinnedList.removePin(Uri.parse("content://something/something"),
    107                     "my_pkg2"));
    108     }
    109 
    110     @Test
    111     public void testMergeSpecs() {
    112         Uri uri = Uri.parse("content://something/something");
    113 
    114         assertEquals(Collections.emptySet(), mCompatPinnedList.getSpecs(uri));
    115 
    116         mCompatPinnedList.addPin(uri, "my_pkg", new ArraySet<>(Arrays.asList(FIRST_SPECS)));
    117         assertSetEquals(new ArraySet<>(Arrays.asList(FIRST_SPECS)),
    118                 mCompatPinnedList.getSpecs(uri));
    119 
    120         mCompatPinnedList.addPin(uri, "my_pkg2", new ArraySet<>(Arrays.asList(SECOND_SPECS)));
    121         assertSetEquals(new ArraySet<>(Arrays.asList(new SliceSpec[]{
    122                 // spec1 is gone because it's not in the second set.
    123                 new SliceSpec("spec2", 1), // spec2 is 1 because it's smaller in the second set.
    124                 new SliceSpec("spec3", 2), // spec3 is the same in both sets
    125                 new SliceSpec("spec4", 1), // spec4 is 1 because it's smaller in the first set.
    126                 // spec5 is gone because it's not in the first set.
    127         })), mCompatPinnedList.getSpecs(uri));
    128     }
    129 
    130     private <T> void assertSetEquals(Set<T> a, Set<T> b) {
    131         if (a.size() != b.size()) {
    132             throw new AssertionFailedError("Wanted " + a + " but received " + b);
    133         }
    134 
    135         for (T o : a) {
    136             if (!b.contains(o)) {
    137                 throw new AssertionFailedError("Wanted " + a + " but received " + b);
    138             }
    139         }
    140     }
    141 }
    142