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 17 package com.android.car.garagemode; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.atLeastOnce; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.content.ContentResolver; 26 import android.content.Context; 27 28 import androidx.test.ext.junit.runners.AndroidJUnit4; 29 import androidx.test.filters.SmallTest; 30 31 import com.android.car.internal.util.IndentingPrintWriter; 32 33 import org.junit.Before; 34 import org.junit.Rule; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.ArgumentCaptor; 38 import org.mockito.Captor; 39 import org.mockito.Mock; 40 import org.mockito.junit.MockitoJUnit; 41 import org.mockito.junit.MockitoRule; 42 43 import java.util.List; 44 45 @RunWith(AndroidJUnit4.class) 46 @SmallTest 47 public class GarageModeServiceTest { 48 @Rule public final MockitoRule rule = MockitoJUnit.rule(); 49 @Mock private Context mMockContext; 50 @Mock private GarageModeController mMockController; 51 @Mock private ContentResolver mMockContentResolver; 52 @Mock private IndentingPrintWriter mMockPrintWriter; 53 @Captor private ArgumentCaptor<String> mCaptorString; 54 55 private GarageModeService mService; 56 57 @Before setUp()58 public void setUp() { 59 when(mMockContext.getContentResolver()).thenReturn(mMockContentResolver); 60 mService = new GarageModeService(mMockContext, mMockController); 61 } 62 63 @Test testInitAndRelease()64 public void testInitAndRelease() { 65 mService.init(); 66 mService.release(); 67 68 verify(mMockController).init(); 69 verify(mMockController).release(); 70 } 71 72 @Test testDump_shouldSucceed()73 public void testDump_shouldSucceed() { 74 when(mMockController.isGarageModeActive()).thenReturn(true); 75 76 mService.dump(mMockPrintWriter); 77 verify(mMockPrintWriter, atLeastOnce()).println(mCaptorString.capture()); 78 List<String> strings = mCaptorString.getAllValues(); 79 assertThat(strings.get(0)).isEqualTo("GarageModeInProgress true"); 80 } 81 82 @Test testIsGarageModeActive_true()83 public void testIsGarageModeActive_true() { 84 when(mMockController.isGarageModeActive()).thenReturn(true); 85 86 assertThat(mService.isGarageModeActive()).isTrue(); 87 } 88 89 @Test testIsGarageModeActive_false()90 public void testIsGarageModeActive_false() { 91 when(mMockController.isGarageModeActive()).thenReturn(false); 92 93 assertThat(mService.isGarageModeActive()).isFalse(); 94 } 95 96 @Test testForceStartGarageMode()97 public void testForceStartGarageMode() { 98 mService.forceStartGarageMode(); 99 100 verify(mMockController).initiateGarageMode(null); 101 } 102 103 @Test testStopAndResetGarageMode()104 public void testStopAndResetGarageMode() { 105 mService.stopAndResetGarageMode(); 106 107 verify(mMockController).resetGarageMode(null); 108 } 109 110 } 111