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.dirlist;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 import android.content.pm.UserProperties;
25 import android.os.UserManager;
26 import android.view.View;
27 import android.widget.Button;
28 
29 import androidx.test.filters.SmallTest;
30 import androidx.test.platform.app.InstrumentationRegistry;
31 
32 import com.android.documentsui.CrossProfileQuietModeException;
33 import com.android.documentsui.Model;
34 import com.android.documentsui.R;
35 import com.android.documentsui.TestConfigStore;
36 import com.android.documentsui.base.State;
37 import com.android.documentsui.base.UserId;
38 import com.android.documentsui.testing.TestActionHandler;
39 import com.android.documentsui.testing.TestEnv;
40 import com.android.documentsui.testing.TestProvidersAccess;
41 import com.android.documentsui.testing.UserManagers;
42 import com.android.modules.utils.build.SdkLevel;
43 
44 import com.google.common.collect.Lists;
45 
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.junit.runners.Parameterized;
50 import org.junit.runners.Parameterized.Parameter;
51 import org.junit.runners.Parameterized.Parameters;
52 
53 import java.util.HashMap;
54 import java.util.Map;
55 
56 @SmallTest
57 @RunWith(Parameterized.class)
58 public final class InflateMessageDocumentHolderTest {
59 
60     private Context mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
61     private Runnable mDefaultCallback = () -> {
62     };
63     private Message mInflateMessage;
64     private TestActionHandler mTestActionHandler = new TestActionHandler();
65     private InflateMessageDocumentHolder mHolder;
66     private TestConfigStore mTestConfigStore = new TestConfigStore();
67 
68     @Parameter(0)
69     public boolean isPrivateSpaceEnabled;
70 
71     /**
72      * Parametrize values for {@code isPrivateSpaceEnabled} to run all the tests twice once with
73      * private space flag enabled and once with it disabled.
74      */
75     @Parameters(name = "privateSpaceEnabled={0}")
data()76     public static Iterable<?> data() {
77         return Lists.newArrayList(true, false);
78     }
79 
80     @Before
setUp()81     public void setUp() {
82         DocumentsAdapter.Environment env =
83                 new TestEnvironment(mContext, TestEnv.create(), mTestActionHandler);
84         env.getDisplayState().action = State.ACTION_GET_CONTENT;
85         env.getDisplayState().supportsCrossProfile = true;
86 
87         mContext.setTheme(R.style.DocumentsTheme);
88         mContext.getTheme().applyStyle(R.style.DocumentsDefaultTheme,  /* force= */false);
89 
90         isPrivateSpaceEnabled = SdkLevel.isAtLeastS() && isPrivateSpaceEnabled;
91         if (isPrivateSpaceEnabled) {
92             mTestConfigStore.enablePrivateSpaceInPhotoPicker();
93             Map<UserId, String> userIdToLabelMap = new HashMap<>();
94             userIdToLabelMap.put(TestProvidersAccess.USER_ID, "Personal");
95             userIdToLabelMap.put(TestProvidersAccess.OtherUser.USER_ID, "Work");
96             env.getDisplayState().canForwardToProfileIdMap.put(TestProvidersAccess.USER_ID, true);
97             env.getDisplayState().canForwardToProfileIdMap.put(
98                     TestProvidersAccess.OtherUser.USER_ID, true);
99             UserManager userManager = UserManagers.create();
100             if (SdkLevel.isAtLeastV()) {
101                 userIdToLabelMap.put(TestProvidersAccess.AnotherUser.USER_ID, "Private");
102                 env.getDisplayState().canForwardToProfileIdMap.put(
103                         TestProvidersAccess.AnotherUser.USER_ID, true);
104                 UserProperties otherUserProperties = new UserProperties.Builder()
105                         .setShowInQuietMode(UserProperties.SHOW_IN_QUIET_MODE_PAUSED)
106                         .build();
107                 UserProperties anotherUserProperties = new UserProperties.Builder()
108                         .setShowInQuietMode(UserProperties.SHOW_IN_QUIET_MODE_PAUSED)
109                         .build();
110                 when(userManager.getUserProperties(TestProvidersAccess.OtherUser.USER_HANDLE))
111                         .thenReturn(otherUserProperties);
112                 when(userManager.getUserProperties(TestProvidersAccess.AnotherUser.USER_HANDLE))
113                         .thenReturn(anotherUserProperties);
114             }
115 
116             mInflateMessage = new Message.InflateMessage(env, mDefaultCallback,
117                     TestProvidersAccess.USER_ID,
118                     TestProvidersAccess.OtherUser.USER_ID, userIdToLabelMap, userManager,
119                     mTestConfigStore);
120         } else {
121             mInflateMessage = new Message.InflateMessage(env, mDefaultCallback, mTestConfigStore);
122             env.getDisplayState().canShareAcrossProfile = true;
123         }
124         mHolder = new InflateMessageDocumentHolder(mContext, /* parent= */null, mTestConfigStore);
125     }
126 
127     @Test
testClickingButtonShouldShowProgressBar()128     public void testClickingButtonShouldShowProgressBar() {
129         if (SdkLevel.isAtLeastV()) return;
130         Model.Update error = new Model.Update(
131                 new CrossProfileQuietModeException(TestProvidersAccess.OtherUser.USER_ID),
132                 /* remoteActionsEnabled= */ true);
133         mInflateMessage.update(error);
134 
135         mHolder.bind(mInflateMessage);
136 
137         View content = mHolder.itemView.findViewById(R.id.content);
138         View crossProfile = mHolder.itemView.findViewById(R.id.cross_profile);
139         View crossProfileContent = mHolder.itemView.findViewById(R.id.cross_profile_content);
140         View progress = mHolder.itemView.findViewById(R.id.cross_profile_progress);
141         Button button = mHolder.itemView.findViewById(R.id.button);
142 
143         assertThat(content.getVisibility()).isEqualTo(View.GONE);
144         assertThat(crossProfile.getVisibility()).isEqualTo(View.VISIBLE);
145         assertThat(crossProfileContent.getVisibility()).isEqualTo(View.VISIBLE);
146         assertThat(progress.getVisibility()).isEqualTo(View.GONE);
147 
148         if (button.getVisibility() == View.VISIBLE) {
149             // The button is visible when docsUI has the permission to modify quiet mode.
150             assertThat(button.callOnClick()).isTrue();
151             assertThat(crossProfile.getVisibility()).isEqualTo(View.VISIBLE);
152             assertThat(crossProfileContent.getVisibility()).isEqualTo(View.GONE);
153             assertThat(progress.getVisibility()).isEqualTo(View.VISIBLE);
154         }
155     }
156 }
157