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

Commit 2119e715 authored by Marie Janssen's avatar Marie Janssen
Browse files

AVRCP: Fix EvictingQueue

Fixes EvictingQueue to not overflow the stack when a thing is added.

Test: runtest bluetooth -c com.android.bluetooth.avrcp.EvictingQueueTest
Bug: 33828042
Change-Id: I35f7f89152ff45edfacfe2c7e673adc1f31e1b3e
(cherry picked from commit 5e65f55d906687a682b4c7bbc9372084e4df3e53)
parent 41b339ef
Loading
Loading
Loading
Loading
+4 −14
Original line number Diff line number Diff line
@@ -367,14 +367,6 @@ class EvictingQueue<E> extends ArrayDeque<E> {
        mMaxSize = maxSize;
    }

    @Override
    public boolean add(E e) {
        if (super.size() == mMaxSize) {
            super.remove();
        }
        return super.add(e);
    }

    @Override
    public void addFirst(E e) {
        if (super.size() == mMaxSize) return;
@@ -383,12 +375,10 @@ class EvictingQueue<E> extends ArrayDeque<E> {

    @Override
    public void addLast(E e) {
        add(e);
        if (super.size() == mMaxSize) {
            super.remove();
        }

    @Override
    public boolean offer(E e) {
        return offerLast(e);
        super.addLast(e);
    }

    @Override
+48 −0
Original line number Diff line number Diff line
package com.android.bluetooth.avrcp;

import android.test.AndroidTestCase;

import junit.framework.Assert;

/** Unit tests for {@link EvictingQueue}. */
public class EvictingQueueTest extends AndroidTestCase {
    public void testEvictingQueue_canAddItems() {
        EvictingQueue<Integer> e = new EvictingQueue<Integer>(10);

        e.add(1);

        assertEquals((long) e.size(), (long) 1);
    }

    public void testEvictingQueue_maxItems() {
        EvictingQueue<Integer> e = new EvictingQueue<Integer>(5);

        e.add(1);
        e.add(2);
        e.add(3);
        e.add(4);
        e.add(5);
        e.add(6);

        assertEquals((long) e.size(), (long) 5);
        // Items drop off the front
        assertEquals((long) e.peek(), (long) 2);
    }

    public void testEvictingQueue_frontDrop() {
        EvictingQueue<Integer> e = new EvictingQueue<Integer>(5);

        e.add(1);
        e.add(2);
        e.add(3);
        e.add(4);
        e.add(5);

        assertEquals((long) e.size(), (long) 5);

        e.addFirst(6);

        assertEquals((long) e.size(), (long) 5);
        assertEquals((long) e.peek(), (long) 1);
    }
}