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.settings.network;
18
19import static com.google.common.truth.Truth.assertThat;
20import static org.mockito.Mockito.doReturn;
21import static org.mockito.Mockito.mock;
22import static org.mockito.Mockito.spy;
23import static org.mockito.Mockito.when;
24
25import android.content.Context;
26import android.os.UserManager;
27
28import com.android.settings.testutils.SettingsRobolectricTestRunner;
29
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33import org.mockito.Mock;
34import org.mockito.MockitoAnnotations;
35
36@RunWith(SettingsRobolectricTestRunner.class)
37public class NetworkResetRestrictionCheckerTest {
38
39    @Mock
40    private UserManager mUserManager;
41    @Mock
42    private Context mContext;
43    private NetworkResetRestrictionChecker mRestrictionChecker;
44
45    @Before
46    public void setUp() {
47        MockitoAnnotations.initMocks(this);
48        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
49        mRestrictionChecker = spy(new NetworkResetRestrictionChecker(mContext));
50    }
51
52    @Test
53    public void testHasRestriction_notAdmin_shouldReturnTrue() {
54        final Context context = mock(Context.class);
55        when(context.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
56        when(mUserManager.isAdminUser()).thenReturn(false);
57
58        assertThat(mRestrictionChecker.hasRestriction()).isTrue();
59    }
60
61    @Test
62    public void testHasRestriction_hasUserRestriction_shouldReturnTrue() {
63        when(mUserManager.isAdminUser()).thenReturn(true);
64        doReturn(true).when(mRestrictionChecker).hasUserBaseRestriction();
65        doReturn(false).when(mRestrictionChecker).isRestrictionEnforcedByAdmin();
66
67        assertThat(mRestrictionChecker.hasRestriction()).isTrue();
68    }
69
70    @Test
71    public void testHasRestriction_hasAdminRestriction_shouldReturnTrue() {
72        when(mUserManager.isAdminUser()).thenReturn(true);
73        doReturn(false).when(mRestrictionChecker).hasUserBaseRestriction();
74        doReturn(true).when(mRestrictionChecker).isRestrictionEnforcedByAdmin();
75
76        assertThat(mRestrictionChecker.hasRestriction()).isTrue();
77    }
78
79    @Test
80    public void testHasRestriction_noRestriction_shouldReturnFalse() {
81        when(mUserManager.isAdminUser()).thenReturn(true);
82        doReturn(false).when(mRestrictionChecker).hasUserBaseRestriction();
83        doReturn(false).when(mRestrictionChecker).isRestrictionEnforcedByAdmin();
84
85        assertThat(mRestrictionChecker.hasRestriction()).isFalse();
86    }
87}
88