File: overlay_combination_cache_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (541 lines) | stat: -rw-r--r-- 21,965 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <algorithm>
#include <cstddef>
#include <memory>
#include <vector>

#include "base/containers/span.h"
#include "components/viz/common/quads/compositor_render_pass.h"
#include "components/viz/common/resources/resource_id.h"
#include "components/viz/service/display/overlay_candidate.h"
#include "components/viz/service/display/overlay_combination_cache.h"
#include "components/viz/service/display/overlay_processor_interface.h"
#include "components/viz/service/display/overlay_processor_strategy.h"
#include "components/viz/service/display/overlay_processor_using_strategy.h"
#include "components/viz/service/display/overlay_proposed_candidate.h"
#include "components/viz/service/display/overlay_strategy_single_on_top.h"
#include "components/viz/service/display/overlay_strategy_underlay.h"
#include "components/viz/test/compositor_frame_helpers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::ElementsAre;
using testing::ElementsAreArray;
using CandidateCombination = viz::OverlayCombinationCache::CandidateCombination;
using CandidateId = viz::OverlayCombinationCache::CandidateId;

namespace viz {
namespace {

// We only need this class to pass a pointer to OverlayStrategySingleOnTop.
class TestOverlayProcessor : public OverlayProcessorUsingStrategy {
  bool IsOverlaySupported() const override { return true; }
  bool NeedsSurfaceDamageRectList() const override { return false; }
  void CheckOverlaySupportImpl(
      const OverlayProcessorInterface::OutputSurfaceOverlayPlane* primary_plane,
      OverlayCandidateList* surfaces) override {}
  size_t GetStrategyCount() const { return 1; }
};

}  // namespace

class OverlayCombinationCacheTest : public testing::Test {
 public:
  OverlayCombinationCacheTest()
      : on_top_strategy_(&overlay_processor_),
        underlay_strategy_(&overlay_processor_) {}

 protected:
  // Access private methods on OverlayCombinationCache for testing.
  std::vector<OverlayProposedCandidate> GetConsideredCandidates(
      base::span<OverlayProposedCandidate const> sorted_candidates,
      size_t max_overlays_possible) {
    return combination_cache_.GetConsideredCandidates(sorted_candidates,
                                                      max_overlays_possible);
  }
  std::vector<CandidateId> GetIds(
      const std::vector<OverlayProposedCandidate>& considered_candidates) {
    return combination_cache_.GetIds(considered_candidates);
  }
  std::vector<std::pair<CandidateCombination, int>> GetPowerSortedCombinations(
      const std::vector<OverlayProposedCandidate>& considered_candidates,
      const std::vector<CandidateId>& considered_ids) {
    return combination_cache_.GetPowerSortedCombinations(considered_candidates,
                                                         considered_ids);
  }

  // Create N candidates with power gains from N..1
  std::vector<OverlayProposedCandidate> MakeCandidates(int num_candidates) {
    std::vector<int> power_gains;
    int order = num_candidates;
    for (int i = 0; i < num_candidates; ++i) {
      power_gains.push_back(order--);
    }
    return MakeCandidatesWithPowerGains(power_gains);
  }

  std::vector<OverlayProposedCandidate> MakeCandidatesWithPowerGains(
      std::vector<int> power_gains) {
    std::vector<OverlayProposedCandidate> proposed_candidates;

    // Use RenderPassBuilder to easily make a QuadList.
    CompositorRenderPassId id{1};
    RenderPassBuilder pass_builder(id, gfx::Rect(0, 0, 100, 100));
    for (size_t i = 0; i < power_gains.size(); ++i) {
      ResourceId resource_id(i + 1);
      pass_builder.AddTextureQuad(gfx::Rect(0, 0, 10, 10), resource_id);
    }
    render_pass_ = pass_builder.Build();

    auto quad_iter = render_pass_->quad_list.begin();
    for (size_t i = 0; i < power_gains.size(); ++i) {
      OverlayCandidate candidate;
      // Give every candidate a unique position so they get different cache keys
      candidate.display_rect.SetRect(i, 0, 10, 10);
      OverlayProposedCandidate proposed_candidate(quad_iter, candidate,
                                                  &on_top_strategy_);
      proposed_candidate.relative_power_gain = power_gains[i];
      proposed_candidates.push_back(proposed_candidate);

      quad_iter++;
    }
    // Canadidate are supposed to be sorted by relative_power_gain.
    std::sort(proposed_candidates.begin(), proposed_candidates.end(),
              [](const auto& cand1, const auto& cand2) {
                return cand1.relative_power_gain > cand2.relative_power_gain;
              });

    return proposed_candidates;
  }

  void ExpectPowerGains(const std::vector<OverlayProposedCandidate>& result,
                        const std::vector<int>& expected_power_gains) const {
    ASSERT_EQ(result.size(), expected_power_gains.size());
    for (size_t i = 0; i < result.size(); ++i) {
      EXPECT_EQ(result[i].relative_power_gain, expected_power_gains[i]);
    }
  }

  OverlayCombinationCache combination_cache_;
  CombinationIdMapper id_mapper_;
  TestOverlayProcessor overlay_processor_;
  OverlayStrategySingleOnTop on_top_strategy_;
  OverlayStrategyUnderlay underlay_strategy_;
  std::unique_ptr<CompositorRenderPass> render_pass_;
};

TEST_F(OverlayCombinationCacheTest, NoCandidates) {
  std::vector<OverlayProposedCandidate> sorted_candidates;

  auto result =
      combination_cache_.GetOverlayCombinationToTest(sorted_candidates, 2);

  // No candidates of course.
  EXPECT_TRUE(result.candidates_to_test.empty());
  EXPECT_FALSE(result.previously_succeeded);
}

TEST_F(OverlayCombinationCacheTest, AllCombosFail) {
  auto sorted_candidates = MakeCandidatesWithPowerGains({10, 5, 1});

  // Descending list sorted by sum of members. Once all combinations have failed
  // we'll resort to just attempting the highest candidate until a cache update.
  std::vector<std::vector<int>> expectations = {
      {10, 5, 1}, {10, 5}, {10, 1}, {10}, {5, 1}, {5}, {1}, {10}, {10}, {10}};

  for (size_t i = 0; i < expectations.size(); ++i) {
    SCOPED_TRACE(i);
    auto result =
        combination_cache_.GetOverlayCombinationToTest(sorted_candidates, 3);

    ExpectPowerGains(result.candidates_to_test, expectations[i]);
    EXPECT_FALSE(result.previously_succeeded);

    // No candidates promoted.
    for (auto& proposed_candidate : result.candidates_to_test) {
      proposed_candidate.candidate.overlay_handled = false;
    }
    combination_cache_.DeclarePromotedCandidates(result.candidates_to_test);
  }
}

TEST_F(OverlayCombinationCacheTest, SomePass) {
  auto sorted_candidates = MakeCandidatesWithPowerGains({10, 5, 1});
  {
    auto result =
        combination_cache_.GetOverlayCombinationToTest(sorted_candidates, 3);

    // Expect all to be tested
    ExpectPowerGains(result.candidates_to_test, {10, 5, 1});
    EXPECT_FALSE(result.previously_succeeded);

    // All candidates promoted.
    for (auto& proposed_candidate : result.candidates_to_test) {
      proposed_candidate.candidate.overlay_handled = true;
    }
    combination_cache_.DeclarePromotedCandidates(result.candidates_to_test);
  }
  {  // Same candidates next frame.
    auto result =
        combination_cache_.GetOverlayCombinationToTest(sorted_candidates, 3);

    // Same combination tested because it passed last time.
    ExpectPowerGains(result.candidates_to_test, {10, 5, 1});
    // This passed last time.
    EXPECT_TRUE(result.previously_succeeded);

    // Only first two pass.
    result.candidates_to_test[0].candidate.overlay_handled = true;
    result.candidates_to_test[1].candidate.overlay_handled = true;
    result.candidates_to_test[2].candidate.overlay_handled = false;
    combination_cache_.DeclarePromotedCandidates(result.candidates_to_test);
  }
  {  // Same candidates next frame.
    auto result =
        combination_cache_.GetOverlayCombinationToTest(sorted_candidates, 3);

    // Next best combination tested.
    ExpectPowerGains(result.candidates_to_test, {10, 5});
    // This passed last time.
    EXPECT_TRUE(result.previously_succeeded);

    // Both fail.
    result.candidates_to_test[0].candidate.overlay_handled = false;
    result.candidates_to_test[1].candidate.overlay_handled = false;
    combination_cache_.DeclarePromotedCandidates(result.candidates_to_test);
  }
  {  // Same candidates next frame.
    auto result =
        combination_cache_.GetOverlayCombinationToTest(sorted_candidates, 3);

    // Next best combination tested.
    ExpectPowerGains(result.candidates_to_test, {10, 1});
    // Not tested before.
    EXPECT_FALSE(result.previously_succeeded);
  }
}

TEST_F(OverlayCombinationCacheTest, CandidatesChange) {
  auto sorted_candidates = MakeCandidatesWithPowerGains({10, 5, 1});
  {
    auto result =
        combination_cache_.GetOverlayCombinationToTest(sorted_candidates, 3);

    // Expect all to be tested
    ExpectPowerGains(result.candidates_to_test, {10, 5, 1});
    // Not tested before.
    EXPECT_FALSE(result.previously_succeeded);

    // All candidates promoted.
    for (auto& proposed_candidate : result.candidates_to_test) {
      proposed_candidate.candidate.overlay_handled = true;
    }
    combination_cache_.DeclarePromotedCandidates(result.candidates_to_test);
  }
  {
    // First candidate has less power savings this frame, but candidates are the
    // same.
    sorted_candidates = MakeCandidatesWithPowerGains({3, 5, 1});
    auto result =
        combination_cache_.GetOverlayCombinationToTest(sorted_candidates, 3);

    // Expect all to be tested, but order is different.
    ExpectPowerGains(result.candidates_to_test, {5, 3, 1});
    // This is the same combination that passed last frame.
    EXPECT_TRUE(result.previously_succeeded);

    // No candidates promoted.
    for (auto& proposed_candidate : result.candidates_to_test) {
      proposed_candidate.candidate.overlay_handled = false;
    }
    combination_cache_.DeclarePromotedCandidates(result.candidates_to_test);
  }
  {
    // Same candidates this frame.
    auto result =
        combination_cache_.GetOverlayCombinationToTest(sorted_candidates, 3);

    // Next best combo tested.
    ExpectPowerGains(result.candidates_to_test, {5, 3});
    // Not tested before.
    EXPECT_FALSE(result.previously_succeeded);

    // All candidates promoted.
    for (auto& proposed_candidate : result.candidates_to_test) {
      proposed_candidate.candidate.overlay_handled = true;
    }
    combination_cache_.DeclarePromotedCandidates(result.candidates_to_test);
  }
  {
    // Third candidate moves a little this frame.
    sorted_candidates[2].candidate.display_rect.set_y(2);
    auto result =
        combination_cache_.GetOverlayCombinationToTest(sorted_candidates, 3);

    // That inavlidated cache results containing the third candidate, so now the
    // combination of all candidates gets tested again.
    ExpectPowerGains(result.candidates_to_test, {5, 3, 1});
    // Not tested before.
    EXPECT_FALSE(result.previously_succeeded);

    // No candidates promoted.
    for (auto& proposed_candidate : result.candidates_to_test) {
      proposed_candidate.candidate.overlay_handled = false;
    }
    combination_cache_.DeclarePromotedCandidates(result.candidates_to_test);
  }
  {
    // Same candidates this frame.
    auto result =
        combination_cache_.GetOverlayCombinationToTest(sorted_candidates, 3);

    // Next best combo tested.
    ExpectPowerGains(result.candidates_to_test, {5, 3});
    // This was tested before, because the third candidate moving didn't
    // invalidate this cache entry.
    EXPECT_TRUE(result.previously_succeeded);
  }
}

TEST_F(OverlayCombinationCacheTest, ClearCache) {
  auto sorted_candidates = MakeCandidatesWithPowerGains({10, 5, 1});
  {
    auto result =
        combination_cache_.GetOverlayCombinationToTest(sorted_candidates, 3);

    // Expect all to be tested
    ExpectPowerGains(result.candidates_to_test, {10, 5, 1});
    // Not tested before.
    EXPECT_FALSE(result.previously_succeeded);

    // No candidates promoted.
    for (auto& proposed_candidate : result.candidates_to_test) {
      proposed_candidate.candidate.overlay_handled = false;
    }
    combination_cache_.DeclarePromotedCandidates(result.candidates_to_test);
  }
  combination_cache_.ClearCache();
  {
    // Same candidates this frame.
    auto result =
        combination_cache_.GetOverlayCombinationToTest(sorted_candidates, 3);

    // Same combination tested because the cache was cleared.
    ExpectPowerGains(result.candidates_to_test, {10, 5, 1});
    // Not tested before.
    EXPECT_FALSE(result.previously_succeeded);

    // All candidates promoted.
    for (auto& proposed_candidate : result.candidates_to_test) {
      proposed_candidate.candidate.overlay_handled = true;
    }
    combination_cache_.DeclarePromotedCandidates(result.candidates_to_test);
  }
  combination_cache_.ClearCache();
  {
    // Same candidates this frame.
    auto result =
        combination_cache_.GetOverlayCombinationToTest(sorted_candidates, 3);

    // Best combination tested.
    ExpectPowerGains(result.candidates_to_test, {10, 5, 1});
    // This succeeded last frame, but the cache was cleared so this is false.
    EXPECT_FALSE(result.previously_succeeded);
  }
}

TEST_F(OverlayCombinationCacheTest, GetConsideredCandidatesUnique) {
  // All 3 candidates will be unique
  auto sorted_candidates = MakeCandidates(3);

  auto considered_candidates = GetConsideredCandidates(sorted_candidates, 4);
  ExpectPowerGains(considered_candidates, {3, 2, 1});

  considered_candidates = GetConsideredCandidates(sorted_candidates, 3);
  ExpectPowerGains(considered_candidates, {3, 2, 1});

  considered_candidates = GetConsideredCandidates(sorted_candidates, 2);
  ExpectPowerGains(considered_candidates, {3, 2});

  considered_candidates = GetConsideredCandidates(sorted_candidates, 1);
  ExpectPowerGains(considered_candidates, {3});

  considered_candidates = GetConsideredCandidates(sorted_candidates, 0);
  ExpectPowerGains(considered_candidates, {});
}

TEST_F(OverlayCombinationCacheTest, GetConsideredCandidatesWithDuplicates) {
  auto sorted_candidates = MakeCandidates(2);

  auto considered_candidates = GetConsideredCandidates(sorted_candidates, 2);

  ExpectPowerGains(considered_candidates, {2, 1});
  // Both are using the on top strategy.
  EXPECT_EQ(considered_candidates[0].strategy, &on_top_strategy_);
  EXPECT_EQ(considered_candidates[1].strategy, &on_top_strategy_);

  // Insert a copy at the front of the first candidate with a different
  // strategy.
  OverlayProposedCandidate& front = sorted_candidates.front();
  OverlayProposedCandidate underlay_duplicate(front.quad_iter, front.candidate,
                                              &underlay_strategy_);
  underlay_duplicate.relative_power_gain = front.relative_power_gain;
  sorted_candidates.insert(sorted_candidates.begin(), underlay_duplicate);

  considered_candidates = GetConsideredCandidates(sorted_candidates, 3);

  // Still only 2 candidates considered, because two are for the same quad.
  ExpectPowerGains(considered_candidates, {2, 1});
  // The first one is used, this time the underlay candidate.
  EXPECT_EQ(considered_candidates[0].strategy, &underlay_strategy_);
  EXPECT_EQ(considered_candidates[1].strategy, &on_top_strategy_);
}

TEST_F(OverlayCombinationCacheTest, GetIds) {
  auto considered_candidates = MakeCandidates(2);

  std::vector<CandidateId> ids = GetIds(considered_candidates);
  ASSERT_THAT(ids, ElementsAre(0, 1));

  // Add 2 more candidates. The first two will be identical to the previous two.
  considered_candidates = MakeCandidates(4);
  ids = GetIds(considered_candidates);
  ASSERT_THAT(ids, ElementsAre(0, 1, 2, 3));

  // Reverse the list.
  std::reverse(considered_candidates.begin(), considered_candidates.end());
  ids = GetIds(considered_candidates);
  // Ids are cached, so the ids list is also reverse.
  ASSERT_THAT(ids, ElementsAre(3, 2, 1, 0));

  // Make 6 new candidates.
  considered_candidates = MakeCandidates(6);
  // Move the candidates 1 pixel so they're all different from the last set.
  for (auto& cand : considered_candidates) {
    cand.candidate.display_rect.set_y(1);
  }

  ids = GetIds(considered_candidates);
  // New ids will get used until they run out, then they'll be reused in order.
  ASSERT_THAT(ids, ElementsAre(4, 5, 6, 7, 0, 1));

  // Make 8 candidates.
  considered_candidates = MakeCandidates(8);
  ids = GetIds(considered_candidates);
  // Again, the unused ids will be assigned first, then they're reused in order.
  ASSERT_THAT(ids, ElementsAre(2, 3, 0, 1, 4, 5, 6, 7));
}

TEST_F(OverlayCombinationCacheTest, GetPowerSortedCombinations) {
  auto considered_candidates = MakeCandidatesWithPowerGains({8, 4, 2, 1});
  std::vector<CandidateId> ids = {3, 2, 1, 0};

  std::vector<std::pair<CandidateCombination, int>> power_sorted_combinations =
      GetPowerSortedCombinations(considered_candidates, ids);

  // Power gains and ids were setup such that so the total power gain equals the
  // decimal value of the bitset's binary representation.
  std::vector<std::pair<CandidateCombination, int>> expected{
      std::pair<CandidateCombination, int>("1111", 15),
      std::pair<CandidateCombination, int>("1110", 14),
      std::pair<CandidateCombination, int>("1101", 13),
      std::pair<CandidateCombination, int>("1100", 12),
      std::pair<CandidateCombination, int>("1011", 11),
      std::pair<CandidateCombination, int>("1010", 10),
      std::pair<CandidateCombination, int>("1001", 9),
      std::pair<CandidateCombination, int>("1000", 8),
      std::pair<CandidateCombination, int>("0111", 7),
      std::pair<CandidateCombination, int>("0110", 6),
      std::pair<CandidateCombination, int>("0101", 5),
      std::pair<CandidateCombination, int>("0100", 4),
      std::pair<CandidateCombination, int>("0011", 3),
      std::pair<CandidateCombination, int>("0010", 2),
      std::pair<CandidateCombination, int>("0001", 1),
  };
  EXPECT_THAT(power_sorted_combinations, ElementsAreArray(expected));

  // Use the same candidates but with different ids. Notably they're still in
  // descending order, but more spread out.
  std::vector<CandidateId> different_ids = {7, 5, 2, 0};
  power_sorted_combinations =
      GetPowerSortedCombinations(considered_candidates, different_ids);

  // Same pattern as before, but with bits more spread out.
  expected = {
      std::pair<CandidateCombination, int>("10100101", 15),
      std::pair<CandidateCombination, int>("10100100", 14),
      std::pair<CandidateCombination, int>("10100001", 13),
      std::pair<CandidateCombination, int>("10100000", 12),
      std::pair<CandidateCombination, int>("10000101", 11),
      std::pair<CandidateCombination, int>("10000100", 10),
      std::pair<CandidateCombination, int>("10000001", 9),
      std::pair<CandidateCombination, int>("10000000", 8),
      std::pair<CandidateCombination, int>("00100101", 7),
      std::pair<CandidateCombination, int>("00100100", 6),
      std::pair<CandidateCombination, int>("00100001", 5),
      std::pair<CandidateCombination, int>("00100000", 4),
      std::pair<CandidateCombination, int>("00000101", 3),
      std::pair<CandidateCombination, int>("00000100", 2),
      std::pair<CandidateCombination, int>("00000001", 1),
  };
  EXPECT_THAT(power_sorted_combinations, ElementsAreArray(expected));
}

TEST_F(OverlayCombinationCacheTest, IdMapper) {
  auto candidates = MakeCandidates(10);

  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[0]), 0u);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[1]), 1u);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[2]), 2u);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[3]), 3u);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[4]), 4u);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[5]), 5u);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[6]), 6u);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[7]), 7u);
  // There's a maximum of 8 ids.
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[8]),
            OverlayCombinationCache::kInvalidCandidateId);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[9]),
            OverlayCombinationCache::kInvalidCandidateId);
  // Id's are cached.
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[0]), 0u);

  EXPECT_EQ(id_mapper_.GetClaimedIds(), CandidateCombination("11111111"));

  id_mapper_.RemoveStaleIds(CandidateCombination("00000101"));

  EXPECT_EQ(id_mapper_.GetClaimedIds(), CandidateCombination("11111010"));

  // Now there's room these candidates.
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[8]), 0u);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[9]), 2u);
  // This candidate's id was stolen.
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[0]),
            OverlayCombinationCache::kInvalidCandidateId);

  EXPECT_EQ(id_mapper_.GetClaimedIds(), CandidateCombination("11111111"));

  id_mapper_.ClearIds();

  EXPECT_EQ(id_mapper_.GetClaimedIds(), CandidateCombination("00000000"));

  // Candidate order is scrambled, but ids are allocated in order
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[7]), 0u);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[4]), 1u);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[3]), 2u);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[0]), 3u);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[5]), 4u);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[1]), 5u);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[6]), 6u);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[9]), 7u);
  // There's a maximum of 8 ids.
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[2]),
            OverlayCombinationCache::kInvalidCandidateId);
  EXPECT_EQ(id_mapper_.GetCandidateId(candidates[8]),
            OverlayCombinationCache::kInvalidCandidateId);
}

}  // namespace viz