Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ae0059ff authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Add test for AdapterSuspend" into main

parents 3e11f1b8 0e32c564
Loading
Loading
Loading
Loading
+11 −2
Original line number Diff line number Diff line
@@ -27,6 +27,8 @@ import android.os.Looper;
import android.util.Log;
import android.view.Display;

import com.android.internal.annotations.VisibleForTesting;

import java.util.Arrays;

public class AdapterSuspend {
@@ -75,12 +77,18 @@ public class AdapterSuspend {
        mDisplayManager.unregisterDisplayListener(mDisplayListener);
    }

    @VisibleForTesting
    boolean isSuspended() {
        return mSuspended;
    }

    private boolean isScreenOn() {
        return Arrays.stream(mDisplayManager.getDisplays())
                .anyMatch(display -> display.getState() == Display.STATE_ON);
    }

    private void handleSuspend() {
    @VisibleForTesting
    void handleSuspend() {
        if (mSuspended) {
            return;
        }
@@ -102,7 +110,8 @@ public class AdapterSuspend {
        Log.i(TAG, "ready to suspend");
    }

    private void handleResume() {
    @VisibleForTesting
    void handleResume() {
        if (!mSuspended) {
            return;
        }
+106 −0
Original line number Diff line number Diff line
/*
 * Copyright 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.bluetooth.btservice;

import static android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE;
import static android.bluetooth.BluetoothAdapter.SCAN_MODE_NONE;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.verify;

import android.content.Context;
import android.hardware.display.DisplayManager;
import android.os.test.TestLooper;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class AdapterSuspendTest {
    private TestLooper mTestLooper;
    private DisplayManager mDisplayManager;
    private AdapterSuspend mAdapterSuspend;

    @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();
    @Mock private AdapterNativeInterface mAdapterNativeInterface;

    @Before
    public void setUp() throws Exception {
        Context context = InstrumentationRegistry.getTargetContext();
        mTestLooper = new TestLooper();
        mDisplayManager = context.getSystemService(DisplayManager.class);

        mAdapterSuspend =
                new AdapterSuspend(
                        mAdapterNativeInterface, mTestLooper.getLooper(), mDisplayManager);
    }

    private void triggerSuspend() throws Exception {
        mAdapterSuspend.handleSuspend();
    }

    private void triggerResume() throws Exception {
        mAdapterSuspend.handleResume();
    }

    private boolean isSuspended() throws Exception {
        return mAdapterSuspend.isSuspended();
    }

    @Test
    public void testSuspend() throws Exception {
        assertThat(isSuspended()).isFalse();

        triggerSuspend();

        verify(mAdapterNativeInterface).setDefaultEventMaskExcept(anyLong(), anyLong());
        verify(mAdapterNativeInterface)
                .setScanMode(AdapterService.convertScanModeToHal(SCAN_MODE_NONE));
        verify(mAdapterNativeInterface).clearEventFilter();
        verify(mAdapterNativeInterface).clearFilterAcceptList();
        verify(mAdapterNativeInterface).disconnectAllAcls();
        assertThat(isSuspended()).isTrue();
    }

    @Test
    public void testResume() throws Exception {
        triggerSuspend();
        assertThat(isSuspended()).isTrue();

        clearInvocations(mAdapterNativeInterface);
        triggerResume();

        verify(mAdapterNativeInterface).setDefaultEventMaskExcept(0, 0);
        verify(mAdapterNativeInterface).clearEventFilter();
        verify(mAdapterNativeInterface).restoreFilterAcceptList();
        verify(mAdapterNativeInterface)
                .setScanMode(AdapterService.convertScanModeToHal(SCAN_MODE_CONNECTABLE));
        assertThat(isSuspended()).isFalse();
    }
}