| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/output/context_cache_controller.h" | 5 #include "cc/output/context_cache_controller.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/test/test_mock_time_task_runner.h" | |
| 9 #include "cc/test/test_context_provider.h" | 8 #include "cc/test/test_context_provider.h" |
| 10 #include "cc/test/test_context_support.h" | 9 #include "cc/test/test_context_support.h" |
| 11 #include "cc/test/test_web_graphics_context_3d.h" | 10 #include "cc/test/test_web_graphics_context_3d.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 13 |
| 15 namespace cc { | 14 namespace cc { |
| 16 namespace { | 15 namespace { |
| 17 using ::testing::Mock; | 16 using ::testing::Mock; |
| 18 using ::testing::StrictMock; | 17 using ::testing::StrictMock; |
| 19 | 18 |
| 20 class MockContextSupport : public TestContextSupport { | 19 class MockContextSupport : public TestContextSupport { |
| 21 public: | 20 public: |
| 22 MockContextSupport() {} | 21 MockContextSupport() {} |
| 23 MOCK_METHOD1(SetAggressivelyFreeResources, | 22 MOCK_METHOD1(SetAggressivelyFreeResources, |
| 24 void(bool aggressively_free_resources)); | 23 void(bool aggressively_free_resources)); |
| 25 }; | 24 }; |
| 26 | 25 |
| 27 TEST(ContextCacheControllerTest, ScopedVisibilityBasic) { | 26 TEST(ContextCacheControllerTest, ScopedVisibilityBasic) { |
| 28 StrictMock<MockContextSupport> context_support; | 27 StrictMock<MockContextSupport> context_support; |
| 29 auto task_runner = make_scoped_refptr(new base::TestMockTimeTaskRunner); | 28 ContextCacheController cache_controller(&context_support, nullptr); |
| 30 ContextCacheController cache_controller(&context_support, task_runner); | |
| 31 | 29 |
| 32 EXPECT_CALL(context_support, SetAggressivelyFreeResources(false)); | 30 EXPECT_CALL(context_support, SetAggressivelyFreeResources(false)); |
| 33 std::unique_ptr<ContextCacheController::ScopedVisibility> visibility = | 31 std::unique_ptr<ContextCacheController::ScopedVisibility> visibility = |
| 34 cache_controller.ClientBecameVisible(); | 32 cache_controller.ClientBecameVisible(); |
| 35 Mock::VerifyAndClearExpectations(&context_support); | 33 Mock::VerifyAndClearExpectations(&context_support); |
| 36 | 34 |
| 37 EXPECT_CALL(context_support, SetAggressivelyFreeResources(true)); | 35 EXPECT_CALL(context_support, SetAggressivelyFreeResources(true)); |
| 38 cache_controller.ClientBecameNotVisible(std::move(visibility)); | 36 cache_controller.ClientBecameNotVisible(std::move(visibility)); |
| 39 } | 37 } |
| 40 | 38 |
| 41 TEST(ContextCacheControllerTest, ScopedVisibilityMulti) { | 39 TEST(ContextCacheControllerTest, ScopedVisibilityMulti) { |
| 42 StrictMock<MockContextSupport> context_support; | 40 StrictMock<MockContextSupport> context_support; |
| 43 auto task_runner = make_scoped_refptr(new base::TestMockTimeTaskRunner); | 41 ContextCacheController cache_controller(&context_support, nullptr); |
| 44 ContextCacheController cache_controller(&context_support, task_runner); | |
| 45 | 42 |
| 46 EXPECT_CALL(context_support, SetAggressivelyFreeResources(false)); | 43 EXPECT_CALL(context_support, SetAggressivelyFreeResources(false)); |
| 47 auto visibility_1 = cache_controller.ClientBecameVisible(); | 44 std::unique_ptr<ContextCacheController::ScopedVisibility> visibility_1 = |
| 45 cache_controller.ClientBecameVisible(); |
| 48 Mock::VerifyAndClearExpectations(&context_support); | 46 Mock::VerifyAndClearExpectations(&context_support); |
| 49 auto visibility_2 = cache_controller.ClientBecameVisible(); | 47 std::unique_ptr<ContextCacheController::ScopedVisibility> visibility_2 = |
| 48 cache_controller.ClientBecameVisible(); |
| 50 | 49 |
| 51 cache_controller.ClientBecameNotVisible(std::move(visibility_1)); | 50 cache_controller.ClientBecameNotVisible(std::move(visibility_1)); |
| 52 EXPECT_CALL(context_support, SetAggressivelyFreeResources(true)); | 51 EXPECT_CALL(context_support, SetAggressivelyFreeResources(true)); |
| 53 cache_controller.ClientBecameNotVisible(std::move(visibility_2)); | 52 cache_controller.ClientBecameNotVisible(std::move(visibility_2)); |
| 54 } | 53 } |
| 55 | 54 |
| 56 TEST(ContextCacheControllerTest, ScopedBusyWhileVisible) { | |
| 57 StrictMock<MockContextSupport> context_support; | |
| 58 auto task_runner = make_scoped_refptr(new base::TestMockTimeTaskRunner); | |
| 59 ContextCacheController cache_controller(&context_support, task_runner); | |
| 60 | |
| 61 EXPECT_CALL(context_support, SetAggressivelyFreeResources(false)); | |
| 62 auto visibility = cache_controller.ClientBecameVisible(); | |
| 63 Mock::VerifyAndClearExpectations(&context_support); | |
| 64 | |
| 65 // Now that we're visible, ensure that going idle triggers a delayed cleanup. | |
| 66 auto busy = cache_controller.ClientBecameBusy(); | |
| 67 cache_controller.ClientBecameNotBusy(std::move(busy)); | |
| 68 | |
| 69 EXPECT_CALL(context_support, SetAggressivelyFreeResources(true)); | |
| 70 EXPECT_CALL(context_support, SetAggressivelyFreeResources(false)); | |
| 71 task_runner->FastForwardBy(base::TimeDelta::FromSeconds(5)); | |
| 72 Mock::VerifyAndClearExpectations(&context_support); | |
| 73 | |
| 74 EXPECT_CALL(context_support, SetAggressivelyFreeResources(true)); | |
| 75 cache_controller.ClientBecameNotVisible(std::move(visibility)); | |
| 76 } | |
| 77 | |
| 78 TEST(ContextCacheControllerTest, ScopedBusyWhileNotVisible) { | |
| 79 StrictMock<MockContextSupport> context_support; | |
| 80 auto task_runner = make_scoped_refptr(new base::TestMockTimeTaskRunner); | |
| 81 ContextCacheController cache_controller(&context_support, task_runner); | |
| 82 | |
| 83 auto busy = cache_controller.ClientBecameBusy(); | |
| 84 | |
| 85 // We are not visible, so becoming busy should not trigger an idle callback. | |
| 86 cache_controller.ClientBecameNotBusy(std::move(busy)); | |
| 87 task_runner->FastForwardBy(base::TimeDelta::FromSeconds(5)); | |
| 88 } | |
| 89 | |
| 90 TEST(ContextCacheControllerTest, ScopedBusyMulitpleWhileVisible) { | |
| 91 StrictMock<MockContextSupport> context_support; | |
| 92 auto task_runner = make_scoped_refptr(new base::TestMockTimeTaskRunner); | |
| 93 ContextCacheController cache_controller(&context_support, task_runner); | |
| 94 | |
| 95 EXPECT_CALL(context_support, SetAggressivelyFreeResources(false)); | |
| 96 auto visible = cache_controller.ClientBecameVisible(); | |
| 97 Mock::VerifyAndClearExpectations(&context_support); | |
| 98 | |
| 99 auto busy_1 = cache_controller.ClientBecameBusy(); | |
| 100 cache_controller.ClientBecameNotBusy(std::move(busy_1)); | |
| 101 auto busy_2 = cache_controller.ClientBecameBusy(); | |
| 102 cache_controller.ClientBecameNotBusy(std::move(busy_2)); | |
| 103 | |
| 104 // When we fast forward, only one cleanup should happen. | |
| 105 EXPECT_CALL(context_support, SetAggressivelyFreeResources(true)); | |
| 106 EXPECT_CALL(context_support, SetAggressivelyFreeResources(false)); | |
| 107 task_runner->FastForwardBy(base::TimeDelta::FromSeconds(5)); | |
| 108 Mock::VerifyAndClearExpectations(&context_support); | |
| 109 | |
| 110 EXPECT_CALL(context_support, SetAggressivelyFreeResources(true)); | |
| 111 cache_controller.ClientBecameNotVisible(std::move(visible)); | |
| 112 } | |
| 113 | |
| 114 } // namespace | 55 } // namespace |
| 115 } // namespace cc | 56 } // namespace cc |
| OLD | NEW |