1 /* 2 * Copyright (C) 2023 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.settings.privatespace; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assume.assumeTrue; 22 import static org.mockito.ArgumentMatchers.any; 23 import static org.mockito.Mockito.doNothing; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.content.Context; 29 import android.content.Intent; 30 import android.os.Flags; 31 import android.os.UserManager; 32 import android.platform.test.annotations.RequiresFlagsEnabled; 33 import android.platform.test.flag.junit.CheckFlagsRule; 34 import android.platform.test.flag.junit.DeviceFlagsValueProvider; 35 36 import androidx.test.core.app.ApplicationProvider; 37 import androidx.test.ext.junit.runners.AndroidJUnit4; 38 import androidx.test.platform.app.InstrumentationRegistry; 39 40 import org.junit.Before; 41 import org.junit.Rule; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.mockito.ArgumentCaptor; 45 import org.mockito.Mock; 46 import org.mockito.MockitoAnnotations; 47 48 @RunWith(AndroidJUnit4.class) 49 public class PrivateSpaceAuthenticationActivityTest { 50 @Rule 51 public final CheckFlagsRule mCheckFlagsRule = 52 DeviceFlagsValueProvider.createCheckFlagsRule(); 53 @Mock private PrivateSpaceMaintainer mPrivateSpaceMaintainer; 54 @Mock private Context mContext; 55 private PrivateSpaceAuthenticationActivity mPrivateSpaceAuthenticationActivity; 56 private Intent mDefaultIntent; 57 58 /** Required setup before a test. */ 59 @Before setUp()60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 63 mContext = ApplicationProvider.getApplicationContext(); 64 mDefaultIntent = new Intent(); 65 mDefaultIntent.setClass(InstrumentationRegistry.getInstrumentation().getTargetContext(), 66 PrivateSpaceAuthenticationActivity.class); 67 InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { 68 try { 69 mPrivateSpaceAuthenticationActivity = 70 spy((PrivateSpaceAuthenticationActivity) InstrumentationRegistry 71 .getInstrumentation().newActivity( 72 getClass().getClassLoader(), 73 PrivateSpaceAuthenticationActivity.class.getName(), 74 mDefaultIntent)); 75 } catch (Exception e) { 76 throw new RuntimeException(e); // nothing to do 77 } 78 }); 79 doNothing().when(mPrivateSpaceAuthenticationActivity).startActivity(any(Intent.class)); 80 PrivateSpaceAuthenticationActivity.Injector injector = 81 new PrivateSpaceAuthenticationActivity.Injector() { 82 @Override 83 PrivateSpaceMaintainer injectPrivateSpaceMaintainer(Context context) { 84 return mPrivateSpaceMaintainer; 85 } 86 }; 87 mPrivateSpaceAuthenticationActivity.setPrivateSpaceMaintainer(injector); 88 } 89 90 /** Tests that when Private does not exist setup flow is started. */ 91 //TODO(b/307729746) Plan to add more tests for complete setup flow 92 @Test 93 @RequiresFlagsEnabled({Flags.FLAG_ALLOW_PRIVATE_PROFILE, 94 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES}) whenPrivateProfileDoesNotExist_triggersSetupFlow()95 public void whenPrivateProfileDoesNotExist_triggersSetupFlow() { 96 assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile()); 97 when(mPrivateSpaceMaintainer.doesPrivateSpaceExist()).thenReturn(false); 98 99 final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); 100 mPrivateSpaceAuthenticationActivity.onLockAuthentication(mContext); 101 verify(mPrivateSpaceAuthenticationActivity).startActivity(intentCaptor.capture()); 102 assertThat(intentCaptor.getValue().getComponent().getClassName()) 103 .isEqualTo(PrivateSpaceSetupActivity.class.getName()); 104 } 105 } 106