Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
image_lru_unittests.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/shell/platform/android/image_lru.h"
6#include "gtest/gtest.h"
7
8namespace flutter {
9namespace testing {
10
11TEST(ImageLRU, CanStoreSingleImage) {
12 auto image = DlImage::Make(nullptr);
13 ImageLRU image_lru;
14
15 EXPECT_EQ(image_lru.FindImage(1), nullptr);
16
17 image_lru.AddImage(image, 1);
18
19 EXPECT_EQ(image_lru.FindImage(1), image);
20}
21
22TEST(ImageLRU, EvictsLRU) {
23 auto image = DlImage::Make(nullptr);
24 ImageLRU image_lru;
25
26 // Fill up the cache, nothing is removed
27 for (auto i = 0u; i < kImageReaderSwapchainSize; i++) {
28 EXPECT_EQ(image_lru.AddImage(image, i + 1), 0u);
29 }
30 // Confirm each image is in the cache. This should keep the LRU
31 // order the same.
32 for (auto i = 0u; i < kImageReaderSwapchainSize; i++) {
33 EXPECT_EQ(image_lru.FindImage(i + 1), image);
34 }
35
36 // Insert new image and verify least recently used was removed.
37 EXPECT_EQ(image_lru.AddImage(image, 100), 1u);
38}
39
40TEST(ImageLRU, CanClear) {
41 auto image = DlImage::Make(nullptr);
42 ImageLRU image_lru;
43
44 // Fill up the cache, nothing is removed
45 for (auto i = 0u; i < kImageReaderSwapchainSize; i++) {
46 EXPECT_EQ(image_lru.AddImage(image, i + 1), 0u);
47 }
48 image_lru.Clear();
49
50 // Expect no cache entries.
51 for (auto i = 0u; i < kImageReaderSwapchainSize; i++) {
52 EXPECT_EQ(image_lru.FindImage(i + 1), nullptr);
53 }
54}
55
56} // namespace testing
57} // namespace flutter
#define TEST(S, s, D, expected)
static sk_sp< DlImage > Make(const SkImage *image)
Definition dl_image.cc:11
HardwareBufferKey AddImage(const sk_sp< flutter::DlImage > &image, HardwareBufferKey key)
Add a new image to the cache with a key, returning the key of the LRU entry that was removed.
Definition image_lru.cc:42
sk_sp< flutter::DlImage > FindImage(std::optional< HardwareBufferKey > key)
Retrieve the image associated with the given [key], or nullptr.
Definition image_lru.cc:9
void Clear()
Remove all entires from the image cache.
Definition image_lru.cc:60
sk_sp< SkImage > image
Definition examples.cpp:29
static constexpr size_t kImageReaderSwapchainSize
Definition image_lru.h:20