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.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.content.Context; 30 import android.os.UserManager; 31 import android.print.PrintJob; 32 import android.print.PrintJobInfo; 33 import android.print.PrintManager; 34 import android.printservice.PrintServiceInfo; 35 36 import androidx.lifecycle.LifecycleOwner; 37 38 import com.android.settings.R; 39 import com.android.settingslib.RestrictedPreference; 40 import com.android.settingslib.core.lifecycle.Lifecycle; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Mock; 46 import org.mockito.Mockito; 47 import org.mockito.MockitoAnnotations; 48 import org.robolectric.RobolectricTestRunner; 49 import org.robolectric.RuntimeEnvironment; 50 import org.robolectric.util.ReflectionHelpers; 51 52 import java.util.ArrayList; 53 import java.util.List; 54 55 @RunWith(RobolectricTestRunner.class) 56 public class PrintSettingsPreferenceControllerTest { 57 58 @Mock 59 private PrintManager mPrintManager; 60 @Mock 61 private UserManager mUserManager; 62 @Mock 63 private RestrictedPreference mPreference; 64 65 private Context mContext; 66 private PrintSettingPreferenceController mController; 67 private LifecycleOwner mLifecycleOwner; 68 private Lifecycle mLifecycle; 69 70 @Before setUp()71 public void setUp() { 72 MockitoAnnotations.initMocks(this); 73 mContext = spy(RuntimeEnvironment.application); 74 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 75 mPreference = spy(new RestrictedPreference(mContext)); 76 mController = new PrintSettingPreferenceController(mContext); 77 mLifecycleOwner = () -> mLifecycle; 78 mLifecycle = new Lifecycle(mLifecycleOwner); 79 ReflectionHelpers.setField(mController, "mPrintManager", mPrintManager); 80 mLifecycle.addObserver(mController); 81 } 82 83 @Test onStartStop_shouldRegisterPrintStateListener()84 public void onStartStop_shouldRegisterPrintStateListener() { 85 mLifecycle.handleLifecycleEvent(ON_START); 86 mLifecycle.handleLifecycleEvent(ON_STOP); 87 88 verify(mPrintManager).addPrintJobStateChangeListener(mController); 89 verify(mPrintManager).removePrintJobStateChangeListener(mController); 90 } 91 92 @Test updateState_hasActiveJob_shouldSetSummaryToNumberOfJobs()93 public void updateState_hasActiveJob_shouldSetSummaryToNumberOfJobs() { 94 final List<PrintJob> printJobs = new ArrayList<>(); 95 final PrintJob job = mock(PrintJob.class, Mockito.RETURNS_DEEP_STUBS); 96 printJobs.add(job); 97 when(job.getInfo().getState()).thenReturn(PrintJobInfo.STATE_STARTED); 98 when(mPrintManager.getPrintJobs()).thenReturn(printJobs); 99 100 mController.updateState(mPreference); 101 102 assertThat(mPreference.getSummary()) 103 .isEqualTo(mContext.getResources() 104 .getQuantityString(R.plurals.print_jobs_summary, 1, 1)); 105 } 106 107 @Test updateState_shouldSetSummaryToNumberOfPrintServices()108 public void updateState_shouldSetSummaryToNumberOfPrintServices() { 109 final List<PrintServiceInfo> printServices = mock(List.class); 110 when(printServices.isEmpty()).thenReturn(false); 111 when(printServices.size()).thenReturn(2); 112 // 2 services 113 when(mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES)) 114 .thenReturn(printServices); 115 116 mController.updateState(mPreference); 117 118 assertThat(mPreference.getSummary()) 119 .isEqualTo(mContext.getResources() 120 .getQuantityString(R.plurals.print_settings_summary, 2, 2)); 121 122 // No service 123 when(mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES)).thenReturn(null); 124 125 mController.updateState(mPreference); 126 127 assertThat(mPreference.getSummary()) 128 .isEqualTo(mContext.getString(R.string.print_settings_summary_no_service)); 129 } 130 131 @Test updateState_shouldCheckRestriction()132 public void updateState_shouldCheckRestriction() { 133 mController.updateState(mPreference); 134 verify(mPreference).checkRestrictionAndSetDisabled(UserManager.DISALLOW_PRINTING); 135 } 136 } 137