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.recyclerview.selection; 18 19import static org.junit.Assert.assertEquals; 20 21import android.os.Bundle; 22import android.support.test.filters.SmallTest; 23import android.support.test.runner.AndroidJUnit4; 24 25import androidx.recyclerview.selection.testing.Bundles; 26import androidx.recyclerview.selection.testing.SelectionTrackers; 27 28import org.junit.Before; 29import org.junit.Test; 30import org.junit.runner.RunWith; 31 32/** 33 * Tests guaranteeing that two distinct selections can be stored side-by-side. 34 */ 35@RunWith(AndroidJUnit4.class) 36@SmallTest 37public final class SelectionTracker_InstanceStateTest { 38 39 private SelectionTracker<String> mStringTracker; 40 private SelectionTracker<Long> mLongTracker; 41 42 private Bundle mBundle; 43 44 @Before 45 public void setUp() { 46 mStringTracker = SelectionTrackers.createStringTracker("tracker-isolation-strings", 100); 47 mLongTracker = SelectionTrackers.createLongTracker("tracker-isolation-longs", 1000); 48 mBundle = new Bundle(); 49 } 50 51 @Test 52 public void testMaintainsDistinctSelections() { 53 mStringTracker.select("3"); 54 mStringTracker.select("13"); 55 mStringTracker.select("33"); 56 57 mLongTracker.select(100L); 58 mLongTracker.select(200L); 59 mLongTracker.select(300L); 60 61 MutableSelection<String> origStrings = new MutableSelection<>(); 62 MutableSelection<Long> origLongs = new MutableSelection<>(); 63 64 mStringTracker.copySelection(origStrings); 65 mLongTracker.copySelection(origLongs); 66 67 mStringTracker.onSaveInstanceState(mBundle); 68 mLongTracker.onSaveInstanceState(mBundle); 69 70 Bundle parceled = Bundles.forceParceling(mBundle); 71 72 mStringTracker.clearSelection(); 73 mLongTracker.clearSelection(); 74 75 mStringTracker.onRestoreInstanceState(parceled); 76 mLongTracker.onRestoreInstanceState(parceled); 77 78 assertEquals(origStrings, mStringTracker.getSelection()); 79 assertEquals(origLongs, mLongTracker.getSelection()); 80 } 81} 82