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.ArgumentMatchers.anyInt; 25 import static org.mockito.ArgumentMatchers.anyObject; 26 import static org.mockito.Mockito.spy; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.when; 29 30 import android.content.Context; 31 import android.print.PrintJob; 32 import android.print.PrintJobInfo; 33 import android.print.PrintManager; 34 35 import androidx.lifecycle.LifecycleOwner; 36 import androidx.preference.Preference; 37 import androidx.preference.PreferenceScreen; 38 39 import com.android.settings.R; 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.MockitoAnnotations; 47 import org.robolectric.RobolectricTestRunner; 48 import org.robolectric.RuntimeEnvironment; 49 50 @RunWith(RobolectricTestRunner.class) 51 public class PrintJobPreferenceControllerTest { 52 private static final String PREF_KEY = "print_job_preference"; 53 54 @Mock 55 private PrintManager mPrintManager; 56 @Mock 57 private PrintJob mPrintJob; 58 @Mock 59 private PrintJobInfo mPrintJobInfo; 60 @Mock 61 private PreferenceScreen mScreen; 62 63 private Context mContext; 64 private LifecycleOwner mLifecycleOwner; 65 private Lifecycle mLifecycle; 66 private PrintJobPreferenceController mController; 67 private Preference mPreference; 68 private String mTestLabel; 69 70 @Before setUp()71 public void setUp() { 72 MockitoAnnotations.initMocks(this); 73 mContext = spy(RuntimeEnvironment.application); 74 mPreference = new Preference(mContext); 75 mTestLabel = "PrintTest"; 76 when(mContext.getSystemService(Context.PRINT_SERVICE)).thenReturn(mPrintManager); 77 when(mPrintManager.getGlobalPrintManagerForUser(anyInt())).thenReturn(mPrintManager); 78 when(mPrintManager.getPrintJob(anyObject())).thenReturn(mPrintJob); 79 when(mPrintJob.getInfo()).thenReturn(mPrintJobInfo); 80 mController = new PrintJobPreferenceController(mContext, PREF_KEY); 81 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 82 when(mPrintJobInfo.getLabel()).thenReturn(mTestLabel); 83 mController.displayPreference(mScreen); 84 mLifecycleOwner = () -> mLifecycle; 85 mLifecycle = new Lifecycle(mLifecycleOwner); 86 mLifecycle.addObserver(mController); 87 } 88 89 @Test onStartStop_shouldRegisterPrintStateListener()90 public void onStartStop_shouldRegisterPrintStateListener() { 91 mLifecycle.handleLifecycleEvent(ON_START); 92 mLifecycle.handleLifecycleEvent(ON_STOP); 93 94 verify(mPrintManager).addPrintJobStateChangeListener(mController); 95 verify(mPrintManager).removePrintJobStateChangeListener(mController); 96 } 97 98 @Test updateUi_jobState_STATE_CREATED()99 public void updateUi_jobState_STATE_CREATED() { 100 when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_CREATED); 101 102 mController.onStart(); 103 String title = mContext.getString( 104 R.string.print_configuring_state_title_template, mTestLabel); 105 106 assertThat(mPreference.getTitle()).isEqualTo(title); 107 } 108 109 @Test updateUi_jobState_STATE_QUEUED()110 public void updateUi_jobState_STATE_QUEUED() { 111 when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_QUEUED); 112 113 mController.onStart(); 114 String title = mContext.getString( 115 R.string.print_printing_state_title_template, mTestLabel); 116 117 assertThat(mPreference.getTitle()).isEqualTo(title); 118 } 119 120 @Test updateUi_jobState_STATE_STARTED()121 public void updateUi_jobState_STATE_STARTED() { 122 when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_STARTED); 123 124 mController.onStart(); 125 String title = mContext.getString( 126 R.string.print_printing_state_title_template, mTestLabel); 127 128 assertThat(mPreference.getTitle()).isEqualTo(title); 129 } 130 131 @Test updateUi_jobState_STATE_QUEUED_and_jobInfo_CANCELLING()132 public void updateUi_jobState_STATE_QUEUED_and_jobInfo_CANCELLING() { 133 when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_QUEUED); 134 when(mPrintJobInfo.isCancelling()).thenReturn(true); 135 136 mController.onStart(); 137 String title = mContext.getString( 138 R.string.print_cancelling_state_title_template, mTestLabel); 139 140 assertThat(mPreference.getTitle()).isEqualTo(title); 141 } 142 143 @Test updateUi_jobState_STATE_STARTED_and_jobInfo_CANCELLING()144 public void updateUi_jobState_STATE_STARTED_and_jobInfo_CANCELLING() { 145 when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_STARTED); 146 when(mPrintJobInfo.isCancelling()).thenReturn(true); 147 148 mController.onStart(); 149 String title = mContext.getString( 150 R.string.print_cancelling_state_title_template, mTestLabel); 151 152 assertThat(mPreference.getTitle()).isEqualTo(title); 153 } 154 155 @Test updateUi_jobState_STATE_FAILED()156 public void updateUi_jobState_STATE_FAILED() { 157 when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_FAILED); 158 159 mController.onStart(); 160 String title = mContext.getString( 161 R.string.print_failed_state_title_template, mTestLabel); 162 163 assertThat(mPreference.getTitle()).isEqualTo(title); 164 } 165 166 @Test updateUi_jobState_STATE_BLOCKED()167 public void updateUi_jobState_STATE_BLOCKED() { 168 when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_BLOCKED); 169 170 mController.onStart(); 171 String title = mContext.getString( 172 R.string.print_blocked_state_title_template, mTestLabel); 173 174 assertThat(mPreference.getTitle()).isEqualTo(title); 175 } 176 177 @Test updateUi_jobState_STATE_BLOCKED_and_jobInfo_CANCELLING()178 public void updateUi_jobState_STATE_BLOCKED_and_jobInfo_CANCELLING() { 179 when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_BLOCKED); 180 when(mPrintJobInfo.isCancelling()).thenReturn(true); 181 182 mController.onStart(); 183 String title = mContext.getString( 184 R.string.print_cancelling_state_title_template, mTestLabel); 185 186 assertThat(mPreference.getTitle()).isEqualTo(title); 187 } 188 } 189