• 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 import static org.mockito.ArgumentMatchers.anyInt;
20 import static org.mockito.Mockito.when;
21 
22 import android.content.Context;
23 import android.support.v7.preference.PreferenceViewHolder;
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 com.android.settings.testutils.SettingsRobolectricTestRunner;
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.RuntimeEnvironment;
38 
39 @RunWith(SettingsRobolectricTestRunner.class)
40 public class CellDataPreferenceTest {
41 
42     @Mock
43     private SubscriptionManager mSubscriptionManager;
44     @Mock
45     private SubscriptionInfo mSubInfo;
46 
47     private Context mContext;
48     private PreferenceViewHolder mHolder;
49     private CellDataPreference mPreference;
50     private SubscriptionManager.OnSubscriptionsChangedListener mListener;
51 
52     @Before
setUp()53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55 
56         mContext = RuntimeEnvironment.application;
57         mPreference = new CellDataPreference(mContext, null);
58         mListener = mPreference.mOnSubscriptionsChangeListener;
59 
60         LayoutInflater inflater = LayoutInflater.from(mContext);
61         final View view = inflater.inflate(mPreference.getLayoutResource(),
62                 new LinearLayout(mContext), false);
63 
64         mHolder = PreferenceViewHolder.createInstanceForTests(view);
65     }
66 
67     @Test
noActiveSub_shouldDisable()68     public void noActiveSub_shouldDisable() {
69         mPreference.mSubscriptionManager = mSubscriptionManager;
70         mListener.onSubscriptionsChanged();
71         assertThat(mPreference.isEnabled()).isFalse();
72 
73         when(mSubscriptionManager.getActiveSubscriptionInfo(anyInt()))
74                 .thenReturn(mSubInfo);
75         mListener.onSubscriptionsChanged();
76         assertThat(mPreference.isEnabled()).isTrue();
77     }
78 }
79