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.settingslib.core.lifecycle.Lifecycle;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.RobolectricTestRunner;
47 import org.robolectric.RuntimeEnvironment;
48 
49 @RunWith(RobolectricTestRunner.class)
50 public class PrintJobMessagePreferenceControllerTest {
51     private static final String PREF_KEY = "print_job_message_preference";
52 
53     @Mock
54     private PrintManager mPrintManager;
55     @Mock
56     private PrintJob mPrintJob;
57     @Mock
58     private PrintJobInfo mPrintJobInfo;
59     @Mock
60     private PreferenceScreen mScreen;
61 
62     private Context mContext;
63     private PrintJobMessagePreferenceController mController;
64     private Preference mPreference;
65     private LifecycleOwner mLifecycleOwner;
66     private Lifecycle mLifecycle;
67 
68     @Before
setUp()69     public void setUp() {
70         MockitoAnnotations.initMocks(this);
71         mContext = spy(RuntimeEnvironment.application);
72         mPreference = new Preference(mContext);
73         when(mContext.getSystemService(Context.PRINT_SERVICE)).thenReturn(mPrintManager);
74         when(mPrintManager.getGlobalPrintManagerForUser(anyInt())).thenReturn(mPrintManager);
75         when(mPrintManager.getPrintJob(anyObject())).thenReturn(mPrintJob);
76         when(mPrintJob.getInfo()).thenReturn(mPrintJobInfo);
77         mController = new PrintJobMessagePreferenceController(mContext, PREF_KEY);
78         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
79         mController.displayPreference(mScreen);
80         mLifecycleOwner = () -> mLifecycle;
81         mLifecycle = new Lifecycle(mLifecycleOwner);
82         mLifecycle.addObserver(mController);
83     }
84 
85     @Test
onStartStop_shouldRegisterPrintStateListener()86     public void onStartStop_shouldRegisterPrintStateListener() {
87         mLifecycle.handleLifecycleEvent(ON_START);
88         mLifecycle.handleLifecycleEvent(ON_STOP);
89 
90         verify(mPrintManager).addPrintJobStateChangeListener(mController);
91         verify(mPrintManager).removePrintJobStateChangeListener(mController);
92     }
93 
94     @Test
updateUi_visiblePreference()95     public void updateUi_visiblePreference() {
96         when(mPrintJobInfo.getStatus(anyObject())).thenReturn("TestPrint");
97         mLifecycle.handleLifecycleEvent(ON_START);
98 
99         assertThat(mPreference.isVisible()).isTrue();
100 
101         mLifecycle.handleLifecycleEvent(ON_STOP);
102     }
103 
104     @Test
updateUi_invisiblePreference()105     public void updateUi_invisiblePreference() {
106         when(mPrintJobInfo.getStatus(anyObject())).thenReturn(null);
107         mLifecycle.handleLifecycleEvent(ON_START);
108 
109         assertThat(mPreference.isVisible()).isFalse();
110 
111         mLifecycle.handleLifecycleEvent(ON_STOP);
112     }
113 }
114