| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 |
| 5 #include "services/ui/gpu/gpu_service.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "gpu/config/gpu_info.h" |
| 13 #include "gpu/ipc/service/gpu_watchdog_thread.h" |
| 14 #include "services/ui/public/interfaces/gpu.mojom.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace ui { |
| 18 namespace test { |
| 19 |
| 20 class GpuServiceTest : public testing::Test { |
| 21 public: |
| 22 GpuServiceTest() |
| 23 : io_thread_("TestIOThread"), |
| 24 wait_(base::WaitableEvent::ResetPolicy::MANUAL, |
| 25 base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
| 26 ~GpuServiceTest() override {} |
| 27 |
| 28 GpuService* gpu_service() { return gpu_service_.get(); } |
| 29 |
| 30 void DestroyService() { gpu_service_ = nullptr; } |
| 31 |
| 32 void BlockIOThread() { |
| 33 wait_.Reset(); |
| 34 io_runner()->PostTask(FROM_HERE, base::Bind(&base::WaitableEvent::Wait, |
| 35 base::Unretained(&wait_))); |
| 36 } |
| 37 |
| 38 void UnblockIOThread() { |
| 39 DCHECK(!wait_.IsSignaled()); |
| 40 wait_.Signal(); |
| 41 } |
| 42 |
| 43 scoped_refptr<base::SingleThreadTaskRunner> io_runner() { |
| 44 return io_thread_.task_runner(); |
| 45 } |
| 46 |
| 47 // testing::Test |
| 48 void SetUp() override { |
| 49 ASSERT_TRUE(io_thread_.Start()); |
| 50 gpu_service_ = base::MakeUnique<GpuService>( |
| 51 gpu::GPUInfo(), nullptr /* watchdog_thread */, io_thread_.task_runner(), |
| 52 gpu::GpuFeatureInfo()); |
| 53 } |
| 54 |
| 55 void TearDown() override { |
| 56 DestroyService(); |
| 57 base::RunLoop runloop; |
| 58 runloop.RunUntilIdle(); |
| 59 io_thread_.Stop(); |
| 60 } |
| 61 |
| 62 private: |
| 63 base::MessageLoop message_loop_; |
| 64 base::Thread io_thread_; |
| 65 std::unique_ptr<GpuService> gpu_service_; |
| 66 base::WaitableEvent wait_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(GpuServiceTest); |
| 69 }; |
| 70 |
| 71 // Tests that GpuService can be destroyed before Bind() succeeds on the IO |
| 72 // thread. |
| 73 TEST_F(GpuServiceTest, ServiceDestroyedBeforeBind) { |
| 74 mojom::GpuServicePtr ptr; |
| 75 mojom::GpuServiceRequest request(&ptr); |
| 76 |
| 77 // Block the IO thread to make sure that the GpuService is destroyed before |
| 78 // the binding happens on the IO thread. |
| 79 BlockIOThread(); |
| 80 gpu_service()->Bind(std::move(request)); |
| 81 DestroyService(); |
| 82 UnblockIOThread(); |
| 83 } |
| 84 |
| 85 // Tests ghat GpuService can be destroyed after Bind() succeeds on the IO |
| 86 // thread. |
| 87 TEST_F(GpuServiceTest, ServiceDestroyedAfterBind) { |
| 88 mojom::GpuServicePtr ptr; |
| 89 mojom::GpuServiceRequest request(&ptr); |
| 90 gpu_service()->Bind(std::move(request)); |
| 91 base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL, |
| 92 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 93 io_runner()->PostTask(FROM_HERE, base::Bind(&base::WaitableEvent::Signal, |
| 94 base::Unretained(&wait))); |
| 95 wait.Wait(); |
| 96 DestroyService(); |
| 97 } |
| 98 |
| 99 } // namespace test |
| 100 } // namespace ui |
| OLD | NEW |