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 package com.android.settings.datausage;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.ArgumentMatchers.any;
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.Mockito.doNothing;
23 import static org.mockito.Mockito.doReturn;
24 
25 import android.content.Context;
26 import android.telephony.SubscriptionInfo;
27 import android.telephony.SubscriptionManager;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.widget.LinearLayout;
31 
32 import androidx.preference.PreferenceViewHolder;
33 
34 import com.android.settings.network.ProxySubscriptionManager;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.RuntimeEnvironment;
43 
44 @RunWith(RobolectricTestRunner.class)
45 public class CellDataPreferenceTest {
46 
47     @Mock
48     private ProxySubscriptionManager mProxySubscriptionMgr;
49     @Mock
50     private SubscriptionManager mSubscriptionManager;
51     @Mock
52     private SubscriptionInfo mSubInfo;
53 
54     private Context mContext;
55     private PreferenceViewHolder mHolder;
56     private CellDataPreference mPreference;
57 
58     @Before
setUp()59     public void setUp() {
60         MockitoAnnotations.initMocks(this);
61 
62         mContext = RuntimeEnvironment.application;
63         mPreference = new CellDataPreference(mContext, null) {
64             @Override
65             ProxySubscriptionManager getProxySubscriptionManager() {
66                 return mProxySubscriptionMgr;
67             }
68             @Override
69             SubscriptionInfo getActiveSubscriptionInfo(int subId) {
70                 return mSubInfo;
71             }
72         };
73         doNothing().when(mSubscriptionManager).setDefaultDataSubId(anyInt());
74         doReturn(mSubscriptionManager).when(mProxySubscriptionMgr).get();
75         doNothing().when(mProxySubscriptionMgr).addActiveSubscriptionsListener(any());
76         doNothing().when(mProxySubscriptionMgr).removeActiveSubscriptionsListener(any());
77 
78         final LayoutInflater inflater = LayoutInflater.from(mContext);
79         final View view = inflater.inflate(mPreference.getLayoutResource(),
80                 new LinearLayout(mContext), false);
81 
82         mHolder = PreferenceViewHolder.createInstanceForTests(view);
83     }
84 
85     @Test
noActiveSub_shouldDisable()86     public void noActiveSub_shouldDisable() {
87         mSubInfo = null;
88         mPreference.mOnSubscriptionsChangeListener.onChanged();
89         assertThat(mPreference.isEnabled()).isFalse();
90     }
91 
92     @Test
hasActiveSub_shouldEnable()93     public void hasActiveSub_shouldEnable() {
94         mPreference.mOnSubscriptionsChangeListener.onChanged();
95         assertThat(mPreference.isEnabled()).isTrue();
96     }
97 }
98