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