1/*
2 * Copyright (C) 2016 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 */
16package com.android.settings.network;
17
18import static com.google.common.truth.Truth.assertThat;
19import static org.mockito.ArgumentMatchers.nullable;
20import static org.mockito.Mockito.mock;
21import static org.mockito.Mockito.times;
22import static org.mockito.Mockito.verify;
23import static org.mockito.Mockito.verifyZeroInteractions;
24import static org.mockito.Mockito.when;
25
26import android.content.Context;
27import android.provider.SearchIndexableResource;
28import android.view.Menu;
29
30import com.android.settings.R;
31import com.android.settings.dashboard.SummaryLoader;
32import com.android.settings.testutils.SettingsRobolectricTestRunner;
33import com.android.settingslib.drawer.CategoryKey;
34
35import org.junit.Before;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38import org.mockito.Mock;
39import org.mockito.MockitoAnnotations;
40import org.robolectric.RuntimeEnvironment;
41import org.robolectric.util.ReflectionHelpers;
42
43import java.util.List;
44
45@RunWith(SettingsRobolectricTestRunner.class)
46public class NetworkDashboardFragmentTest {
47
48    @Mock
49    private Context mContext;
50
51    private NetworkDashboardFragment mFragment;
52
53    @Before
54    public void setUp() {
55        MockitoAnnotations.initMocks(this);
56        mFragment = new NetworkDashboardFragment();
57    }
58
59    @Test
60    public void testCategory_isNetwork() {
61        assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_NETWORK);
62    }
63
64    @Test
65    public void testSearchIndexProvider_shouldIndexResource() {
66        final List<SearchIndexableResource> indexRes =
67                NetworkDashboardFragment.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(
68                    RuntimeEnvironment.application,
69                        true /* enabled */);
70
71        assertThat(indexRes).isNotNull();
72        assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
73    }
74
75    @Test
76    public void testSummaryProvider_hasMobileAndHotspot_shouldReturnMobileSummary() {
77        final MobileNetworkPreferenceController mobileNetworkPreferenceController =
78                mock(MobileNetworkPreferenceController.class);
79        final TetherPreferenceController tetherPreferenceController =
80                mock(TetherPreferenceController.class);
81
82        final SummaryLoader summaryLoader = mock(SummaryLoader.class);
83        final SummaryLoader.SummaryProvider provider =
84                new NetworkDashboardFragment.SummaryProvider(mContext, summaryLoader,
85                        mobileNetworkPreferenceController, tetherPreferenceController);
86
87        provider.setListening(false);
88
89        verifyZeroInteractions(summaryLoader);
90
91        when(mobileNetworkPreferenceController.isAvailable()).thenReturn(true);
92        when(tetherPreferenceController.isAvailable()).thenReturn(true);
93
94        provider.setListening(true);
95
96        verify(mContext).getString(R.string.wifi_settings_title);
97        verify(mContext).getString(R.string.network_dashboard_summary_data_usage);
98        verify(mContext).getString(R.string.network_dashboard_summary_hotspot);
99        verify(mContext).getString(R.string.network_dashboard_summary_mobile);
100        verify(mContext, times(3)).getString(R.string.join_many_items_middle, null, null);
101    }
102
103    @Test
104    public void testSummaryProvider_noMobileOrHotspot_shouldReturnSimpleSummary() {
105        final MobileNetworkPreferenceController mobileNetworkPreferenceController =
106                mock(MobileNetworkPreferenceController.class);
107        final TetherPreferenceController tetherPreferenceController =
108                mock(TetherPreferenceController.class);
109
110        final SummaryLoader summaryLoader = mock(SummaryLoader.class);
111        final SummaryLoader.SummaryProvider provider =
112                new NetworkDashboardFragment.SummaryProvider(mContext, summaryLoader,
113                        mobileNetworkPreferenceController, tetherPreferenceController);
114
115        provider.setListening(false);
116
117        verifyZeroInteractions(summaryLoader);
118
119        when(mobileNetworkPreferenceController.isAvailable()).thenReturn(false);
120        when(tetherPreferenceController.isAvailable()).thenReturn(false);
121
122        provider.setListening(true);
123
124        verify(mContext).getString(R.string.wifi_settings_title);
125        verify(mContext).getString(R.string.network_dashboard_summary_data_usage);
126        verify(mContext).getString(R.string.join_many_items_middle, null, null);
127    }
128}