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.sidebar;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.res.Resources;
22 import android.view.View;
23 
24 import androidx.test.filters.MediumTest;
25 import androidx.test.platform.app.InstrumentationRegistry;
26 import androidx.test.runner.AndroidJUnit4;
27 
28 import com.android.documentsui.R;
29 import com.android.documentsui.base.State;
30 import com.android.documentsui.base.UserId;
31 import com.android.documentsui.testing.TestProvidersAccess;
32 
33 import com.google.common.collect.Lists;
34 import com.google.common.truth.Correspondence;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 
40 import java.util.Collections;
41 import java.util.List;
42 import java.util.Objects;
43 
44 /**
45  * A unit test for {@link UserItemsCombiner}.
46  */
47 @RunWith(AndroidJUnit4.class)
48 @MediumTest
49 public class UserItemsCombinerTest {
50     private static final UserId PERSONAL_USER = TestProvidersAccess.USER_ID;
51     private static final UserId WORK_USER = TestProvidersAccess.OtherUser.USER_ID;
52 
53     private static final List<Item> PERSONAL_ITEMS = Lists.newArrayList(
54             personalItem("personal 1"),
55             personalItem("personal 2"),
56             personalItem("personal 3")
57     );
58 
59     private static final List<Item> WORK_ITEMS = Lists.newArrayList(
60             workItem("work 1")
61     );
62 
63     private static final Correspondence<Item, Item> ITEM_CORRESPONDENCE =
64             Correspondence.from((Item actual, Item expected) -> {
65                     return Objects.equals(actual.title, expected.title)
66                             && Objects.equals(actual.userId, expected.userId);
67             }, "has same title and userId as in");
68 
69     private final State mState = new State();
70     private final Resources mResources =
71             InstrumentationRegistry.getInstrumentation().getTargetContext().getResources();
72     private UserItemsCombiner mCombiner;
73 
74     @Before
setUp()75     public void setUp() {
76         mState.canShareAcrossProfile = true;
77         mState.supportsCrossProfile = true;
78     }
79 
80     @Test
testCreatePresentableList_empty()81     public void testCreatePresentableList_empty() {
82         mCombiner = new UserItemsCombiner(mResources, mState)
83                 .setRootListForCurrentUser(Collections.emptyList())
84                 .setRootListForOtherUser(Collections.emptyList());
85         assertThat(mCombiner.createPresentableList()).isEmpty();
86     }
87 
88     @Test
testCreatePresentableList_currentIsPersonal_personalItemsOnly()89     public void testCreatePresentableList_currentIsPersonal_personalItemsOnly() {
90         mCombiner = new UserItemsCombiner(mResources, mState)
91                 .setRootListForCurrentUser(PERSONAL_ITEMS)
92                 .setRootListForOtherUser(Collections.emptyList());
93         assertThat(mCombiner.createPresentableList())
94                 .comparingElementsUsing(ITEM_CORRESPONDENCE)
95                 .containsExactlyElementsIn(PERSONAL_ITEMS)
96                 .inOrder();
97     }
98 
99     @Test
testCreatePresentableList_currentIsWork_personalItemsOnly()100     public void testCreatePresentableList_currentIsWork_personalItemsOnly() {
101         mCombiner = new UserItemsCombiner(mResources, mState)
102                 .overrideCurrentUserForTest(WORK_USER)
103                 .setRootListForCurrentUser(Collections.emptyList())
104                 .setRootListForOtherUser(PERSONAL_ITEMS);
105         assertThat(mCombiner.createPresentableList())
106                 .comparingElementsUsing(ITEM_CORRESPONDENCE)
107                 .containsExactlyElementsIn(PERSONAL_ITEMS)
108                 .inOrder();
109     }
110 
111     @Test
testCreatePresentableList_currentIsPersonal_workItemsOnly()112     public void testCreatePresentableList_currentIsPersonal_workItemsOnly() {
113         mCombiner = new UserItemsCombiner(mResources, mState)
114                 .setRootListForCurrentUser(Collections.emptyList())
115                 .setRootListForOtherUser(WORK_ITEMS);
116         assertThat(mCombiner.createPresentableList())
117                 .comparingElementsUsing(ITEM_CORRESPONDENCE)
118                 .containsExactlyElementsIn(WORK_ITEMS)
119                 .inOrder();
120     }
121 
122     @Test
testCreatePresentableList_currentIsWork_workItemsOnly()123     public void testCreatePresentableList_currentIsWork_workItemsOnly() {
124         mCombiner = new UserItemsCombiner(mResources, mState)
125                 .overrideCurrentUserForTest(WORK_USER)
126                 .setRootListForCurrentUser(WORK_ITEMS)
127                 .setRootListForOtherUser(Collections.emptyList());
128         assertThat(mCombiner.createPresentableList())
129                 .comparingElementsUsing(ITEM_CORRESPONDENCE)
130                 .containsExactlyElementsIn(WORK_ITEMS)
131                 .inOrder();
132     }
133 
134     @Test
testCreatePresentableList_currentIsPersonal_personalAndWorkItems()135     public void testCreatePresentableList_currentIsPersonal_personalAndWorkItems() {
136         mCombiner = new UserItemsCombiner(mResources, mState)
137                 .setRootListForCurrentUser(PERSONAL_ITEMS)
138                 .setRootListForOtherUser(WORK_ITEMS);
139 
140         List<Item> expected = Lists.newArrayList();
141         expected.add(new HeaderItem(mResources.getString(R.string.personal_tab)));
142         expected.addAll(PERSONAL_ITEMS);
143         expected.add(new HeaderItem(mResources.getString(R.string.work_tab)));
144         expected.addAll(WORK_ITEMS);
145 
146         assertThat(mCombiner.createPresentableList())
147                 .comparingElementsUsing(ITEM_CORRESPONDENCE)
148                 .containsExactlyElementsIn(expected)
149                 .inOrder();
150     }
151 
152     @Test
testCreatePresentableList_currentIsWork_personalAndWorkItems()153     public void testCreatePresentableList_currentIsWork_personalAndWorkItems() {
154         mCombiner = new UserItemsCombiner(mResources, mState)
155                 .overrideCurrentUserForTest(WORK_USER)
156                 .setRootListForCurrentUser(WORK_ITEMS)
157                 .setRootListForOtherUser(PERSONAL_ITEMS);
158 
159         List<Item> expected = Lists.newArrayList();
160         expected.add(new HeaderItem(mResources.getString(R.string.personal_tab)));
161         expected.addAll(PERSONAL_ITEMS);
162         expected.add(new HeaderItem(mResources.getString(R.string.work_tab)));
163         expected.addAll(WORK_ITEMS);
164 
165         assertThat(mCombiner.createPresentableList())
166                 .comparingElementsUsing(ITEM_CORRESPONDENCE)
167                 .containsExactlyElementsIn(expected)
168                 .inOrder();
169     }
170 
171     @Test
testCreatePresentableList_currentIsPersonal_personalAndWorkItems_cannotShare()172     public void testCreatePresentableList_currentIsPersonal_personalAndWorkItems_cannotShare() {
173         mState.canShareAcrossProfile = false;
174         mCombiner = new UserItemsCombiner(mResources, mState)
175                 .setRootListForCurrentUser(PERSONAL_ITEMS)
176                 .setRootListForOtherUser(WORK_ITEMS);
177 
178         assertThat(mCombiner.createPresentableList())
179                 .comparingElementsUsing(ITEM_CORRESPONDENCE)
180                 .containsExactlyElementsIn(PERSONAL_ITEMS)
181                 .inOrder();
182     }
183 
184     @Test
testCreatePresentableList_currentIsWork_personalItemsOnly_cannotShare()185     public void testCreatePresentableList_currentIsWork_personalItemsOnly_cannotShare() {
186         mState.canShareAcrossProfile = false;
187         mCombiner = new UserItemsCombiner(mResources, mState)
188                 .overrideCurrentUserForTest(WORK_USER)
189                 .setRootListForCurrentUser(Collections.emptyList())
190                 .setRootListForOtherUser(PERSONAL_ITEMS);
191 
192         assertThat(mCombiner.createPresentableList()).isEmpty();
193     }
194 
personalItem(String title)195     private static TestItem personalItem(String title) {
196         return new TestItem(title, PERSONAL_USER);
197     }
198 
workItem(String title)199     private static TestItem workItem(String title) {
200         return new TestItem(title, WORK_USER);
201     }
202 
203     private static class TestItem extends Item {
204 
TestItem(String title, UserId userId)205         TestItem(String title, UserId userId) {
206             super(/* layoutId= */ 0, title, /* stringId= */ "", userId);
207         }
208 
209         @Override
bindView(View convertView)210         void bindView(View convertView) {
211         }
212 
213         @Override
isRoot()214         boolean isRoot() {
215             return false;
216         }
217 
218         @Override
open()219         void open() {
220         }
221 
222         @Override
toString()223         public String toString() {
224             return title + "(" + userId + ")";
225         }
226     }
227 }
228