• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.anyInt;
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 import android.telephony.SubscriptionInfo;
25 import android.telephony.SubscriptionManager;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.widget.LinearLayout;
29 
30 import androidx.preference.PreferenceViewHolder;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 
40 @RunWith(RobolectricTestRunner.class)
41 public class CellDataPreferenceTest {
42 
43     @Mock
44     private SubscriptionManager mSubscriptionManager;
45     @Mock
46     private SubscriptionInfo mSubInfo;
47 
48     private Context mContext;
49     private PreferenceViewHolder mHolder;
50     private CellDataPreference mPreference;
51     private SubscriptionManager.OnSubscriptionsChangedListener mListener;
52 
53     @Before
setUp()54     public void setUp() {
55         MockitoAnnotations.initMocks(this);
56 
57         mContext = RuntimeEnvironment.application;
58         mPreference = new CellDataPreference(mContext, null);
59         mListener = mPreference.mOnSubscriptionsChangeListener;
60 
61         LayoutInflater inflater = LayoutInflater.from(mContext);
62         final View view = inflater.inflate(mPreference.getLayoutResource(),
63                 new LinearLayout(mContext), false);
64 
65         mHolder = PreferenceViewHolder.createInstanceForTests(view);
66     }
67 
68     @Test
noActiveSub_shouldDisable()69     public void noActiveSub_shouldDisable() {
70         mPreference.mSubscriptionManager = mSubscriptionManager;
71         mListener.onSubscriptionsChanged();
72         assertThat(mPreference.isEnabled()).isFalse();
73 
74         when(mSubscriptionManager.getActiveSubscriptionInfo(anyInt())).thenReturn(mSubInfo);
75         mListener.onSubscriptionsChanged();
76         assertThat(mPreference.isEnabled()).isTrue();
77     }
78 }
79