File: surface_references_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (548 lines) | stat: -rw-r--r-- 22,125 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
542
543
544
545
546
547
548
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>

#include <unordered_map>
#include <vector>

#include "base/containers/flat_set.h"
#include "base/test/test_mock_time_task_runner.h"
#include "components/viz/common/surfaces/surface_id.h"
#include "components/viz/service/frame_sinks/compositor_frame_sink_support.h"
#include "components/viz/service/frame_sinks/frame_sink_manager_impl.h"
#include "components/viz/service/surfaces/surface.h"
#include "components/viz/service/surfaces/surface_manager.h"
#include "components/viz/test/compositor_frame_helpers.h"
#include "components/viz/test/surface_id_allocator_set.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::ElementsAre;
using testing::IsEmpty;
using testing::SizeIs;
using testing::UnorderedElementsAre;

namespace viz {
namespace {

constexpr FrameSinkId kFrameSink1(1, 0);
constexpr FrameSinkId kFrameSink2(2, 0);
constexpr FrameSinkId kFrameSink3(3, 0);

}  // namespace

// Tests for reference tracking in CompositorFrameSinkSupport and
// SurfaceManager.
class SurfaceReferencesTest : public testing::Test {
 public:
  SurfaceReferencesTest()
      : task_runner_(base::MakeRefCounted<base::TestMockTimeTaskRunner>()),
        scoped_context_(task_runner_.get()) {}

  SurfaceManager& GetSurfaceManager() { return *manager_->surface_manager(); }

  // Creates a new Surface with the provided |frame_sink_id| and |local_id|.
  // Will first create a Surfacesupport for |frame_sink_id| if necessary.
  SurfaceId CreateSurface(const FrameSinkId& frame_sink_id,
                          uint32_t parent_id) {
    SurfaceId surface_id =
        allocator_set_.MakeSurfaceId(frame_sink_id, parent_id);
    GetCompositorFrameSinkSupport(frame_sink_id)
        .SubmitCompositorFrame(surface_id.local_surface_id(),
                               MakeDefaultCompositorFrame());
    return surface_id;
  }

  // Destroy Surface with |surface_id|.
  void DestroySurface(const SurfaceId& surface_id) {
    GetCompositorFrameSinkSupport(surface_id.frame_sink_id())
        .EvictSurface(surface_id.local_surface_id());
  }

  CompositorFrameSinkSupport& GetCompositorFrameSinkSupport(
      const FrameSinkId& frame_sink_id) {
    auto& support_ptr = supports_[frame_sink_id];
    if (!support_ptr) {
      manager_->RegisterFrameSinkId(frame_sink_id,
                                    true /* report_activation */);
      constexpr bool is_root = false;
      support_ptr = std::make_unique<CompositorFrameSinkSupport>(
          nullptr, manager_.get(), frame_sink_id, is_root);
    }
    return *support_ptr;
  }

  void DestroyCompositorFrameSinkSupport(const FrameSinkId& frame_sink_id) {
    auto support_ptr = supports_.find(frame_sink_id);
    ASSERT_NE(support_ptr, supports_.end());
    supports_.erase(support_ptr);
    manager_->InvalidateFrameSinkId(frame_sink_id);
  }

  void RemoveSurfaceReference(const SurfaceId& parent_id,
                              const SurfaceId& child_id) {
    manager_->surface_manager()->RemoveSurfaceReferences(
        {SurfaceReference(parent_id, child_id)});
  }

  void AddSurfaceReference(const SurfaceId& parent_id,
                           const SurfaceId& child_id) {
    manager_->surface_manager()->AddSurfaceReferences(
        {SurfaceReference(parent_id, child_id)});
  }

  // Returns all the references where |surface_id| is the parent.
  const base::flat_set<SurfaceId>& GetReferencesFrom(
      const SurfaceId& surface_id) {
    return GetSurfaceManager().GetSurfacesReferencedByParent(surface_id);
  }

  // Returns all the references where |surface_id| is the child.
  const base::flat_set<SurfaceId> GetReferencesFor(
      const SurfaceId& surface_id) {
    return GetSurfaceManager().GetSurfacesThatReferenceChildForTesting(
        surface_id);
  }

  // Temporary references are stored as a map in SurfaceManager. This method
  // converts the map to a vector.
  std::vector<SurfaceId> GetAllTempReferences() {
    std::vector<SurfaceId> temp_references;
    for (auto& map_entry : GetSurfaceManager().temporary_references_)
      temp_references.push_back(map_entry.first);
    return temp_references;
  }

  bool IsTemporaryReferenceTimerRunning() const {
    return manager_->surface_manager()->expire_timer_->IsRunning();
  }

 protected:
  // testing::Test:
  void SetUp() override {
    // Start each test with a fresh SurfaceManager instance.
    manager_ = std::make_unique<FrameSinkManagerImpl>(
        FrameSinkManagerImpl::InitParams());
  }
  void TearDown() override {
    supports_.clear();
    manager_.reset();
  }

  // Use a TestMockTimeTaskRunner so we can test timer behaviour in |manager_|.
  scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
  base::TestMockTimeTaskRunner::ScopedContext scoped_context_;

  std::unique_ptr<FrameSinkManagerImpl> manager_;
  std::unordered_map<FrameSinkId,
                     std::unique_ptr<CompositorFrameSinkSupport>,
                     FrameSinkIdHash>
      supports_;
  SurfaceIdAllocatorSet allocator_set_;
};

TEST_F(SurfaceReferencesTest, AddReference) {
  SurfaceId id1 = CreateSurface(kFrameSink1, 1);
  AddSurfaceReference(GetSurfaceManager().GetRootSurfaceId(), id1);

  EXPECT_THAT(GetReferencesFor(id1),
              UnorderedElementsAre(GetSurfaceManager().GetRootSurfaceId()));
  EXPECT_THAT(GetReferencesFrom(id1), IsEmpty());
}

TEST_F(SurfaceReferencesTest, AddRemoveReference) {
  SurfaceId id1 = CreateSurface(kFrameSink1, 1);
  AddSurfaceReference(GetSurfaceManager().GetRootSurfaceId(), id1);

  SurfaceId id2 = CreateSurface(kFrameSink2, 1);
  AddSurfaceReference(id1, id2);

  EXPECT_THAT(GetReferencesFor(id1),
              UnorderedElementsAre(GetSurfaceManager().GetRootSurfaceId()));
  EXPECT_THAT(GetReferencesFor(id2), UnorderedElementsAre(id1));
  EXPECT_THAT(GetReferencesFrom(id1), UnorderedElementsAre(id2));
  EXPECT_THAT(GetReferencesFrom(id2), IsEmpty());

  RemoveSurfaceReference(id1, id2);
  EXPECT_THAT(GetReferencesFor(id1), SizeIs(1));
  EXPECT_THAT(GetReferencesFor(id2), IsEmpty());
  EXPECT_THAT(GetReferencesFrom(id1), IsEmpty());
  EXPECT_THAT(GetReferencesFrom(id2), IsEmpty());
}

TEST_F(SurfaceReferencesTest, NewSurfaceFromFrameSink) {
  SurfaceId id1 = CreateSurface(kFrameSink1, 1);
  SurfaceId id2 = CreateSurface(kFrameSink2, 1);
  SurfaceId id3 = CreateSurface(kFrameSink3, 1);

  AddSurfaceReference(GetSurfaceManager().GetRootSurfaceId(), id1);
  AddSurfaceReference(id1, id2);
  AddSurfaceReference(id2, id3);

  // |kFramesink2| received a CompositorFrame with a new size, so it destroys
  // |id2| and creates |id2_next|. No reference have been removed yet.
  SurfaceId id2_next = CreateSurface(kFrameSink2, 2);
  EXPECT_NE(nullptr, GetSurfaceManager().GetSurfaceForId(id2));
  EXPECT_NE(nullptr, GetSurfaceManager().GetSurfaceForId(id2_next));

  // Add references to and from |id2_next|.
  AddSurfaceReference(id1, id2_next);
  AddSurfaceReference(id2_next, id3);
  EXPECT_THAT(GetReferencesFor(id2), UnorderedElementsAre(id1));
  EXPECT_THAT(GetReferencesFor(id2_next), UnorderedElementsAre(id1));
  EXPECT_THAT(GetReferencesFor(id3), UnorderedElementsAre(id2, id2_next));

  RemoveSurfaceReference(id1, id2);
  GetSurfaceManager().GarbageCollectSurfaces();
  EXPECT_THAT(GetReferencesFor(id2), IsEmpty());
  EXPECT_THAT(GetReferencesFor(id2_next), UnorderedElementsAre(id1));
  EXPECT_THAT(GetReferencesFor(id3), UnorderedElementsAre(id2_next));

  // |id2| should be deleted during GC but other surfaces shouldn't.
  EXPECT_EQ(nullptr, GetSurfaceManager().GetSurfaceForId(id2));
  EXPECT_NE(nullptr, GetSurfaceManager().GetSurfaceForId(id2_next));
  EXPECT_NE(nullptr, GetSurfaceManager().GetSurfaceForId(id3));
}

TEST_F(SurfaceReferencesTest, ReferenceCycleGetsDeleted) {
  SurfaceId id1 = CreateSurface(kFrameSink1, 1);
  SurfaceId id2 = CreateSurface(kFrameSink2, 1);
  SurfaceId id3 = CreateSurface(kFrameSink3, 1);

  AddSurfaceReference(GetSurfaceManager().GetRootSurfaceId(), id1);
  AddSurfaceReference(id1, id2);
  AddSurfaceReference(id2, id3);

  // This reference forms a cycle between id2 and id3.
  AddSurfaceReference(id3, id2);

  DestroySurface(id3);
  DestroySurface(id2);
  DestroySurface(id1);

  RemoveSurfaceReference(GetSurfaceManager().GetRootSurfaceId(), id1);

  GetSurfaceManager().GarbageCollectSurfaces();

  // Removing the reference from the root to id1 should allow all three surfaces
  // to be deleted during GC even with a cycle between 2 and 3.
  EXPECT_EQ(nullptr, GetSurfaceManager().GetSurfaceForId(id1));
  EXPECT_EQ(nullptr, GetSurfaceManager().GetSurfaceForId(id2));
  EXPECT_EQ(nullptr, GetSurfaceManager().GetSurfaceForId(id3));
}

TEST_F(SurfaceReferencesTest, SurfacesAreDeletedDuringGarbageCollection) {
  SurfaceId id1 = CreateSurface(kFrameSink1, 1);
  SurfaceId id2 = CreateSurface(kFrameSink2, 1);

  AddSurfaceReference(GetSurfaceManager().GetRootSurfaceId(), id1);
  AddSurfaceReference(id1, id2);

  EXPECT_NE(nullptr, GetSurfaceManager().GetSurfaceForId(id1));
  EXPECT_NE(nullptr, GetSurfaceManager().GetSurfaceForId(id2));

  // Destroying the surfaces shouldn't delete them yet, since there is still an
  // active reference on all surfaces.
  DestroySurface(id1);
  DestroySurface(id2);
  GetSurfaceManager().GarbageCollectSurfaces();

  EXPECT_NE(nullptr, GetSurfaceManager().GetSurfaceForId(id1));
  EXPECT_NE(nullptr, GetSurfaceManager().GetSurfaceForId(id2));

  // Should delete |id2| when the only reference to it is removed.
  RemoveSurfaceReference(id1, id2);
  GetSurfaceManager().GarbageCollectSurfaces();
  EXPECT_EQ(nullptr, GetSurfaceManager().GetSurfaceForId(id2));

  // Should delete |id1| when the only reference to it is removed.
  RemoveSurfaceReference(GetSurfaceManager().GetRootSurfaceId(), id1);
  GetSurfaceManager().GarbageCollectSurfaces();
  EXPECT_EQ(nullptr, GetSurfaceManager().GetSurfaceForId(id1));
}

TEST_F(SurfaceReferencesTest, GarbageCollectionWorksRecursively) {
  SurfaceId id1 = CreateSurface(kFrameSink1, 1);
  SurfaceId id2 = CreateSurface(kFrameSink2, 1);
  SurfaceId id3 = CreateSurface(kFrameSink3, 1);

  AddSurfaceReference(GetSurfaceManager().GetRootSurfaceId(), id1);
  AddSurfaceReference(id1, id2);
  AddSurfaceReference(id2, id3);

  DestroySurface(id3);
  DestroySurface(id2);
  DestroySurface(id1);

  GetSurfaceManager().GarbageCollectSurfaces();

  // Destroying the surfaces shouldn't delete them yet, since there is still an
  // active reference on all surfaces.
  EXPECT_NE(nullptr, GetSurfaceManager().GetSurfaceForId(id3));
  EXPECT_NE(nullptr, GetSurfaceManager().GetSurfaceForId(id2));
  EXPECT_NE(nullptr, GetSurfaceManager().GetSurfaceForId(id1));

  RemoveSurfaceReference(GetSurfaceManager().GetRootSurfaceId(), id1);

  GetSurfaceManager().GarbageCollectSurfaces();

  // Removing the reference from the root to id1 should allow all three surfaces
  // to be deleted during GC.
  EXPECT_EQ(nullptr, GetSurfaceManager().GetSurfaceForId(id1));
  EXPECT_EQ(nullptr, GetSurfaceManager().GetSurfaceForId(id2));
  EXPECT_EQ(nullptr, GetSurfaceManager().GetSurfaceForId(id3));
}

// Verify that surfaces marked as live are not garbage collected and any
// dependencies are also not garbage collected.
TEST_F(SurfaceReferencesTest, LiveSurfaceStillReachable) {
  SurfaceId id1 = CreateSurface(kFrameSink1, 1);
  SurfaceId id2 = CreateSurface(kFrameSink2, 1);
  SurfaceId id3 = CreateSurface(kFrameSink3, 1);

  AddSurfaceReference(GetSurfaceManager().GetRootSurfaceId(), id1);
  AddSurfaceReference(id1, id2);
  AddSurfaceReference(id2, id3);
  ASSERT_THAT(GetAllTempReferences(), IsEmpty());

  // Marking |id3| for destruction shouldn't cause it be garbage collected
  // because |id2| is still reachable from the root.
  DestroySurface(id3);
  GetSurfaceManager().GarbageCollectSurfaces();
  EXPECT_NE(nullptr, GetSurfaceManager().GetSurfaceForId(id3));

  // Removing the surface reference to |id2| makes it not reachable from the
  // root, however it's not marked as destroyed and is still live. Make sure we
  // also don't delete any dependencies, such as |id3|, as well.
  RemoveSurfaceReference(id1, id2);
  GetSurfaceManager().GarbageCollectSurfaces();
  EXPECT_NE(nullptr, GetSurfaceManager().GetSurfaceForId(id3));
  EXPECT_NE(nullptr, GetSurfaceManager().GetSurfaceForId(id2));

  // |id2| is unreachable and destroyed. Garbage collection should delete both
  // |id2| and |id3| now.
  DestroySurface(id2);
  GetSurfaceManager().GarbageCollectSurfaces();
  EXPECT_EQ(nullptr, GetSurfaceManager().GetSurfaceForId(id3));
  EXPECT_EQ(nullptr, GetSurfaceManager().GetSurfaceForId(id2));
}

TEST_F(SurfaceReferencesTest, TryAddReferenceSameReferenceTwice) {
  SurfaceId id1 = CreateSurface(kFrameSink1, 1);
  SurfaceId id2 = CreateSurface(kFrameSink2, 1);

  AddSurfaceReference(GetSurfaceManager().GetRootSurfaceId(), id1);
  AddSurfaceReference(id1, id2);
  EXPECT_THAT(GetReferencesFor(id2), SizeIs(1));
  EXPECT_THAT(GetReferencesFrom(id1), SizeIs(1));

  // The second request should be ignored without crashing.
  AddSurfaceReference(id1, id2);
  EXPECT_THAT(GetReferencesFor(id2), SizeIs(1));
  EXPECT_THAT(GetReferencesFrom(id1), SizeIs(1));
}

TEST_F(SurfaceReferencesTest, AddingSelfReferenceFails) {
  SurfaceId id1 = CreateSurface(kFrameSink2, 1);

  // A temporary reference must exist to |id1|.
  EXPECT_THAT(GetAllTempReferences(), ElementsAre(id1));
  EXPECT_THAT(GetReferencesFrom(id1), IsEmpty());
  EXPECT_THAT(GetReferencesFor(id1), IsEmpty());

  // Try to add a self reference. This should fail.
  AddSurfaceReference(id1, id1);

  // Adding a self reference should be ignored without crashing. The temporary
  // reference to |id1| must still exist.
  EXPECT_THAT(GetAllTempReferences(), ElementsAre(id1));
  EXPECT_THAT(GetReferencesFrom(id1), IsEmpty());
  EXPECT_THAT(GetReferencesFor(id1), IsEmpty());
}

TEST_F(SurfaceReferencesTest, RemovingNonexistantReferenceFails) {
  SurfaceId id1 = CreateSurface(kFrameSink1, 1);
  SurfaceId id2 = CreateSurface(kFrameSink2, 1);

  // Removing non-existent reference should be ignored.
  AddSurfaceReference(id1, id2);
  RemoveSurfaceReference(id2, id1);
  EXPECT_THAT(GetReferencesFrom(id1), SizeIs(1));
  EXPECT_THAT(GetReferencesFor(id2), SizeIs(1));
}

TEST_F(SurfaceReferencesTest, AddSurfaceThenReference) {
  // Create a new surface.
  const SurfaceId surface_id = CreateSurface(kFrameSink2, 1);

  // A temporary reference must be added to |surface_id|.
  EXPECT_THAT(GetAllTempReferences(), ElementsAre(surface_id));

  // Create |parent_id| and add a real reference from it to |surface_id|.
  const SurfaceId parent_id = CreateSurface(kFrameSink1, 1);
  AddSurfaceReference(parent_id, surface_id);

  // The temporary reference to |surface_id| should be gone.
  // The only temporary reference should be to |parent_id|.
  // There must be a real reference from |parent_id| to |child_id|.
  EXPECT_THAT(GetReferencesFrom(parent_id), ElementsAre(surface_id));
  EXPECT_THAT(GetAllTempReferences(), ElementsAre(parent_id));
}

TEST_F(SurfaceReferencesTest, AddSurfaceThenRootReference) {
  // Create a new surface.
  const SurfaceId surface_id = CreateSurface(kFrameSink1, 1);

  // Temporary reference should be added to |surface_id|.
  EXPECT_THAT(GetAllTempReferences(), ElementsAre(surface_id));

  // Add a surface reference from root to |surface_id|.
  AddSurfaceReference(GetSurfaceManager().GetRootSurfaceId(), surface_id);

  // The temporary reference should be gone and there should now be a surface
  // reference from root to |surface_id|.
  EXPECT_THAT(GetAllTempReferences(), IsEmpty());
  EXPECT_THAT(GetReferencesFrom(GetSurfaceManager().GetRootSurfaceId()),
              ElementsAre(surface_id));
}

TEST_F(SurfaceReferencesTest, AddTwoSurfacesThenOneReference) {
  // Create two surfaces with different FrameSinkIds.
  const SurfaceId surface_id1 = CreateSurface(kFrameSink2, 1);
  const SurfaceId surface_id2 = CreateSurface(kFrameSink3, 1);

  // Temporary reference should be added for both surfaces.
  EXPECT_THAT(GetAllTempReferences(),
              UnorderedElementsAre(surface_id1, surface_id2));

  // Create |parent_id| and add a real reference from it to |surface_id1|.
  const SurfaceId parent_id = CreateSurface(kFrameSink1, 1);
  AddSurfaceReference(parent_id, surface_id1);

  // Real reference must be added to |surface_id1| and the temporary reference
  // to it must be gone.
  // There should still be a temporary reference left to |surface_id2|.
  // A temporary reference to |parent_id| must be created.
  EXPECT_THAT(GetAllTempReferences(),
              UnorderedElementsAre(parent_id, surface_id2));
  EXPECT_THAT(GetReferencesFrom(parent_id), ElementsAre(surface_id1));
}

TEST_F(SurfaceReferencesTest, AddSurfacesSkipReference) {
  // Add two surfaces that have the same FrameSinkId. This would happen
  // when a client submits two CompositorFrames before parent submits a new
  // CompositorFrame.
  const SurfaceId surface_id1 = CreateSurface(kFrameSink2, 1);
  const SurfaceId surface_id2 = CreateSurface(kFrameSink2, 2);

  // Temporary references should be added for both surfaces and they should be
  // stored in the order of creation.
  EXPECT_THAT(GetAllTempReferences(),
              UnorderedElementsAre(surface_id1, surface_id2));

  // Create |parent_id| and add a reference from it to |surface_id2| which was
  // created later.
  const SurfaceId parent_id = CreateSurface(kFrameSink1, 1);
  AddSurfaceReference(parent_id, surface_id2);

  // The real reference should be added for |surface_id2| and the temporary
  // references to both |surface_id1| and |surface_id2| should be gone.
  // There should be a temporary reference to |parent_id|.
  EXPECT_THAT(GetAllTempReferences(), ElementsAre(parent_id));
  EXPECT_THAT(GetReferencesFrom(parent_id), ElementsAre(surface_id2));
}

TEST_F(SurfaceReferencesTest, RemoveFirstTempReferenceOnly) {
  // Add two surfaces that have the same FrameSinkId. This would happen
  // when a client submits two CFs before parent submits a new CF.
  const SurfaceId surface_id1 = CreateSurface(kFrameSink2, 1);
  const SurfaceId surface_id2 = CreateSurface(kFrameSink2, 2);

  // Temporary references should be added for both surfaces and they should be
  // stored in the order of creation.
  EXPECT_THAT(GetAllTempReferences(),
              UnorderedElementsAre(surface_id1, surface_id2));

  // Create |parent_id| and add a reference from it to |surface_id1| which was
  // created earlier.
  const SurfaceId parent_id = CreateSurface(kFrameSink1, 1);
  AddSurfaceReference(parent_id, surface_id1);

  // The real reference should be added for |surface_id1| and its temporary
  // reference should be removed. The temporary reference for |surface_id2|
  // should remain. A temporary reference must be added for |parent_id|.
  EXPECT_THAT(GetAllTempReferences(),
              UnorderedElementsAre(parent_id, surface_id2));
  EXPECT_THAT(GetReferencesFrom(parent_id), ElementsAre(surface_id1));
}

TEST_F(SurfaceReferencesTest, SurfaceWithTemporaryReferenceIsDeleted) {
  const SurfaceId id1 = CreateSurface(kFrameSink1, 1);
  AddSurfaceReference(GetSurfaceManager().GetRootSurfaceId(), id1);

  // We create |id2| and never add a real reference to it. This leaves the
  // temporary reference.
  const SurfaceId id2 = CreateSurface(kFrameSink2, 1);
  ASSERT_THAT(GetAllTempReferences(), UnorderedElementsAre(id2));
  EXPECT_NE(nullptr, GetSurfaceManager().GetSurfaceForId(id2));

  // Destroy both surfaces so they can be garbage collected. We remove the
  // surface reference to |id1| which will run GarbageCollectSurfaces().
  DestroySurface(id1);
  DestroySurface(id2);
  RemoveSurfaceReference(GetSurfaceManager().GetRootSurfaceId(), id1);

  GetSurfaceManager().GarbageCollectSurfaces();

  // |id1| is destroyed and has no references, so it's deleted.
  EXPECT_EQ(nullptr, GetSurfaceManager().GetSurfaceForId(id1));

  // |id2| is destroyed and the temporary reference is dropped, so it's deleted.
  EXPECT_EQ(nullptr, GetSurfaceManager().GetSurfaceForId(id2));
}

// Checks that adding a surface reference clears the temporary reference.
TEST_F(SurfaceReferencesTest, InvalidateHasNoEffectOnSurfaceReferences) {
  const SurfaceId parent_id = CreateSurface(kFrameSink1, 1);
  AddSurfaceReference(GetSurfaceManager().GetRootSurfaceId(), parent_id);

  const SurfaceId id1 = CreateSurface(kFrameSink2, 1);
  ASSERT_THAT(GetAllTempReferences(), UnorderedElementsAre(id1));

  // Adding a real surface reference will remove the temporary reference.
  AddSurfaceReference(parent_id, id1);
  ASSERT_THAT(GetAllTempReferences(), IsEmpty());
  ASSERT_THAT(GetReferencesFor(id1), UnorderedElementsAre(parent_id));
}

// Check that old temporary references are deleted, but only for surfaces marked
// as destroyed.
TEST_F(SurfaceReferencesTest, MarkOldTemporaryReferences) {
  constexpr base::TimeDelta kFastForwardTime = base::Seconds(30);

  // There are no temporary references so the timer should be stopped.
  EXPECT_FALSE(IsTemporaryReferenceTimerRunning());

  // Creating the surface should create a temporary reference and start timer.
  const SurfaceId id = CreateSurface(kFrameSink2, 1);
  ASSERT_THAT(GetAllTempReferences(), UnorderedElementsAre(id));
  EXPECT_TRUE(IsTemporaryReferenceTimerRunning());

  // The temporary reference should not be marked as old and then deleted
  // because the surface is still alive.
  task_runner_->FastForwardBy(kFastForwardTime);
  ASSERT_THAT(GetAllTempReferences(), UnorderedElementsAre(id));
  EXPECT_TRUE(IsTemporaryReferenceTimerRunning());

  // After the surface is marked as destroyed, the temporary reference should
  // be marked as old then deleted. The timer should stop because there are no
  // temporary references.
  DestroySurface(id);
  task_runner_->FastForwardBy(kFastForwardTime);
  ASSERT_THAT(GetAllTempReferences(), IsEmpty());
  EXPECT_FALSE(IsTemporaryReferenceTimerRunning());
}

}  // namespace viz