Avi Drissman | 3f7a9d8 | 2022-09-08 20:55:42 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Sadrul Habib Chowdhury | 6b9697f | 2017-12-08 04:13:43 | [diff] [blame] | 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 "cc/raster/staging_buffer_pool.h" |
| 6 | |
Sadrul Habib Chowdhury | 6b9697f | 2017-12-08 04:13:43 | [diff] [blame] | 7 | #include "base/run_loop.h" |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 8 | #include "base/task/single_thread_task_runner.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 9 | #include "base/test/task_environment.h" |
kylechar | dfe762f | 2018-02-26 17:00:01 | [diff] [blame] | 10 | #include "components/viz/test/test_context_provider.h" |
Sadrul Habib Chowdhury | 6b9697f | 2017-12-08 04:13:43 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace cc { |
| 14 | |
| 15 | TEST(StagingBufferPoolTest, ShutdownImmediatelyAfterCreation) { |
kylechar | dfe762f | 2018-02-26 17:00:01 | [diff] [blame] | 16 | auto context_provider = viz::TestContextProvider::CreateWorker(); |
Sadrul Habib Chowdhury | 6b9697f | 2017-12-08 04:13:43 | [diff] [blame] | 17 | bool use_partial_raster = false; |
| 18 | int max_staging_buffer_usage_in_bytes = 1024; |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 19 | auto task_runner = base::SingleThreadTaskRunner::GetCurrentDefault(); |
Sadrul Habib Chowdhury | 6b9697f | 2017-12-08 04:13:43 | [diff] [blame] | 20 | // Create a StagingBufferPool and immediately shut it down. |
| 21 | auto pool = std::make_unique<StagingBufferPool>( |
danakj | 2b8f93fa | 2018-04-27 19:31:29 | [diff] [blame] | 22 | task_runner.get(), context_provider.get(), use_partial_raster, |
| 23 | max_staging_buffer_usage_in_bytes); |
Sadrul Habib Chowdhury | 6b9697f | 2017-12-08 04:13:43 | [diff] [blame] | 24 | pool->Shutdown(); |
| 25 | // Flush the message loop. |
| 26 | auto flush_message_loop = [] { |
| 27 | base::RunLoop runloop; |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 28 | base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
| 29 | FROM_HERE, runloop.QuitClosure()); |
Sadrul Habib Chowdhury | 6b9697f | 2017-12-08 04:13:43 | [diff] [blame] | 30 | runloop.Run(); |
| 31 | }; |
| 32 | |
| 33 | // Constructing the pool does a post-task to add itself as an observer. So |
| 34 | // allow for that registration to complete first. |
| 35 | flush_message_loop(); |
| 36 | |
| 37 | // Now, destroy the pool, and trigger a notification from the |
Takashi Sakamoto | 370e46f | 2018-10-05 02:51:10 | [diff] [blame] | 38 | // MemoryPressureListener. |
Sadrul Habib Chowdhury | 6b9697f | 2017-12-08 04:13:43 | [diff] [blame] | 39 | pool = nullptr; |
Takashi Sakamoto | 370e46f | 2018-10-05 02:51:10 | [diff] [blame] | 40 | base::MemoryPressureListener::SimulatePressureNotification( |
| 41 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
Sadrul Habib Chowdhury | 6b9697f | 2017-12-08 04:13:43 | [diff] [blame] | 42 | // Allow the callbacks in the observers to run. |
| 43 | flush_message_loop(); |
| 44 | // No crash. |
| 45 | } |
| 46 | |
| 47 | } // namespace cc |