1 /*
2  * Copyright (C) 2017 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 org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22 
23 import android.app.Activity;
24 import android.content.res.Resources;
25 import android.print.PrintManager;
26 import android.printservice.PrintServiceInfo;
27 
28 import com.android.settings.R;
29 import com.android.settings.SettingsRobolectricTestRunner;
30 import com.android.settings.TestConfig;
31 import com.android.settings.dashboard.SummaryLoader;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.robolectric.annotation.Config;
39 
40 import java.util.List;
41 
42 
43 @RunWith(SettingsRobolectricTestRunner.class)
44 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
45 public class PrintSettingsFragmentTest {
46 
47     @Mock
48     private PrintSettingsFragment.PrintSummaryProvider.PrintManagerWrapper mPrintManager;
49     @Mock
50     private Activity mActivity;
51     @Mock
52     private Resources mRes;
53     @Mock
54     private SummaryLoader mSummaryLoader;
55     private SummaryLoader.SummaryProvider mSummaryProvider;
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60         when(mActivity.getResources()).thenReturn(mRes);
61         mSummaryProvider = new PrintSettingsFragment.PrintSummaryProvider(mActivity, mSummaryLoader,
62                 mPrintManager);
63     }
64 
65     @Test
testSummary_shouldSetSummaryToNumberOfPrintServices()66     public void testSummary_shouldSetSummaryToNumberOfPrintServices() {
67         final List<PrintServiceInfo> printServices = mock(List.class);
68         when(printServices.isEmpty()).thenReturn(false);
69         when(printServices.size()).thenReturn(2);
70         // 2 services
71         when(mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES))
72             .thenReturn(printServices);
73 
74         mSummaryProvider.setListening(true);
75 
76         verify(mRes).getQuantityString(R.plurals.print_settings_summary, 2, 2);
77 
78         // No service
79         when(mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES)).thenReturn(null);
80 
81         mSummaryProvider.setListening(true);
82 
83         verify(mActivity).getString(R.string.print_settings_summary_no_service);
84     }
85 }
86