blob: 566ac4daf9502e58896f203e98ec6061d8b7f3a2 [file] [log] [blame]
Narayan Kamathf0202a92017-12-07 15:45:33 +00001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
19import android.os.WorkSource.WorkChain;
20
21import junit.framework.TestCase;
22
Narayan Kamath81822022017-12-08 11:56:01 +000023import java.util.ArrayList;
Narayan Kamathf0202a92017-12-07 15:45:33 +000024import java.util.List;
25
26/**
27 * Provides unit tests for hidden / unstable WorkSource APIs that are not CTS testable.
28 *
29 * These tests will be moved to CTS when finalized.
30 */
31public class WorkSourceTest extends TestCase {
32 public void testWorkChain_add() {
33 WorkChain wc1 = new WorkChain();
34 wc1.addNode(56, null);
35
36 assertEquals(56, wc1.getUids()[0]);
37 assertEquals(null, wc1.getTags()[0]);
38 assertEquals(1, wc1.getSize());
39
40 wc1.addNode(57, "foo");
41 assertEquals(56, wc1.getUids()[0]);
42 assertEquals(null, wc1.getTags()[0]);
43 assertEquals(57, wc1.getUids()[1]);
44 assertEquals("foo", wc1.getTags()[1]);
45
46 assertEquals(2, wc1.getSize());
47 }
48
49 public void testWorkChain_equalsHashCode() {
50 WorkChain wc1 = new WorkChain();
51 WorkChain wc2 = new WorkChain();
52
53 assertEquals(wc1, wc2);
54 assertEquals(wc1.hashCode(), wc2.hashCode());
55
56 wc1.addNode(1, null);
57 wc2.addNode(1, null);
58 assertEquals(wc1, wc2);
59 assertEquals(wc1.hashCode(), wc2.hashCode());
60
61 wc1.addNode(2, "tag");
62 wc2.addNode(2, "tag");
63 assertEquals(wc1, wc2);
64 assertEquals(wc1.hashCode(), wc2.hashCode());
65
66 wc1 = new WorkChain();
67 wc2 = new WorkChain();
68 wc1.addNode(5, null);
69 wc2.addNode(6, null);
70 assertFalse(wc1.equals(wc2));
71 assertFalse(wc1.hashCode() == wc2.hashCode());
72
73 wc1 = new WorkChain();
74 wc2 = new WorkChain();
75 wc1.addNode(5, "tag1");
76 wc2.addNode(5, "tag2");
77 assertFalse(wc1.equals(wc2));
78 assertFalse(wc1.hashCode() == wc2.hashCode());
79 }
80
81 public void testWorkChain_constructor() {
82 WorkChain wc1 = new WorkChain();
83 wc1.addNode(1, "foo")
84 .addNode(2, null)
85 .addNode(3, "baz");
86
87 WorkChain wc2 = new WorkChain(wc1);
88 assertEquals(wc1, wc2);
89
90 wc1.addNode(4, "baz");
91 assertFalse(wc1.equals(wc2));
92 }
93
94 public void testDiff_workChains() {
95 WorkSource ws1 = new WorkSource();
96 ws1.add(50);
97 ws1.createWorkChain().addNode(52, "foo");
98 WorkSource ws2 = new WorkSource();
99 ws2.add(50);
100 ws2.createWorkChain().addNode(60, "bar");
101
102 // Diffs don't take WorkChains into account for the sake of backward compatibility.
103 assertFalse(ws1.diff(ws2));
104 assertFalse(ws2.diff(ws1));
105 }
106
107 public void testEquals_workChains() {
108 WorkSource ws1 = new WorkSource();
109 ws1.add(50);
110 ws1.createWorkChain().addNode(52, "foo");
111
112 WorkSource ws2 = new WorkSource();
113 ws2.add(50);
114 ws2.createWorkChain().addNode(52, "foo");
115
116 assertEquals(ws1, ws2);
117
118 // Unequal number of WorkChains.
119 ws2.createWorkChain().addNode(53, "baz");
120 assertFalse(ws1.equals(ws2));
121
122 // Different WorkChain contents.
123 WorkSource ws3 = new WorkSource();
124 ws3.add(50);
125 ws3.createWorkChain().addNode(60, "bar");
126
127 assertFalse(ws1.equals(ws3));
128 assertFalse(ws3.equals(ws1));
129 }
130
131 public void testWorkSourceParcelling() {
132 WorkSource ws = new WorkSource();
133
134 WorkChain wc = ws.createWorkChain();
135 wc.addNode(56, "foo");
136 wc.addNode(75, "baz");
137 WorkChain wc2 = ws.createWorkChain();
138 wc2.addNode(20, "foo2");
139 wc2.addNode(30, "baz2");
140
141 Parcel p = Parcel.obtain();
142 ws.writeToParcel(p, 0);
143 p.setDataPosition(0);
144
145 WorkSource unparcelled = WorkSource.CREATOR.createFromParcel(p);
146
147 assertEquals(unparcelled, ws);
148 }
149
150 public void testSet_workChains() {
151 WorkSource ws1 = new WorkSource();
152 ws1.add(50);
153
154 WorkSource ws2 = new WorkSource();
155 ws2.add(60);
156 WorkChain wc = ws2.createWorkChain();
157 wc.addNode(75, "tag");
158
159 ws1.set(ws2);
160
161 // Assert that the WorkChains are copied across correctly to the new WorkSource object.
162 List<WorkChain> workChains = ws1.getWorkChains();
163 assertEquals(1, workChains.size());
164
165 assertEquals(1, workChains.get(0).getSize());
166 assertEquals(75, workChains.get(0).getUids()[0]);
167 assertEquals("tag", workChains.get(0).getTags()[0]);
168
169 // Also assert that a deep copy of workchains is made, so the addition of a new WorkChain
170 // or the modification of an existing WorkChain has no effect.
171 ws2.createWorkChain();
172 assertEquals(1, ws1.getWorkChains().size());
173
174 wc.addNode(50, "tag2");
175 assertEquals(1, ws1.getWorkChains().size());
176 assertEquals(1, ws1.getWorkChains().get(0).getSize());
177 }
178
179 public void testSet_nullWorkChain() {
180 WorkSource ws = new WorkSource();
181 ws.add(60);
182 WorkChain wc = ws.createWorkChain();
183 wc.addNode(75, "tag");
184
185 ws.set(null);
186 assertEquals(0, ws.getWorkChains().size());
187 }
188
189 public void testAdd_workChains() {
190 WorkSource ws = new WorkSource();
191 ws.createWorkChain().addNode(70, "foo");
192
193 WorkSource ws2 = new WorkSource();
194 ws2.createWorkChain().addNode(60, "tag");
195
196 ws.add(ws2);
197
198 // Check that the new WorkChain is added to the end of the list.
199 List<WorkChain> workChains = ws.getWorkChains();
200 assertEquals(2, workChains.size());
201 assertEquals(1, workChains.get(1).getSize());
202 assertEquals(60, ws.getWorkChains().get(1).getUids()[0]);
203 assertEquals("tag", ws.getWorkChains().get(1).getTags()[0]);
204
205 // Adding the same WorkChain twice should be a no-op.
206 ws.add(ws2);
207 assertEquals(2, workChains.size());
208 }
Narayan Kamath6192f732017-12-21 09:43:38 +0000209
210 public void testSet_noWorkChains() {
211 WorkSource ws = new WorkSource();
212 ws.set(10);
213 assertEquals(1, ws.size());
214 assertEquals(10, ws.get(0));
215
216 WorkSource ws2 = new WorkSource();
217 ws2.set(20, "foo");
218 assertEquals(1, ws2.size());
219 assertEquals(20, ws2.get(0));
220 assertEquals("foo", ws2.getName(0));
221 }
Narayan Kamath81822022017-12-08 11:56:01 +0000222
223 public void testDiffChains_noChanges() {
224 // WorkSources with no chains.
225 assertEquals(null, WorkSource.diffChains(new WorkSource(), new WorkSource()));
226
227 // WorkSources with the same chains.
228 WorkSource ws1 = new WorkSource();
229 ws1.createWorkChain().addNode(50, "tag");
230 ws1.createWorkChain().addNode(60, "tag2");
231
232 WorkSource ws2 = new WorkSource();
233 ws2.createWorkChain().addNode(50, "tag");
234 ws2.createWorkChain().addNode(60, "tag2");
235
236 assertEquals(null, WorkSource.diffChains(ws1, ws1));
237 assertEquals(null, WorkSource.diffChains(ws2, ws1));
238 }
239
240 public void testDiffChains_noChains() {
241 // Diffs against a worksource with no chains.
242 WorkSource ws1 = new WorkSource();
243 WorkSource ws2 = new WorkSource();
244 ws2.createWorkChain().addNode(70, "tag");
245 ws2.createWorkChain().addNode(60, "tag2");
246
247 // The "old" work source has no chains, so "newChains" should be non-null.
248 ArrayList<WorkChain>[] diffs = WorkSource.diffChains(ws1, ws2);
249 assertNotNull(diffs[0]);
250 assertNull(diffs[1]);
251 assertEquals(2, diffs[0].size());
252 assertEquals(ws2.getWorkChains(), diffs[0]);
253
254 // The "new" work source has no chains, so "oldChains" should be non-null.
255 diffs = WorkSource.diffChains(ws2, ws1);
256 assertNull(diffs[0]);
257 assertNotNull(diffs[1]);
258 assertEquals(2, diffs[1].size());
259 assertEquals(ws2.getWorkChains(), diffs[1]);
260 }
261
262 public void testDiffChains_onlyAdditionsOrRemovals() {
263 WorkSource ws1 = new WorkSource();
264 WorkSource ws2 = new WorkSource();
265 ws2.createWorkChain().addNode(70, "tag");
266 ws2.createWorkChain().addNode(60, "tag2");
267
268 // Both work sources have WorkChains : test the case where changes were only added
269 // or were only removed.
270 ws1.createWorkChain().addNode(70, "tag");
271
272 // The "new" work source only contains additions (60, "tag2") in this case.
273 ArrayList<WorkChain>[] diffs = WorkSource.diffChains(ws1, ws2);
274 assertNotNull(diffs[0]);
275 assertNull(diffs[1]);
276 assertEquals(1, diffs[0].size());
277 assertEquals(new WorkChain().addNode(60, "tag2"), diffs[0].get(0));
278
279 // The "new" work source only contains removals (60, "tag2") in this case.
280 diffs = WorkSource.diffChains(ws2, ws1);
281 assertNull(diffs[0]);
282 assertNotNull(diffs[1]);
283 assertEquals(1, diffs[1].size());
284 assertEquals(new WorkChain().addNode(60, "tag2"), diffs[1].get(0));
285 }
286
287
288 public void testDiffChains_generalCase() {
289 WorkSource ws1 = new WorkSource();
290 WorkSource ws2 = new WorkSource();
291
292 // Both work sources have WorkChains, test the case where chains were added AND removed.
293 ws1.createWorkChain().addNode(0, "tag0");
294 ws2.createWorkChain().addNode(0, "tag0_changed");
295 ArrayList<WorkChain>[] diffs = WorkSource.diffChains(ws1, ws2);
296 assertNotNull(diffs[0]);
297 assertNotNull(diffs[1]);
298 assertEquals(ws2.getWorkChains(), diffs[0]);
299 assertEquals(ws1.getWorkChains(), diffs[1]);
300
301 // Give both WorkSources a chain in common; it should not be a part of any diffs.
302 ws1.createWorkChain().addNode(1, "tag1");
303 ws2.createWorkChain().addNode(1, "tag1");
304 diffs = WorkSource.diffChains(ws1, ws2);
305 assertNotNull(diffs[0]);
306 assertNotNull(diffs[1]);
307 assertEquals(1, diffs[0].size());
308 assertEquals(1, diffs[1].size());
309 assertEquals(new WorkChain().addNode(0, "tag0_changed"), diffs[0].get(0));
310 assertEquals(new WorkChain().addNode(0, "tag0"), diffs[1].get(0));
311
312 // Finally, test the case where more than one chain was added / removed.
313 ws1.createWorkChain().addNode(2, "tag2");
314 ws2.createWorkChain().addNode(2, "tag2_changed");
315 diffs = WorkSource.diffChains(ws1, ws2);
316 assertNotNull(diffs[0]);
317 assertNotNull(diffs[1]);
318 assertEquals(2, diffs[0].size());
319 assertEquals(2, diffs[1].size());
320 assertEquals(new WorkChain().addNode(0, "tag0_changed"), diffs[0].get(0));
321 assertEquals(new WorkChain().addNode(2, "tag2_changed"), diffs[0].get(1));
322 assertEquals(new WorkChain().addNode(0, "tag0"), diffs[1].get(0));
323 assertEquals(new WorkChain().addNode(2, "tag2"), diffs[1].get(1));
324 }
Narayan Kamathcbe06772017-12-27 14:22:47 +0000325
326 public void testGetAttributionId() {
327 WorkSource ws1 = new WorkSource();
328 WorkChain wc = ws1.createWorkChain();
329 wc.addNode(100, "tag");
330 assertEquals(100, wc.getAttributionUid());
331 wc.addNode(200, "tag2");
332 assertEquals(100, wc.getAttributionUid());
333 }
Narayan Kamathee07f622018-01-08 12:20:28 +0000334
335 public void testRemove_fromChainedWorkSource() {
336 WorkSource ws1 = new WorkSource();
337 ws1.createWorkChain().addNode(50, "foo");
338 ws1.createWorkChain().addNode(75, "bar");
339 ws1.add(100);
340
341 WorkSource ws2 = new WorkSource();
342 ws2.add(100);
343
344 assertTrue(ws1.remove(ws2));
345 assertEquals(2, ws1.getWorkChains().size());
346 assertEquals(50, ws1.getWorkChains().get(0).getAttributionUid());
347 assertEquals(75, ws1.getWorkChains().get(1).getAttributionUid());
348
349 ws2.createWorkChain().addNode(50, "foo");
350 assertTrue(ws1.remove(ws2));
351 assertEquals(1, ws1.getWorkChains().size());
352 assertEquals(75, ws1.getWorkChains().get(0).getAttributionUid());
353 }
Narayan Kamathf0202a92017-12-07 15:45:33 +0000354}