1 /*
2  * Copyright 2017 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.recyclerview.selection.testing;
18 
19 import androidx.recyclerview.selection.DefaultSelectionTracker;
20 import androidx.recyclerview.selection.EventBridge;
21 import androidx.recyclerview.selection.ItemKeyProvider;
22 import androidx.recyclerview.selection.SelectionPredicates;
23 import androidx.recyclerview.selection.SelectionTracker;
24 import androidx.recyclerview.selection.StorageStrategy;
25 
26 import org.jspecify.annotations.NonNull;
27 
28 public final class SelectionTrackers {
29 
SelectionTrackers()30     private SelectionTrackers() {}
31 
createStringTracker(String id, int numItems)32     public static SelectionTracker<String> createStringTracker(String id, int numItems) {
33         TestAdapter<String> adapter = new TestAdapter<>(TestData.createStringData(numItems));
34         ItemKeyProvider<String> keyProvider =
35                 new TestItemKeyProvider<>(ItemKeyProvider.SCOPE_MAPPED, adapter);
36         SelectionTracker<String> tracker = new DefaultSelectionTracker<>(
37                 id,
38                 keyProvider,
39                 SelectionPredicates.createSelectAnything(),
40                 StorageStrategy.createStringStorage());
41 
42         EventBridge.install(adapter, tracker, keyProvider, SelectionTrackers::runImmediately);
43 
44         return tracker;
45     }
46 
createLongTracker(String id, int numItems)47     public static SelectionTracker<Long> createLongTracker(String id, int numItems) {
48         TestAdapter<Long> adapter = new TestAdapter<>(TestData.createLongData(numItems));
49         ItemKeyProvider<Long> keyProvider =
50                 new TestItemKeyProvider<>(ItemKeyProvider.SCOPE_MAPPED, adapter);
51         SelectionTracker<Long> tracker = new DefaultSelectionTracker<>(
52                 id,
53                 keyProvider,
54                 SelectionPredicates.createSelectAnything(),
55                 StorageStrategy.createLongStorage());
56 
57         EventBridge.install(adapter, tracker, keyProvider, SelectionTrackers::runImmediately);
58 
59         return tracker;
60     }
61 
62     // Test stand-in for RecyclerView::postOnAnimation.
runImmediately(@onNull Runnable runnable)63     private static void runImmediately(@NonNull Runnable runnable) {
64         runnable.run();
65     }
66 }
67