davidln | 1445fc1 | 2019-01-07 14:08:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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.car.settings.system; |
| 18 | |
| 19 | import static android.app.Activity.RESULT_CANCELED; |
| 20 | import static android.app.Activity.RESULT_OK; |
| 21 | |
| 22 | import static com.android.car.settings.system.MasterClearFragment.CHECK_LOCK_REQUEST_CODE; |
| 23 | |
| 24 | import static com.google.common.truth.Truth.assertThat; |
| 25 | |
| 26 | import static org.mockito.Mockito.when; |
| 27 | |
| 28 | import android.app.Activity; |
| 29 | import android.car.userlib.CarUserManagerHelper; |
Heemin Seog | 3cf33e0 | 2019-06-20 14:00:18 -0700 | [diff] [blame] | 30 | import android.content.Context; |
davidln | 1445fc1 | 2019-01-07 14:08:22 -0800 | [diff] [blame] | 31 | import android.content.Intent; |
Heemin Seog | 3cf33e0 | 2019-06-20 14:00:18 -0700 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
davidln | 1445fc1 | 2019-01-07 14:08:22 -0800 | [diff] [blame] | 33 | import android.widget.Button; |
| 34 | |
| 35 | import androidx.fragment.app.Fragment; |
| 36 | |
davidln | 1445fc1 | 2019-01-07 14:08:22 -0800 | [diff] [blame] | 37 | import com.android.car.settings.R; |
| 38 | import com.android.car.settings.security.CheckLockActivity; |
| 39 | import com.android.car.settings.testutils.FragmentController; |
| 40 | import com.android.car.settings.testutils.ShadowCarUserManagerHelper; |
| 41 | import com.android.car.settings.testutils.ShadowUserManager; |
| 42 | |
| 43 | import org.junit.After; |
| 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | import org.mockito.Mock; |
| 48 | import org.mockito.MockitoAnnotations; |
davidln | 15c9351 | 2019-06-07 16:58:00 -0700 | [diff] [blame] | 49 | import org.robolectric.RobolectricTestRunner; |
Heemin Seog | 3cf33e0 | 2019-06-20 14:00:18 -0700 | [diff] [blame] | 50 | import org.robolectric.RuntimeEnvironment; |
| 51 | import org.robolectric.Shadows; |
davidln | 1445fc1 | 2019-01-07 14:08:22 -0800 | [diff] [blame] | 52 | import org.robolectric.annotation.Config; |
| 53 | import org.robolectric.shadows.ShadowApplication; |
| 54 | |
| 55 | import java.util.Collections; |
| 56 | |
| 57 | /** Unit test for {@link MasterClearFragment}. */ |
davidln | 15c9351 | 2019-06-07 16:58:00 -0700 | [diff] [blame] | 58 | @RunWith(RobolectricTestRunner.class) |
davidln | 1445fc1 | 2019-01-07 14:08:22 -0800 | [diff] [blame] | 59 | @Config(shadows = {ShadowCarUserManagerHelper.class, ShadowUserManager.class}) |
| 60 | public class MasterClearFragmentTest { |
| 61 | |
| 62 | @Mock |
| 63 | private CarUserManagerHelper mCarUserManagerHelper; |
| 64 | |
| 65 | private MasterClearFragment mFragment; |
| 66 | |
| 67 | @Before |
| 68 | public void setUp() { |
| 69 | // Setup needed by instantiated PreferenceControllers. |
| 70 | MockitoAnnotations.initMocks(this); |
Heemin Seog | 3cf33e0 | 2019-06-20 14:00:18 -0700 | [diff] [blame] | 71 | Context context = RuntimeEnvironment.application; |
davidln | 1445fc1 | 2019-01-07 14:08:22 -0800 | [diff] [blame] | 72 | ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper); |
Heemin Seog | 3cf33e0 | 2019-06-20 14:00:18 -0700 | [diff] [blame] | 73 | Shadows.shadowOf(context.getPackageManager()) |
| 74 | .setSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, true); |
davidln | 1445fc1 | 2019-01-07 14:08:22 -0800 | [diff] [blame] | 75 | when(mCarUserManagerHelper.getAllSwitchableUsers()).thenReturn(Collections.emptyList()); |
| 76 | |
| 77 | mFragment = FragmentController.of(new MasterClearFragment()).setup(); |
| 78 | } |
| 79 | |
| 80 | @After |
| 81 | public void tearDown() { |
| 82 | ShadowCarUserManagerHelper.reset(); |
| 83 | ShadowUserManager.reset(); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void masterClearButtonClicked_launchesCheckLockActivity() { |
| 88 | findMasterClearButton(mFragment.requireActivity()).performClick(); |
| 89 | |
| 90 | Intent startedIntent = ShadowApplication.getInstance().getNextStartedActivity(); |
| 91 | assertThat(startedIntent.getComponent().getClassName()).isEqualTo( |
| 92 | CheckLockActivity.class.getName()); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void processActivityResult_resultOk_launchesMasterClearConfirmFragment() { |
| 97 | mFragment.processActivityResult(CHECK_LOCK_REQUEST_CODE, RESULT_OK, /* data= */ null); |
| 98 | |
| 99 | Fragment launchedFragment = mFragment.getFragmentManager().findFragmentById( |
| 100 | R.id.fragment_container); |
| 101 | |
| 102 | assertThat(launchedFragment).isInstanceOf(MasterClearConfirmFragment.class); |
| 103 | } |
| 104 | |
| 105 | @Test |
| 106 | public void processActivityResult_otherResultCode_doesNothing() { |
| 107 | mFragment.processActivityResult(CHECK_LOCK_REQUEST_CODE, RESULT_CANCELED, /* data= */ null); |
| 108 | |
| 109 | Fragment launchedFragment = mFragment.getFragmentManager().findFragmentById( |
| 110 | R.id.fragment_container); |
| 111 | |
| 112 | assertThat(launchedFragment).isInstanceOf(MasterClearFragment.class); |
| 113 | } |
| 114 | |
| 115 | private Button findMasterClearButton(Activity activity) { |
| 116 | return activity.findViewById(R.id.action_button1); |
| 117 | } |
| 118 | } |