1 /*
2  * Copyright (C) 2020 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 com.android.documentsui;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import androidx.recyclerview.selection.SelectionTracker;
22 import androidx.test.filters.SmallTest;
23 
24 import org.junit.Before;
25 import org.junit.Test;
26 
27 @SmallTest
28 public class ProfileTabsControllerTest {
29 
30     private SelectionTracker<String> mSelectionMgr = SelectionHelpers.createTestInstance();
31 
32     private FakeProfileTabs mFakeProfileTabs = new FakeProfileTabs();
33     private ProfileTabsController mController = new ProfileTabsController(mSelectionMgr,
34             enabled -> mFakeProfileTabs.enabled = enabled);
35 
36     @Before
setUp()37     public void setUp() throws Exception {
38         mSelectionMgr.addObserver(mController);
39     }
40 
41     @Test
testNoSelection_enable()42     public void testNoSelection_enable() {
43         mController.onSelectionRestored();
44         assertThat(mFakeProfileTabs.isEnabled()).isTrue();
45     }
46 
47     @Test
testClearSelection_enable()48     public void testClearSelection_enable() {
49         mSelectionMgr.select("foo");
50         mSelectionMgr.clearSelection();
51         assertThat(mFakeProfileTabs.isEnabled()).isTrue();
52     }
53 
54     @Test
testDeselectAll_enable()55     public void testDeselectAll_enable() {
56         mSelectionMgr.select("foo");
57         mSelectionMgr.select("bar");
58         mSelectionMgr.deselect("foo");
59         mSelectionMgr.deselect("bar");
60         assertThat(mFakeProfileTabs.isEnabled()).isTrue();
61     }
62 
63     @Test
testSelection_disable()64     public void testSelection_disable() {
65         mSelectionMgr.select("foo");
66         assertThat(mFakeProfileTabs.isEnabled()).isFalse();
67     }
68 
69     @Test
testMultipleSelection_disable()70     public void testMultipleSelection_disable() {
71         mSelectionMgr.select("foo");
72         mSelectionMgr.select("bar");
73         mSelectionMgr.select("apple");
74         assertThat(mFakeProfileTabs.isEnabled()).isFalse();
75     }
76 
77     @Test
testDeselectSome_Disable()78     public void testDeselectSome_Disable() {
79         mSelectionMgr.select("foo");
80         mSelectionMgr.select("bar");
81         mSelectionMgr.deselect("bar");
82         assertThat(mFakeProfileTabs.isEnabled()).isFalse();
83     }
84 
85     private static class FakeProfileTabs {
86         Boolean enabled = null;
87 
isEnabled()88         boolean isEnabled() {
89             if (enabled == null) {
90                 throw new IllegalArgumentException("Not initialized");
91             }
92             return enabled;
93         }
94     }
95 }
96