OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.base; | |
6 | |
7 import android.test.InstrumentationTestCase; | |
8 import android.test.suitebuilder.annotation.SmallTest; | |
9 | |
10 import org.chromium.base.test.util.Feature; | |
11 | |
12 import java.lang.Iterable; | |
13 import java.util.Iterator; | |
14 import java.util.NoSuchElementException; | |
15 | |
16 /** | |
17 * Tests for (@link ObserverList}. | |
18 */ | |
19 public class ObserverListTest extends InstrumentationTestCase { | |
20 interface Observer { | |
21 void observe(int x); | |
22 } | |
23 | |
24 private static class Foo implements Observer { | |
25 private final int mScalar; | |
26 private int mTotal = 0; | |
27 | |
28 Foo(int scalar) { | |
29 mScalar = scalar; | |
30 } | |
31 | |
32 @Override | |
33 public void observe(int x) { | |
34 mTotal += x * mScalar; | |
35 } | |
36 } | |
37 | |
38 /** | |
39 * An observer which removes a given Observer object from the list when obse rve is called. | |
40 */ | |
41 private static class FooRemover implements Observer { | |
42 private final ObserverList<Observer> mList; | |
43 private final Observer mDoomed; | |
44 | |
45 FooRemover(ObserverList<Observer> list, Observer innocent) { | |
46 mList = list; | |
47 mDoomed = innocent; | |
48 } | |
49 | |
50 @Override | |
51 public void observe(int x) { | |
52 mList.removeObserver(mDoomed); | |
53 } | |
54 } | |
55 | |
56 private static <T> int getSizeOfIterable(Iterable<T> iterable) { | |
57 int num = 0; | |
58 for (T el : iterable) | |
59 num++; | |
60 return num; | |
61 } | |
62 | |
63 @SmallTest | |
64 @Feature({"Android-AppBase"}) | |
65 public void testRemoveWhileIteration() { | |
bulach
2013/02/28 12:39:12
it'd be nice to add a "testAddWhileIterating" and
nilesh
2013/02/28 19:35:36
Makes sense, add a test for addWhileIterating.
| |
66 ObserverList<Observer> observerList = new ObserverList<Observer>(); | |
67 Foo a, b, c, d, e; | |
bulach
2013/02/28 12:39:12
nit: I don't think this is strictly forbidden, but
nilesh
2013/02/28 19:35:36
Done.
| |
68 a = new Foo(1); | |
69 b = new Foo(-1); | |
70 c = new Foo(1); | |
71 d = new Foo(-1); | |
72 e = new Foo(-1); | |
73 FooRemover evil = new FooRemover(observerList, c); | |
74 | |
75 observerList.addObserver(a); | |
76 observerList.addObserver(b); | |
77 | |
78 for (Observer obs : observerList) | |
79 obs.observe(10); | |
80 | |
81 // Removing an observer not in the list should do nothing. | |
82 observerList.removeObserver(e); | |
83 | |
84 observerList.addObserver(evil); | |
85 observerList.addObserver(c); | |
86 observerList.addObserver(d); | |
87 | |
88 for (Observer obs : observerList) | |
89 obs.observe(10); | |
90 | |
91 // observe should be called twice on a. | |
92 assertEquals(20, a.mTotal); | |
93 // observe should be called twice on b. | |
94 assertEquals(-20, b.mTotal); | |
95 // evil removed c from the observerList before it got any callbacks. | |
96 assertEquals(0, c.mTotal); | |
97 // observe should be called once on d. | |
98 assertEquals(-10, d.mTotal); | |
99 // e was never added to the list, observe should not be called. | |
100 assertEquals(0, e.mTotal); | |
101 } | |
102 | |
103 @SmallTest | |
104 @Feature({"Android-AppBase"}) | |
105 public void testIterator() { | |
106 ObserverList<Integer> observerList = new ObserverList<Integer>(); | |
107 observerList.addObserver(5); | |
108 observerList.addObserver(10); | |
109 observerList.addObserver(15); | |
110 assertEquals(3, getSizeOfIterable(observerList)); | |
111 | |
112 observerList.removeObserver(10); | |
113 assertEquals(2, getSizeOfIterable(observerList)); | |
114 | |
115 Iterator<Integer> it = observerList.iterator(); | |
116 assertTrue(it.hasNext()); | |
117 assertTrue(5 == it.next()); | |
118 assertTrue(it.hasNext()); | |
119 assertTrue(15 == it.next()); | |
120 assertFalse(it.hasNext()); | |
121 | |
122 boolean removeExceptionThrown = false; | |
123 try { | |
124 it.remove(); | |
125 fail("Expecting UnsupportedOperationException to be thrown here."); | |
126 } catch (UnsupportedOperationException e) { | |
127 removeExceptionThrown = true; | |
128 } | |
129 assertTrue(removeExceptionThrown); | |
130 assertEquals(2, getSizeOfIterable(observerList)); | |
131 | |
132 boolean noElementExceptionThrown = false; | |
133 try { | |
134 it.next(); | |
135 fail("Expecting NoSuchElementException to be thrown here."); | |
136 } catch (NoSuchElementException e) { | |
137 noElementExceptionThrown = true; | |
138 } | |
139 assertTrue(noElementExceptionThrown); | |
140 } | |
141 } | |
OLD | NEW |