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

Commit 9e35df21 authored by Jimmy Shiu's avatar Jimmy Shiu Committed by Android (Google) Code Review
Browse files

Merge "Fixed flaky HintManagerServiceTest#testReportActualWorkDuration" into udc-dev

parents 67c74242 5ecea7cd
Loading
Loading
Loading
Loading
+26 −27
Original line number Original line Diff line number Diff line
@@ -57,6 +57,8 @@ import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.MockitoAnnotations;


import java.util.concurrent.CountDownLatch;

/**
/**
 * Tests for {@link com.android.server.power.hint.HintManagerService}.
 * Tests for {@link com.android.server.power.hint.HintManagerService}.
 *
 *
@@ -149,29 +151,28 @@ public class HintManagerServiceTest {
        // Set session to background and calling updateHintAllowed() would invoke pause();
        // Set session to background and calling updateHintAllowed() would invoke pause();
        service.mUidObserver.onUidStateChanged(
        service.mUidObserver.onUidStateChanged(
                a.mUid, ActivityManager.PROCESS_STATE_TRANSIENT_BACKGROUND, 0, 0);
                a.mUid, ActivityManager.PROCESS_STATE_TRANSIENT_BACKGROUND, 0, 0);
        final Object sync = new Object();

        // Using CountDownLatch to ensure above onUidStateChanged() job was digested.
        final CountDownLatch latch = new CountDownLatch(1);
        FgThread.getHandler().post(() -> {
        FgThread.getHandler().post(() -> {
            synchronized (sync) {
            latch.countDown();
                sync.notify();
            }
        });
        });
        synchronized (sync) {
        latch.await();
            sync.wait();

        }
        assumeFalse(a.updateHintAllowed());
        assumeFalse(a.updateHintAllowed());
        verify(mNativeWrapperMock, times(1)).halPauseHintSession(anyLong());
        verify(mNativeWrapperMock, times(1)).halPauseHintSession(anyLong());


        // Set session to foreground and calling updateHintAllowed() would invoke resume();
        // Set session to foreground and calling updateHintAllowed() would invoke resume();
        service.mUidObserver.onUidStateChanged(
        service.mUidObserver.onUidStateChanged(
                a.mUid, ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND, 0, 0);
                a.mUid, ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND, 0, 0);

        // Using CountDownLatch to ensure above onUidStateChanged() job was digested.
        final CountDownLatch latch2 = new CountDownLatch(1);
        FgThread.getHandler().post(() -> {
        FgThread.getHandler().post(() -> {
            synchronized (sync) {
            latch2.countDown();
                sync.notify();
            }
        });
        });
        synchronized (sync) {
        latch2.await();
            sync.wait();

        }
        assumeTrue(a.updateHintAllowed());
        assumeTrue(a.updateHintAllowed());
        verify(mNativeWrapperMock, times(1)).halResumeHintSession(anyLong());
        verify(mNativeWrapperMock, times(1)).halResumeHintSession(anyLong());
    }
    }
@@ -237,15 +238,14 @@ public class HintManagerServiceTest {
        // Set session to background, then the duration would not be updated.
        // Set session to background, then the duration would not be updated.
        service.mUidObserver.onUidStateChanged(
        service.mUidObserver.onUidStateChanged(
                a.mUid, ActivityManager.PROCESS_STATE_TRANSIENT_BACKGROUND, 0, 0);
                a.mUid, ActivityManager.PROCESS_STATE_TRANSIENT_BACKGROUND, 0, 0);
        final Object sync = new Object();

        // Using CountDownLatch to ensure above onUidStateChanged() job was digested.
        final CountDownLatch latch = new CountDownLatch(1);
        FgThread.getHandler().post(() -> {
        FgThread.getHandler().post(() -> {
            synchronized (sync) {
            latch.countDown();
                sync.notify();
            }
        });
        });
        synchronized (sync) {
        latch.await();
            sync.wait();

        }
        assumeFalse(a.updateHintAllowed());
        assumeFalse(a.updateHintAllowed());
        a.reportActualWorkDuration(DURATIONS_THREE, TIMESTAMPS_THREE);
        a.reportActualWorkDuration(DURATIONS_THREE, TIMESTAMPS_THREE);
        verify(mNativeWrapperMock, never()).halReportActualWorkDuration(anyLong(), any(), any());
        verify(mNativeWrapperMock, never()).halReportActualWorkDuration(anyLong(), any(), any());
@@ -287,15 +287,14 @@ public class HintManagerServiceTest {


        service.mUidObserver.onUidStateChanged(
        service.mUidObserver.onUidStateChanged(
                a.mUid, ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND, 0, 0);
                a.mUid, ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND, 0, 0);
        final Object sync = new Object();

        // Using CountDownLatch to ensure above onUidStateChanged() job was digested.
        final CountDownLatch latch = new CountDownLatch(1);
        FgThread.getHandler().post(() -> {
        FgThread.getHandler().post(() -> {
            synchronized (sync) {
            latch.countDown();
                sync.notify();
            }
        });
        });
        synchronized (sync) {
        latch.await();
            sync.wait();

        }
        assertFalse(a.updateHintAllowed());
        assertFalse(a.updateHintAllowed());
    }
    }