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
diff --git a/android/app/src/com/android/bluetooth/avrcp/AvrcpHelperClasses.java b/android/app/src/com/android/bluetooth/avrcp/AvrcpHelperClasses.java
index 0311280..0012c02 100644
--- a/android/app/src/com/android/bluetooth/avrcp/AvrcpHelperClasses.java
+++ b/android/app/src/com/android/bluetooth/avrcp/AvrcpHelperClasses.java
@@ -368,14 +368,6 @@
}
@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 @@
@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/android/app/tests/src/com/android/bluetooth/avrcp/EvictingQueueTest.java b/android/app/tests/src/com/android/bluetooth/avrcp/EvictingQueueTest.java
new file mode 100644
index 0000000..643ce4d
--- /dev/null
+++ b/android/app/tests/src/com/android/bluetooth/avrcp/EvictingQueueTest.java
@@ -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);
+ }
+}