1 /* 2 * Copyright (C) 2018 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.print; 18 19 import static androidx.lifecycle.Lifecycle.Event.ON_START; 20 import static androidx.lifecycle.Lifecycle.Event.ON_STOP; 21 22 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 23 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; 24 25 import static com.google.common.truth.Truth.assertThat; 26 27 import static org.mockito.Mockito.mock; 28 import static org.mockito.Mockito.never; 29 import static org.mockito.Mockito.spy; 30 import static org.mockito.Mockito.verify; 31 import static org.mockito.Mockito.when; 32 33 import android.content.Context; 34 import android.content.pm.PackageManager; 35 import android.os.UserManager; 36 import android.print.PrintJob; 37 import android.print.PrintJobInfo; 38 import android.print.PrintManager; 39 import android.printservice.PrintServiceInfo; 40 41 import androidx.lifecycle.LifecycleOwner; 42 43 import com.android.settings.R; 44 import com.android.settingslib.RestrictedPreference; 45 import com.android.settingslib.core.lifecycle.Lifecycle; 46 import com.android.settingslib.utils.StringUtil; 47 48 import org.junit.Before; 49 import org.junit.Test; 50 import org.junit.runner.RunWith; 51 import org.mockito.Mock; 52 import org.mockito.Mockito; 53 import org.mockito.MockitoAnnotations; 54 import org.robolectric.RobolectricTestRunner; 55 import org.robolectric.RuntimeEnvironment; 56 import org.robolectric.util.ReflectionHelpers; 57 58 import java.util.ArrayList; 59 import java.util.List; 60 61 @RunWith(RobolectricTestRunner.class) 62 public class PrintSettingsPreferenceControllerTest { 63 64 @Mock 65 private PrintManager mPrintManager; 66 @Mock 67 private UserManager mUserManager; 68 @Mock 69 private RestrictedPreference mPreference; 70 @Mock 71 private PackageManager mPackageManager; 72 73 private Context mContext; 74 private PrintSettingPreferenceController mController; 75 private LifecycleOwner mLifecycleOwner; 76 private Lifecycle mLifecycle; 77 78 @Before setUp()79 public void setUp() { 80 MockitoAnnotations.initMocks(this); 81 mContext = spy(RuntimeEnvironment.application); 82 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 83 mPreference = spy(new RestrictedPreference(mContext)); 84 mController = new PrintSettingPreferenceController(mContext); 85 mLifecycleOwner = () -> mLifecycle; 86 mLifecycle = new Lifecycle(mLifecycleOwner); 87 ReflectionHelpers.setField(mController, "mPrintManager", mPrintManager); 88 ReflectionHelpers.setField(mController, "mPackageManager", mPackageManager); 89 mLifecycle.addObserver(mController); 90 } 91 92 @Test onStartStop_shouldRegisterPrintStateListener()93 public void onStartStop_shouldRegisterPrintStateListener() { 94 mLifecycle.handleLifecycleEvent(ON_START); 95 mLifecycle.handleLifecycleEvent(ON_STOP); 96 97 verify(mPrintManager).addPrintJobStateChangeListener(mController); 98 verify(mPrintManager).removePrintJobStateChangeListener(mController); 99 } 100 101 @Test onStartStop_printManagerIsNull_doNothing()102 public void onStartStop_printManagerIsNull_doNothing() { 103 ReflectionHelpers.setField(mController, "mPrintManager", null); 104 105 mLifecycle.handleLifecycleEvent(ON_START); 106 mLifecycle.handleLifecycleEvent(ON_STOP); 107 108 verify(mPrintManager, never()).addPrintJobStateChangeListener(mController); 109 verify(mPrintManager, never()).removePrintJobStateChangeListener(mController); 110 } 111 112 @Test updateState_hasActiveJob_shouldSetSummaryToNumberOfJobs()113 public void updateState_hasActiveJob_shouldSetSummaryToNumberOfJobs() { 114 final List<PrintJob> printJobs = new ArrayList<>(); 115 final PrintJob job = mock(PrintJob.class, Mockito.RETURNS_DEEP_STUBS); 116 printJobs.add(job); 117 when(job.getInfo().getState()).thenReturn(PrintJobInfo.STATE_STARTED); 118 when(mPrintManager.getPrintJobs()).thenReturn(printJobs); 119 120 mController.updateState(mPreference); 121 122 assertThat(mPreference.getSummary()) 123 .isEqualTo(StringUtil.getIcuPluralsString(mContext, 1, 124 R.string.print_jobs_summary)); 125 } 126 127 @Test updateState_shouldSetSummaryToNumberOfPrintServices()128 public void updateState_shouldSetSummaryToNumberOfPrintServices() { 129 final List<PrintServiceInfo> printServices = mock(List.class); 130 when(printServices.isEmpty()).thenReturn(false); 131 when(printServices.size()).thenReturn(2); 132 // 2 services 133 when(mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES)) 134 .thenReturn(printServices); 135 136 mController.updateState(mPreference); 137 138 assertThat(mPreference.getSummary()) 139 .isEqualTo(StringUtil.getIcuPluralsString(mContext, 2, 140 R.string.print_settings_summary)); 141 142 // No service 143 when(mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES)).thenReturn(null); 144 145 mController.updateState(mPreference); 146 147 assertThat(mPreference.getSummary()) 148 .isEqualTo(mContext.getString(R.string.print_settings_summary_no_service)); 149 } 150 151 @Test updateState_shouldCheckRestriction()152 public void updateState_shouldCheckRestriction() { 153 mController.updateState(mPreference); 154 verify(mPreference).checkRestrictionAndSetDisabled(UserManager.DISALLOW_PRINTING); 155 } 156 157 @Test getAvailabilityStatus_hasFeature_returnsAvailable()158 public void getAvailabilityStatus_hasFeature_returnsAvailable() { 159 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_PRINTING)) 160 .thenReturn(true); 161 162 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 163 } 164 165 @Test getAvailabilityStatus_noFeature_returnsUnsupported()166 public void getAvailabilityStatus_noFeature_returnsUnsupported() { 167 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_PRINTING)) 168 .thenReturn(false); 169 170 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 171 } 172 173 @Test getAvailabilityStatus_printManagerIsNull_returnsUnsupported()174 public void getAvailabilityStatus_printManagerIsNull_returnsUnsupported() { 175 ReflectionHelpers.setField(mController, "mPrintManager", null); 176 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_PRINTING)) 177 .thenReturn(true); 178 179 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 180 } 181 } 182