1 /*
2 * Copyright (C) 2016 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
17 #include <algorithm>
18
19 #include "gtest/gtest.h"
20
21 #include "chre/core/request_multiplexer.h"
22
23 using chre::RequestMultiplexer;
24
25 class DummyRequest {
26 public:
DummyRequest()27 DummyRequest() : DummyRequest(0) {}
28
DummyRequest(int priority)29 DummyRequest(int priority) : mPriority(priority) {}
30
isEquivalentTo(const DummyRequest & request) const31 bool isEquivalentTo(const DummyRequest& request) const {
32 return (mPriority == request.mPriority);
33 }
34
mergeWith(const DummyRequest & request)35 bool mergeWith(const DummyRequest& request) {
36 bool newMaximal = false;
37 if (request.mPriority > mPriority) {
38 mPriority = request.mPriority;
39 newMaximal = true;
40 }
41
42 return newMaximal;
43 }
44
getPriority() const45 int getPriority() const {
46 return mPriority;
47 }
48
49 private:
50 int mPriority;
51 };
52
TEST(RequestMultiplexer,DefaultRequestDoesNotCauseNewMaximal)53 TEST(RequestMultiplexer, DefaultRequestDoesNotCauseNewMaximal) {
54 RequestMultiplexer<DummyRequest> multiplexer;
55 DummyRequest request;
56 size_t index;
57 bool maximalRequestChanged;
58 ASSERT_TRUE(multiplexer.addRequest(request, &index, &maximalRequestChanged));
59 EXPECT_FALSE(maximalRequestChanged);
60 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 0);
61 }
62
TEST(RequestMultiplexer,FirstHighPriorityRequestCausesNewMaximal)63 TEST(RequestMultiplexer, FirstHighPriorityRequestCausesNewMaximal) {
64 RequestMultiplexer<DummyRequest> multiplexer;
65 DummyRequest request(10);
66 size_t index;
67 bool maximalRequestChanged;
68 ASSERT_TRUE(multiplexer.addRequest(request, &index, &maximalRequestChanged));
69 EXPECT_TRUE(maximalRequestChanged);
70 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
71 }
72
TEST(RequestMultiplexer,NewLowerPriorityRequestDoesNotCauseNewMaximal)73 TEST(RequestMultiplexer, NewLowerPriorityRequestDoesNotCauseNewMaximal) {
74 RequestMultiplexer<DummyRequest> multiplexer;
75 size_t index;
76
77 {
78 DummyRequest request(10);
79 bool maximalRequestChanged;
80 ASSERT_TRUE(multiplexer.addRequest(request, &index,
81 &maximalRequestChanged));
82 EXPECT_TRUE(maximalRequestChanged);
83 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
84 }
85
86 {
87 DummyRequest request(5);
88 bool maximalRequestChanged;
89 ASSERT_TRUE(multiplexer.addRequest(request, &index,
90 &maximalRequestChanged));
91 EXPECT_FALSE(maximalRequestChanged);
92 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
93 }
94 }
95
TEST(RequestMultiplexer,AddOneRemoveMaximal)96 TEST(RequestMultiplexer, AddOneRemoveMaximal) {
97 RequestMultiplexer<DummyRequest> multiplexer;
98 DummyRequest request(10);
99 size_t index;
100 bool maximalRequestChanged;
101 ASSERT_TRUE(multiplexer.addRequest(request, &index, &maximalRequestChanged));
102 EXPECT_TRUE(maximalRequestChanged);
103 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 10);
104 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
105
106 DummyRequest defaultRequest;
107 multiplexer.removeRequest(0, &maximalRequestChanged);
108 EXPECT_TRUE(maximalRequestChanged);
109 EXPECT_TRUE(multiplexer.getCurrentMaximalRequest()
110 .isEquivalentTo(defaultRequest));
111 EXPECT_TRUE(multiplexer.getRequests().empty());
112 }
113
TEST(RequestMultiplexer,AddManyRemoveMaximal)114 TEST(RequestMultiplexer, AddManyRemoveMaximal) {
115 RequestMultiplexer<DummyRequest> multiplexer;
116 size_t index;
117
118 {
119 DummyRequest request(10);
120 bool maximalRequestChanged;
121 ASSERT_TRUE(multiplexer.addRequest(request, &index,
122 &maximalRequestChanged));
123 EXPECT_TRUE(maximalRequestChanged);
124 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 10);
125 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
126 }
127
128 {
129 DummyRequest request(5);
130 bool maximalRequestChanged;
131 ASSERT_TRUE(multiplexer.addRequest(request, &index,
132 &maximalRequestChanged));
133 EXPECT_FALSE(maximalRequestChanged);
134 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 5);
135 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
136 }
137
138 {
139 DummyRequest request(10);
140 bool maximalRequestChanged;
141 ASSERT_TRUE(multiplexer.addRequest(request, &index,
142 &maximalRequestChanged));
143 EXPECT_FALSE(maximalRequestChanged);
144 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 10);
145 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
146 }
147
148 bool maximalRequestChanged;
149 multiplexer.removeRequest(0, &maximalRequestChanged);
150 EXPECT_FALSE(maximalRequestChanged);
151 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
152 EXPECT_EQ(multiplexer.getRequests()[0].getPriority(), 5);
153 EXPECT_EQ(multiplexer.getRequests()[1].getPriority(), 10);
154 }
155
TEST(RequestMultiplexer,AddManyRemoveBeforeMaximalThenRemoveMaximal)156 TEST(RequestMultiplexer, AddManyRemoveBeforeMaximalThenRemoveMaximal) {
157 RequestMultiplexer<DummyRequest> multiplexer;
158 size_t index;
159
160 {
161 DummyRequest request(1);
162 bool maximalRequestChanged;
163 ASSERT_TRUE(multiplexer.addRequest(request, &index,
164 &maximalRequestChanged));
165 EXPECT_TRUE(maximalRequestChanged);
166 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 1);
167 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 1);
168 }
169
170 {
171 DummyRequest request(5);
172 bool maximalRequestChanged;
173 ASSERT_TRUE(multiplexer.addRequest(request, &index,
174 &maximalRequestChanged));
175 EXPECT_TRUE(maximalRequestChanged);
176 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 5);
177 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 5);
178 }
179
180 {
181 DummyRequest request(10);
182 bool maximalRequestChanged;
183 ASSERT_TRUE(multiplexer.addRequest(request, &index,
184 &maximalRequestChanged));
185 EXPECT_TRUE(maximalRequestChanged);
186 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 10);
187 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
188 }
189
190 bool maximalRequestChanged;
191 multiplexer.removeRequest(0, &maximalRequestChanged);
192 EXPECT_FALSE(maximalRequestChanged);
193 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
194 EXPECT_EQ(multiplexer.getRequests()[0].getPriority(), 5);
195 EXPECT_EQ(multiplexer.getRequests()[1].getPriority(), 10);
196
197 multiplexer.removeRequest(1, &maximalRequestChanged);
198 EXPECT_TRUE(maximalRequestChanged);
199 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 5);
200 EXPECT_EQ(multiplexer.getRequests()[0].getPriority(), 5);
201 }
202
TEST(RequestMultiplexer,AddManyRemoveAfterMaximalThenRemoveMaximal)203 TEST(RequestMultiplexer, AddManyRemoveAfterMaximalThenRemoveMaximal) {
204 RequestMultiplexer<DummyRequest> multiplexer;
205 size_t index;
206
207 {
208 DummyRequest request(1);
209 bool maximalRequestChanged;
210 ASSERT_TRUE(multiplexer.addRequest(request, &index,
211 &maximalRequestChanged));
212 EXPECT_TRUE(maximalRequestChanged);
213 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 1);
214 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 1);
215 }
216
217 {
218 DummyRequest request(5);
219 bool maximalRequestChanged;
220 ASSERT_TRUE(multiplexer.addRequest(request, &index,
221 &maximalRequestChanged));
222 EXPECT_TRUE(maximalRequestChanged);
223 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 5);
224 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 5);
225 }
226
227 {
228 DummyRequest request(10);
229 bool maximalRequestChanged;
230 ASSERT_TRUE(multiplexer.addRequest(request, &index,
231 &maximalRequestChanged));
232 EXPECT_TRUE(maximalRequestChanged);
233 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 10);
234 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
235 }
236
237 {
238 DummyRequest request(5);
239 bool maximalRequestChanged;
240 ASSERT_TRUE(multiplexer.addRequest(request, &index,
241 &maximalRequestChanged));
242 EXPECT_FALSE(maximalRequestChanged);
243 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 5);
244 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
245 }
246
247 bool maximalRequestChanged;
248 multiplexer.removeRequest(3, &maximalRequestChanged);
249 EXPECT_FALSE(maximalRequestChanged);
250 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
251 EXPECT_EQ(multiplexer.getRequests()[0].getPriority(), 1);
252 EXPECT_EQ(multiplexer.getRequests()[1].getPriority(), 5);
253 EXPECT_EQ(multiplexer.getRequests()[2].getPriority(), 10);
254
255 multiplexer.removeRequest(2, &maximalRequestChanged);
256 EXPECT_TRUE(maximalRequestChanged);
257 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 5);
258 EXPECT_EQ(multiplexer.getRequests()[0].getPriority(), 1);
259 EXPECT_EQ(multiplexer.getRequests()[1].getPriority(), 5);
260 }
261
TEST(RequestMultiplexer,AddManyUpdateWithLowerPriority)262 TEST(RequestMultiplexer, AddManyUpdateWithLowerPriority) {
263 RequestMultiplexer<DummyRequest> multiplexer;
264 size_t index;
265
266 {
267 DummyRequest request(1);
268 bool maximalRequestChanged;
269 ASSERT_TRUE(multiplexer.addRequest(request, &index,
270 &maximalRequestChanged));
271 EXPECT_TRUE(maximalRequestChanged);
272 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 1);
273 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 1);
274 }
275
276 {
277 DummyRequest request(5);
278 bool maximalRequestChanged;
279 ASSERT_TRUE(multiplexer.addRequest(request, &index,
280 &maximalRequestChanged));
281 EXPECT_TRUE(maximalRequestChanged);
282 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 5);
283 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 5);
284 }
285
286 {
287 DummyRequest request(10);
288 bool maximalRequestChanged;
289 ASSERT_TRUE(multiplexer.addRequest(request, &index,
290 &maximalRequestChanged));
291 EXPECT_TRUE(maximalRequestChanged);
292 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 10);
293 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
294 }
295
296 {
297 DummyRequest request(8);
298 bool maximalRequestChanged;
299 multiplexer.updateRequest(1, request, &maximalRequestChanged);
300 EXPECT_FALSE(maximalRequestChanged);
301 EXPECT_EQ(multiplexer.getRequests()[1].getPriority(), 8);
302 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
303 }
304 }
305
TEST(RequestMultiplexer,AddManyUpdateWithNewMaximalLowerPriority)306 TEST(RequestMultiplexer, AddManyUpdateWithNewMaximalLowerPriority) {
307 RequestMultiplexer<DummyRequest> multiplexer;
308 size_t index;
309
310 {
311 DummyRequest request(1);
312 bool maximalRequestChanged;
313 ASSERT_TRUE(multiplexer.addRequest(request, &index,
314 &maximalRequestChanged));
315 EXPECT_TRUE(maximalRequestChanged);
316 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 1);
317 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 1);
318 }
319
320 {
321 DummyRequest request(5);
322 bool maximalRequestChanged;
323 ASSERT_TRUE(multiplexer.addRequest(request, &index,
324 &maximalRequestChanged));
325 EXPECT_TRUE(maximalRequestChanged);
326 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 5);
327 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 5);
328 }
329
330 {
331 DummyRequest request(10);
332 bool maximalRequestChanged;
333 ASSERT_TRUE(multiplexer.addRequest(request, &index,
334 &maximalRequestChanged));
335 EXPECT_TRUE(maximalRequestChanged);
336 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 10);
337 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
338 }
339
340 {
341 DummyRequest request(8);
342 bool maximalRequestChanged;
343 multiplexer.updateRequest(2, request, &maximalRequestChanged);
344 EXPECT_TRUE(maximalRequestChanged);
345 EXPECT_EQ(multiplexer.getRequests()[2].getPriority(), 8);
346 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 8);
347 }
348 }
349
TEST(RequestMultiplexer,AddManyUpdateNewMaximal)350 TEST(RequestMultiplexer, AddManyUpdateNewMaximal) {
351 RequestMultiplexer<DummyRequest> multiplexer;
352 size_t index;
353
354 {
355 DummyRequest request(1);
356 bool maximalRequestChanged;
357 ASSERT_TRUE(multiplexer.addRequest(request, &index,
358 &maximalRequestChanged));
359 EXPECT_TRUE(maximalRequestChanged);
360 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 1);
361 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 1);
362 }
363
364 {
365 DummyRequest request(5);
366 bool maximalRequestChanged;
367 ASSERT_TRUE(multiplexer.addRequest(request, &index,
368 &maximalRequestChanged));
369 EXPECT_TRUE(maximalRequestChanged);
370 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 5);
371 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 5);
372 }
373
374 {
375 DummyRequest request(10);
376 bool maximalRequestChanged;
377 ASSERT_TRUE(multiplexer.addRequest(request, &index,
378 &maximalRequestChanged));
379 EXPECT_TRUE(maximalRequestChanged);
380 EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 10);
381 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
382 }
383
384 {
385 DummyRequest request(20);
386 bool maximalRequestChanged;
387 multiplexer.updateRequest(1, request, &maximalRequestChanged);
388 EXPECT_TRUE(maximalRequestChanged);
389 EXPECT_EQ(multiplexer.getRequests()[1].getPriority(), 20);
390 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 20);
391 }
392 }
393
TEST(RequestMultiplexer,RemoveAllRequestsEmpty)394 TEST(RequestMultiplexer, RemoveAllRequestsEmpty) {
395 RequestMultiplexer<DummyRequest> multiplexer;
396
397 bool maximalRequestChanged;
398 multiplexer.removeAllRequests(&maximalRequestChanged);
399
400 EXPECT_FALSE(maximalRequestChanged);
401 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 0);
402 }
403
TEST(RequestMultiplexer,RemoveAllRequestsNonEmpty)404 TEST(RequestMultiplexer, RemoveAllRequestsNonEmpty) {
405 RequestMultiplexer<DummyRequest> multiplexer;
406 size_t index;
407
408 {
409 DummyRequest request(1);
410 bool maximalRequestChanged;
411 ASSERT_TRUE(multiplexer.addRequest(request, &index,
412 &maximalRequestChanged));
413 }
414
415 bool maximalRequestChanged;
416 multiplexer.removeAllRequests(&maximalRequestChanged);
417
418 EXPECT_TRUE(maximalRequestChanged);
419 EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 0);
420 }
421