• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FakeRequest {
26  public:
FakeRequest()27   FakeRequest() : FakeRequest(0) {}
28 
FakeRequest(int priority)29   FakeRequest(int priority) : mPriority(priority) {}
30 
isEquivalentTo(const FakeRequest & request) const31   bool isEquivalentTo(const FakeRequest &request) const {
32     return (mPriority == request.mPriority);
33   }
34 
mergeWith(const FakeRequest & request)35   bool mergeWith(const FakeRequest &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<FakeRequest> multiplexer;
55   FakeRequest 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<FakeRequest> multiplexer;
65   FakeRequest 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<FakeRequest> multiplexer;
75   size_t index;
76 
77   {
78     FakeRequest request(10);
79     bool maximalRequestChanged;
80     ASSERT_TRUE(
81         multiplexer.addRequest(request, &index, &maximalRequestChanged));
82     EXPECT_TRUE(maximalRequestChanged);
83     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
84   }
85 
86   {
87     FakeRequest request(5);
88     bool maximalRequestChanged;
89     ASSERT_TRUE(
90         multiplexer.addRequest(request, &index, &maximalRequestChanged));
91     EXPECT_FALSE(maximalRequestChanged);
92     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
93   }
94 }
95 
TEST(RequestMultiplexer,AddOneRemoveMaximal)96 TEST(RequestMultiplexer, AddOneRemoveMaximal) {
97   RequestMultiplexer<FakeRequest> multiplexer;
98   FakeRequest 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   FakeRequest defaultRequest;
107   multiplexer.removeRequest(0, &maximalRequestChanged);
108   EXPECT_TRUE(maximalRequestChanged);
109   EXPECT_TRUE(
110       multiplexer.getCurrentMaximalRequest().isEquivalentTo(defaultRequest));
111   EXPECT_TRUE(multiplexer.getRequests().empty());
112 }
113 
TEST(RequestMultiplexer,AddManyRemoveMaximal)114 TEST(RequestMultiplexer, AddManyRemoveMaximal) {
115   RequestMultiplexer<FakeRequest> multiplexer;
116   size_t index;
117 
118   {
119     FakeRequest request(10);
120     bool maximalRequestChanged;
121     ASSERT_TRUE(
122         multiplexer.addRequest(request, &index, &maximalRequestChanged));
123     EXPECT_TRUE(maximalRequestChanged);
124     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 10);
125     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
126   }
127 
128   {
129     FakeRequest request(5);
130     bool maximalRequestChanged;
131     ASSERT_TRUE(
132         multiplexer.addRequest(request, &index, &maximalRequestChanged));
133     EXPECT_FALSE(maximalRequestChanged);
134     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 5);
135     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
136   }
137 
138   {
139     FakeRequest request(10);
140     bool maximalRequestChanged;
141     ASSERT_TRUE(
142         multiplexer.addRequest(request, &index, &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<FakeRequest> multiplexer;
158   size_t index;
159 
160   {
161     FakeRequest request(1);
162     bool maximalRequestChanged;
163     ASSERT_TRUE(
164         multiplexer.addRequest(request, &index, &maximalRequestChanged));
165     EXPECT_TRUE(maximalRequestChanged);
166     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 1);
167     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 1);
168   }
169 
170   {
171     FakeRequest request(5);
172     bool maximalRequestChanged;
173     ASSERT_TRUE(
174         multiplexer.addRequest(request, &index, &maximalRequestChanged));
175     EXPECT_TRUE(maximalRequestChanged);
176     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 5);
177     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 5);
178   }
179 
180   {
181     FakeRequest request(10);
182     bool maximalRequestChanged;
183     ASSERT_TRUE(
184         multiplexer.addRequest(request, &index, &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<FakeRequest> multiplexer;
205   size_t index;
206 
207   {
208     FakeRequest request(1);
209     bool maximalRequestChanged;
210     ASSERT_TRUE(
211         multiplexer.addRequest(request, &index, &maximalRequestChanged));
212     EXPECT_TRUE(maximalRequestChanged);
213     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 1);
214     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 1);
215   }
216 
217   {
218     FakeRequest request(5);
219     bool maximalRequestChanged;
220     ASSERT_TRUE(
221         multiplexer.addRequest(request, &index, &maximalRequestChanged));
222     EXPECT_TRUE(maximalRequestChanged);
223     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 5);
224     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 5);
225   }
226 
227   {
228     FakeRequest request(10);
229     bool maximalRequestChanged;
230     ASSERT_TRUE(
231         multiplexer.addRequest(request, &index, &maximalRequestChanged));
232     EXPECT_TRUE(maximalRequestChanged);
233     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 10);
234     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
235   }
236 
237   {
238     FakeRequest request(5);
239     bool maximalRequestChanged;
240     ASSERT_TRUE(
241         multiplexer.addRequest(request, &index, &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<FakeRequest> multiplexer;
264   size_t index;
265 
266   {
267     FakeRequest request(1);
268     bool maximalRequestChanged;
269     ASSERT_TRUE(
270         multiplexer.addRequest(request, &index, &maximalRequestChanged));
271     EXPECT_TRUE(maximalRequestChanged);
272     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 1);
273     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 1);
274   }
275 
276   {
277     FakeRequest request(5);
278     bool maximalRequestChanged;
279     ASSERT_TRUE(
280         multiplexer.addRequest(request, &index, &maximalRequestChanged));
281     EXPECT_TRUE(maximalRequestChanged);
282     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 5);
283     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 5);
284   }
285 
286   {
287     FakeRequest request(10);
288     bool maximalRequestChanged;
289     ASSERT_TRUE(
290         multiplexer.addRequest(request, &index, &maximalRequestChanged));
291     EXPECT_TRUE(maximalRequestChanged);
292     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 10);
293     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
294   }
295 
296   {
297     FakeRequest 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,AddManyUpdateWithLowerPriorityMoveSemantics)306 TEST(RequestMultiplexer, AddManyUpdateWithLowerPriorityMoveSemantics) {
307   RequestMultiplexer<FakeRequest> multiplexer;
308   size_t index;
309 
310   {
311     FakeRequest request(1);
312     bool maximalRequestChanged;
313     ASSERT_TRUE(multiplexer.addRequest(std::move(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     FakeRequest request(5);
322     bool maximalRequestChanged;
323     ASSERT_TRUE(multiplexer.addRequest(std::move(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     FakeRequest request(10);
332     bool maximalRequestChanged;
333     ASSERT_TRUE(multiplexer.addRequest(std::move(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     FakeRequest request(8);
342     bool maximalRequestChanged;
343     multiplexer.updateRequest(1, std::move(request), &maximalRequestChanged);
344     EXPECT_FALSE(maximalRequestChanged);
345     EXPECT_EQ(multiplexer.getRequests()[1].getPriority(), 8);
346     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
347   }
348 }
349 
TEST(RequestMultiplexer,AddManyUpdateWithNewMaximalLowerPriority)350 TEST(RequestMultiplexer, AddManyUpdateWithNewMaximalLowerPriority) {
351   RequestMultiplexer<FakeRequest> multiplexer;
352   size_t index;
353 
354   {
355     FakeRequest request(1);
356     bool maximalRequestChanged;
357     ASSERT_TRUE(
358         multiplexer.addRequest(request, &index, &maximalRequestChanged));
359     EXPECT_TRUE(maximalRequestChanged);
360     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 1);
361     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 1);
362   }
363 
364   {
365     FakeRequest request(5);
366     bool maximalRequestChanged;
367     ASSERT_TRUE(
368         multiplexer.addRequest(request, &index, &maximalRequestChanged));
369     EXPECT_TRUE(maximalRequestChanged);
370     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 5);
371     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 5);
372   }
373 
374   {
375     FakeRequest request(10);
376     bool maximalRequestChanged;
377     ASSERT_TRUE(
378         multiplexer.addRequest(request, &index, &maximalRequestChanged));
379     EXPECT_TRUE(maximalRequestChanged);
380     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 10);
381     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
382   }
383 
384   {
385     FakeRequest request(8);
386     bool maximalRequestChanged;
387     multiplexer.updateRequest(2, request, &maximalRequestChanged);
388     EXPECT_TRUE(maximalRequestChanged);
389     EXPECT_EQ(multiplexer.getRequests()[2].getPriority(), 8);
390     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 8);
391   }
392 }
393 
TEST(RequestMultiplexer,AddManyUpdateNewMaximal)394 TEST(RequestMultiplexer, AddManyUpdateNewMaximal) {
395   RequestMultiplexer<FakeRequest> multiplexer;
396   size_t index;
397 
398   {
399     FakeRequest request(1);
400     bool maximalRequestChanged;
401     ASSERT_TRUE(
402         multiplexer.addRequest(request, &index, &maximalRequestChanged));
403     EXPECT_TRUE(maximalRequestChanged);
404     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 1);
405     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 1);
406   }
407 
408   {
409     FakeRequest request(5);
410     bool maximalRequestChanged;
411     ASSERT_TRUE(
412         multiplexer.addRequest(request, &index, &maximalRequestChanged));
413     EXPECT_TRUE(maximalRequestChanged);
414     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 5);
415     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 5);
416   }
417 
418   {
419     FakeRequest request(10);
420     bool maximalRequestChanged;
421     ASSERT_TRUE(
422         multiplexer.addRequest(request, &index, &maximalRequestChanged));
423     EXPECT_TRUE(maximalRequestChanged);
424     EXPECT_EQ(multiplexer.getRequests()[index].getPriority(), 10);
425     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 10);
426   }
427 
428   {
429     FakeRequest request(20);
430     bool maximalRequestChanged;
431     multiplexer.updateRequest(1, request, &maximalRequestChanged);
432     EXPECT_TRUE(maximalRequestChanged);
433     EXPECT_EQ(multiplexer.getRequests()[1].getPriority(), 20);
434     EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 20);
435   }
436 }
437 
TEST(RequestMultiplexer,RemoveAllRequestsEmpty)438 TEST(RequestMultiplexer, RemoveAllRequestsEmpty) {
439   RequestMultiplexer<FakeRequest> multiplexer;
440 
441   bool maximalRequestChanged;
442   multiplexer.removeAllRequests(&maximalRequestChanged);
443 
444   EXPECT_FALSE(maximalRequestChanged);
445   EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 0);
446 }
447 
TEST(RequestMultiplexer,RemoveAllRequestsNonEmpty)448 TEST(RequestMultiplexer, RemoveAllRequestsNonEmpty) {
449   RequestMultiplexer<FakeRequest> multiplexer;
450   size_t index;
451 
452   {
453     FakeRequest request(1);
454     bool maximalRequestChanged;
455     ASSERT_TRUE(
456         multiplexer.addRequest(request, &index, &maximalRequestChanged));
457   }
458 
459   bool maximalRequestChanged;
460   multiplexer.removeAllRequests(&maximalRequestChanged);
461 
462   EXPECT_TRUE(maximalRequestChanged);
463   EXPECT_EQ(multiplexer.getCurrentMaximalRequest().getPriority(), 0);
464 }
465