1 /*
2  * Copyright (C) 2022 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 
17 package com.android.ondevicepersonalization.services;
18 
19 import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
20 
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.IBinder;
27 
28 import androidx.test.core.app.ApplicationProvider;
29 import androidx.test.rule.ServiceTestRule;
30 
31 import com.android.dx.mockito.inline.extended.ExtendedMockito;
32 import com.android.ondevicepersonalization.services.util.DebugUtils;
33 
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.junit.runners.JUnit4;
39 import org.mockito.MockitoSession;
40 
41 import java.util.concurrent.TimeoutException;
42 
43 @RunWith(JUnit4.class)
44 public class OnDevicePersonalizationDebugServiceTest {
45     @Rule
46     public final ServiceTestRule serviceRule = new ServiceTestRule();
47     private final Context mContext = ApplicationProvider.getApplicationContext();
48     private OnDevicePersonalizationDebugServiceDelegate mService;
49 
50     @Before
setup()51     public void setup() throws Exception {
52         mService = new OnDevicePersonalizationDebugServiceDelegate(mContext);
53     }
54 
55     @Test
testIsEnabledTrueWhenDeveloperModeOn()56     public void testIsEnabledTrueWhenDeveloperModeOn() throws Exception {
57         MockitoSession session = ExtendedMockito.mockitoSession()
58                 .mockStatic(DebugUtils.class)
59                 .startMocking();
60         try {
61             when(DebugUtils.isDeveloperModeEnabled(mContext)).thenReturn(true);
62             assertTrue(mService.isEnabled());
63         } finally {
64             session.finishMocking();
65         }
66     }
67 
68     @Test
testIsEnabledFalseWhenDeveloperModeOff()69     public void testIsEnabledFalseWhenDeveloperModeOff() throws Exception {
70         MockitoSession session = ExtendedMockito.mockitoSession()
71                 .mockStatic(DebugUtils.class)
72                 .startMocking();
73         try {
74             when(DebugUtils.isDeveloperModeEnabled(mContext)).thenReturn(false);
75             assertFalse(mService.isEnabled());
76         } finally {
77             session.finishMocking();
78         }
79     }
80 
81     @Test
testWithBoundService()82     public void testWithBoundService() throws TimeoutException {
83         Intent serviceIntent = new Intent(mContext,
84                 OnDevicePersonalizationDebugServiceImpl.class);
85         IBinder binder = serviceRule.bindService(serviceIntent);
86         assertTrue(binder instanceof OnDevicePersonalizationDebugServiceDelegate);
87     }
88 
89 }
90