OSDN Git Service

AVRCP: Fix EvictingQueue
authorMarie Janssen <jamuraa@google.com>
Thu, 20 Apr 2017 19:42:14 +0000 (12:42 -0700)
committerMarie Janssen <jamuraa@google.com>
Thu, 20 Apr 2017 20:01:34 +0000 (13:01 -0700)
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)

src/com/android/bluetooth/avrcp/AvrcpHelperClasses.java
tests/src/com/android/bluetooth/avrcp/EvictingQueueTest.java [new file with mode: 0644]

index 0311280..0012c02 100644 (file)
@@ -368,14 +368,6 @@ class EvictingQueue<E> extends ArrayDeque<E> {
     }
 
     @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;
         super.addFirst(e);
@@ -383,12 +375,10 @@ class EvictingQueue<E> extends ArrayDeque<E> {
 
     @Override
     public void addLast(E e) {
-        add(e);
-    }
-
-    @Override
-    public boolean offer(E e) {
-        return offerLast(e);
+        if (super.size() == mMaxSize) {
+            super.remove();
+        }
+        super.addLast(e);
     }
 
     @Override
diff --git a/tests/src/com/android/bluetooth/avrcp/EvictingQueueTest.java b/tests/src/com/android/bluetooth/avrcp/EvictingQueueTest.java
new file mode 100644 (file)
index 0000000..643ce4d
--- /dev/null
@@ -0,0 +1,48 @@
+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);
+    }
+}