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
17package com.android.settingslib.deviceinfo;
18
19import static com.google.common.truth.Truth.assertWithMessage;
20import static org.mockito.ArgumentMatchers.anyInt;
21import static org.mockito.Mockito.doReturn;
22import static org.mockito.Mockito.mock;
23
24import android.content.Context;
25import android.os.PersistableBundle;
26import android.support.v7.preference.Preference;
27import android.support.v7.preference.PreferenceScreen;
28import android.telephony.CarrierConfigManager;
29import android.telephony.SubscriptionManager;
30
31import com.android.settingslib.SettingsLibRobolectricTestRunner;
32import com.android.settingslib.core.lifecycle.Lifecycle;
33
34import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37import org.mockito.Mock;
38import org.mockito.MockitoAnnotations;
39import org.robolectric.annotation.Config;
40import org.robolectric.annotation.Implementation;
41import org.robolectric.annotation.Implements;
42
43@RunWith(SettingsLibRobolectricTestRunner.class)
44public class ImsStatusPreferenceControllerTest {
45    @Mock
46    private Context mContext;
47    @Mock
48    private Lifecycle mLifecycle;
49    @Mock
50    private PreferenceScreen mScreen;
51    @Mock
52    private Preference mPreference;
53
54    @Before
55    public void setUp() {
56        MockitoAnnotations.initMocks(this);
57        doReturn(mPreference).when(mScreen)
58                .findPreference(AbstractImsStatusPreferenceController.KEY_IMS_REGISTRATION_STATE);
59    }
60
61    @Test
62    @Config(shadows = ShadowSubscriptionManager.class)
63    public void testIsAvailable() {
64        CarrierConfigManager carrierConfigManager = mock(CarrierConfigManager.class);
65        doReturn(carrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
66
67        PersistableBundle config = new PersistableBundle(1);
68        config.putBoolean(CarrierConfigManager.KEY_SHOW_IMS_REGISTRATION_STATUS_BOOL, true);
69        doReturn(config).when(carrierConfigManager).getConfigForSubId(anyInt());
70
71        final AbstractImsStatusPreferenceController imsStatusPreferenceController =
72                new ConcreteImsStatusPreferenceController(mContext, mLifecycle);
73
74        assertWithMessage("Should be available when IMS registration is true").that(
75                imsStatusPreferenceController.isAvailable()).isTrue();
76
77        config.putBoolean(CarrierConfigManager.KEY_SHOW_IMS_REGISTRATION_STATUS_BOOL, false);
78
79        assertWithMessage("Should not be available when IMS registration is false")
80                .that(imsStatusPreferenceController.isAvailable()).isFalse();
81
82        doReturn(null).when(carrierConfigManager).getConfigForSubId(anyInt());
83
84        assertWithMessage("Should not be available when IMS registration is false")
85                .that(imsStatusPreferenceController.isAvailable()).isFalse();
86
87        doReturn(null).when(mContext).getSystemService(CarrierConfigManager.class);
88
89        assertWithMessage("Should not be available when CarrierConfigManager service is null")
90                .that(imsStatusPreferenceController.isAvailable()).isFalse();
91    }
92
93    @Implements(SubscriptionManager.class)
94    public static class ShadowSubscriptionManager {
95        @Implementation
96        public static int getDefaultDataSubscriptionId() {
97            return 1234;
98        }
99    }
100
101    private static class ConcreteImsStatusPreferenceController
102            extends AbstractImsStatusPreferenceController {
103
104        public ConcreteImsStatusPreferenceController(Context context,
105                Lifecycle lifecycle) {
106            super(context, lifecycle);
107        }
108    }
109}
110