frame_eviction_manager_unittest.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2019 The Chromium 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. #include "components/viz/client/frame_eviction_manager.h"
  5. #include <algorithm>
  6. #include <vector>
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace viz {
  9. namespace {
  10. class TestFrameEvictionManagerClient : public FrameEvictionManagerClient {
  11. public:
  12. TestFrameEvictionManagerClient() = default;
  13. TestFrameEvictionManagerClient(const TestFrameEvictionManagerClient&) =
  14. delete;
  15. TestFrameEvictionManagerClient& operator=(
  16. const TestFrameEvictionManagerClient&) = delete;
  17. ~TestFrameEvictionManagerClient() override = default;
  18. // FrameEvictionManagerClient:
  19. void EvictCurrentFrame() override {
  20. FrameEvictionManager::GetInstance()->RemoveFrame(this);
  21. has_frame_ = false;
  22. }
  23. bool has_frame() const { return has_frame_; }
  24. private:
  25. bool has_frame_ = true;
  26. };
  27. } // namespace
  28. using FrameEvictionManagerTest = testing::Test;
  29. TEST_F(FrameEvictionManagerTest, ScopedPause) {
  30. constexpr int kMaxSavedFrames = 1;
  31. constexpr int kFrames = 2;
  32. FrameEvictionManager* manager = FrameEvictionManager::GetInstance();
  33. manager->set_max_number_of_saved_frames(kMaxSavedFrames);
  34. std::vector<TestFrameEvictionManagerClient> frames(kFrames);
  35. {
  36. FrameEvictionManager::ScopedPause scoped_pause;
  37. for (auto& frame : frames)
  38. manager->AddFrame(&frame, /*locked=*/false);
  39. // All frames stays because |scoped_pause| holds off frame eviction.
  40. EXPECT_EQ(kFrames,
  41. std::count_if(frames.begin(), frames.end(),
  42. [](const TestFrameEvictionManagerClient& frame) {
  43. return frame.has_frame();
  44. }));
  45. }
  46. // Frame eviction happens when |scoped_pause| goes out of scope.
  47. EXPECT_EQ(kMaxSavedFrames,
  48. std::count_if(frames.begin(), frames.end(),
  49. [](const TestFrameEvictionManagerClient& frame) {
  50. return frame.has_frame();
  51. }));
  52. }
  53. } // namespace viz