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

Commit 88c504cb authored by Vinit Nayak's avatar Vinit Nayak
Browse files

Only fire EvictionCallback when item actually evicted

In TaskKeyLruCache, we currently call the EvictionCallback
without checking if the entry was actually evicted or not.

fixes: 138952364
Test: Unit tests, manually inspected via debugger

Change-Id: Ic8e871f8e5a0324e4d00011d5bcd7036302b5c68
parent 2ef7b3ec
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ public class TaskKeyLruCache<V> extends TaskKeyCache<V> {

            @Override
            protected void entryRemoved(boolean evicted, Integer taskId, V oldV, V newV) {
                if (mEvictionCallback != null) {
                if (mEvictionCallback != null && evicted) {
                    mEvictionCallback.onEntryEvicted(mKeys.get(taskId));
                }

+40 −6
Original line number Diff line number Diff line
@@ -20,6 +20,10 @@ package com.android.systemui.shared.recents.model;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNull;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import android.test.suitebuilder.annotation.SmallTest;

import com.android.systemui.SysuiTestCase;
@@ -27,10 +31,10 @@ import com.android.systemui.SysuiTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.junit.MockitoJUnitRunner;

@SmallTest
@RunWith(JUnit4.class)
@RunWith(MockitoJUnitRunner.class)
public class TaskKeyLruCacheTest extends SysuiTestCase {
    private static int sCacheSize = 3;
    private static int sIdTask1 = 1;
@@ -38,13 +42,18 @@ public class TaskKeyLruCacheTest extends SysuiTestCase {
    private static int sIdTask3 = 3;
    private static int sIdUser1 = 1;

    TaskKeyCache<Integer> mCache = new TaskKeyLruCache<>(sCacheSize, null);
    Task.TaskKey mKey1;
    Task.TaskKey mKey2;
    Task.TaskKey mKey3;
    TaskKeyLruCache.EvictionCallback mEvictionCallback;

    TaskKeyLruCache<Integer> mCache;
    private Task.TaskKey mKey1;
    private Task.TaskKey mKey2;
    private Task.TaskKey mKey3;

    @Before
    public void setup() {
        mEvictionCallback = mock(TaskKeyLruCache.EvictionCallback.class);
        mCache = new TaskKeyLruCache<>(sCacheSize, mEvictionCallback);

        mKey1 = new Task.TaskKey(sIdTask1, 0, null, null, sIdUser1, System.currentTimeMillis());
        mKey2 = new Task.TaskKey(sIdTask2, 0, null, null, sIdUser1, System.currentTimeMillis());
        mKey3 = new Task.TaskKey(sIdTask3, 0, null, null, sIdUser1, System.currentTimeMillis());
@@ -90,6 +99,7 @@ public class TaskKeyLruCacheTest extends SysuiTestCase {
        assertNull(mCache.get(mKey1));
        assertEquals(3, mCache.mKeys.size());
        assertEquals(mKey2, mCache.mKeys.valueAt(0));
        verify(mEvictionCallback, times(1)).onEntryEvicted(mKey1);
    }

    @Test
@@ -102,5 +112,29 @@ public class TaskKeyLruCacheTest extends SysuiTestCase {

        assertNull(mCache.get(mKey2));
        assertEquals(2, mCache.mKeys.size());
        verify(mEvictionCallback, times(0)).onEntryEvicted(mKey2);
    }

    @Test
    public void put_evictionCallback_notCalled() {
        mCache.put(mKey1, 1);
        verify(mEvictionCallback, times(0)).onEntryEvicted(mKey1);
    }

    @Test
    public void evictAll_evictionCallback_called() {
        mCache.put(mKey1, 1);
        mCache.evictAllCache();
        verify(mEvictionCallback, times(1)).onEntryEvicted(mKey1);
    }

    @Test
    public void trimAll_evictionCallback_called() {
        mCache.put(mKey1, 1);
        mCache.put(mKey2, 2);
        mCache.trimToSize(-1);
        verify(mEvictionCallback, times(1)).onEntryEvicted(mKey1);
        verify(mEvictionCallback, times(1)).onEntryEvicted(mKey2);

    }
}