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" |
8 #include "cc/test/test_context_provider.h" | 9 #include "cc/test/test_context_provider.h" |
9 #include "cc/test/test_context_support.h" | 10 #include "cc/test/test_context_support.h" |
10 #include "cc/test/test_web_graphics_context_3d.h" | 11 #include "cc/test/test_web_graphics_context_3d.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 namespace cc { | 15 namespace cc { |
15 namespace { | 16 namespace { |
16 using ::testing::Mock; | 17 using ::testing::Mock; |
17 using ::testing::StrictMock; | 18 using ::testing::StrictMock; |
18 | 19 |
19 class MockContextSupport : public TestContextSupport { | 20 class MockContextSupport : public TestContextSupport { |
20 public: | 21 public: |
21 MockContextSupport() {} | 22 MockContextSupport() {} |
22 MOCK_METHOD1(SetAggressivelyFreeResources, | 23 MOCK_METHOD1(SetAggressivelyFreeResources, |
23 void(bool aggressively_free_resources)); | 24 void(bool aggressively_free_resources)); |
24 }; | 25 }; |
25 | 26 |
26 TEST(ContextCacheControllerTest, ScopedVisibilityBasic) { | 27 TEST(ContextCacheControllerTest, ScopedVisibilityBasic) { |
27 StrictMock<MockContextSupport> context_support; | 28 StrictMock<MockContextSupport> context_support; |
28 ContextCacheController cache_controller(&context_support, nullptr); | 29 auto task_runner = make_scoped_refptr(new base::TestMockTimeTaskRunner); |
| 30 ContextCacheController cache_controller(&context_support, task_runner); |
29 | 31 |
30 EXPECT_CALL(context_support, SetAggressivelyFreeResources(false)); | 32 EXPECT_CALL(context_support, SetAggressivelyFreeResources(false)); |
31 std::unique_ptr<ContextCacheController::ScopedVisibility> visibility = | 33 std::unique_ptr<ContextCacheController::ScopedVisibility> visibility = |
32 cache_controller.ClientBecameVisible(); | 34 cache_controller.ClientBecameVisible(); |
33 Mock::VerifyAndClearExpectations(&context_support); | 35 Mock::VerifyAndClearExpectations(&context_support); |
34 | 36 |
35 EXPECT_CALL(context_support, SetAggressivelyFreeResources(true)); | 37 EXPECT_CALL(context_support, SetAggressivelyFreeResources(true)); |
36 cache_controller.ClientBecameNotVisible(std::move(visibility)); | 38 cache_controller.ClientBecameNotVisible(std::move(visibility)); |
37 } | 39 } |
38 | 40 |
39 TEST(ContextCacheControllerTest, ScopedVisibilityMulti) { | 41 TEST(ContextCacheControllerTest, ScopedVisibilityMulti) { |
40 StrictMock<MockContextSupport> context_support; | 42 StrictMock<MockContextSupport> context_support; |
41 ContextCacheController cache_controller(&context_support, nullptr); | 43 auto task_runner = make_scoped_refptr(new base::TestMockTimeTaskRunner); |
| 44 ContextCacheController cache_controller(&context_support, task_runner); |
42 | 45 |
43 EXPECT_CALL(context_support, SetAggressivelyFreeResources(false)); | 46 EXPECT_CALL(context_support, SetAggressivelyFreeResources(false)); |
44 std::unique_ptr<ContextCacheController::ScopedVisibility> visibility_1 = | 47 auto visibility_1 = cache_controller.ClientBecameVisible(); |
45 cache_controller.ClientBecameVisible(); | |
46 Mock::VerifyAndClearExpectations(&context_support); | 48 Mock::VerifyAndClearExpectations(&context_support); |
47 std::unique_ptr<ContextCacheController::ScopedVisibility> visibility_2 = | 49 auto visibility_2 = cache_controller.ClientBecameVisible(); |
48 cache_controller.ClientBecameVisible(); | |
49 | 50 |
50 cache_controller.ClientBecameNotVisible(std::move(visibility_1)); | 51 cache_controller.ClientBecameNotVisible(std::move(visibility_1)); |
51 EXPECT_CALL(context_support, SetAggressivelyFreeResources(true)); | 52 EXPECT_CALL(context_support, SetAggressivelyFreeResources(true)); |
52 cache_controller.ClientBecameNotVisible(std::move(visibility_2)); | 53 cache_controller.ClientBecameNotVisible(std::move(visibility_2)); |
53 } | 54 } |
54 | 55 |
| 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 |
55 } // namespace | 114 } // namespace |
56 } // namespace cc | 115 } // namespace cc |
OLD | NEW |