Chromium Code Reviews| Index: services/ui/ws/gpu_host_unittest.cc |
| diff --git a/services/ui/ws/gpu_host_unittest.cc b/services/ui/ws/gpu_host_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9cee2e012f9e491168ccc8837fb2a54e091543d5 |
| --- /dev/null |
| +++ b/services/ui/ws/gpu_host_unittest.cc |
| @@ -0,0 +1,114 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "services/ui/ws/gpu_host.h" |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "gpu/config/gpu_info.h" |
| +#include "gpu/ipc/service/gpu_watchdog_thread.h" |
| +#include "services/ui/gpu/gpu_service.h" |
| +#include "services/ui/public/interfaces/gpu.mojom.h" |
| +#include "services/ui/ws/gpu_client.h" |
| +#include "services/ui/ws/gpu_host_delegate.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace ui { |
| +namespace ws { |
| +namespace test { |
| +namespace { |
| + |
| +// No-opt implementation of GpuHostDelegate. |
| +class TestGpuHostDelegate : public GpuHostDelegate { |
| + public: |
| + TestGpuHostDelegate() {} |
| + ~TestGpuHostDelegate() override {} |
| + |
| + // GpuHostDelegate: |
| + void OnGpuServiceInitialized() override {} |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(TestGpuHostDelegate); |
| +}; |
| + |
| +// Test implementation of GpuService. For testing behaviour of calls made by |
| +// GpuClient |
| +class TestGpuService : public GpuService { |
| + public: |
| + TestGpuService(); |
| + ~TestGpuService() override {} |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(TestGpuService); |
| +}; |
| + |
| +TestGpuService::TestGpuService() |
| + : GpuService(gpu::GPUInfo(), |
| + gpu::GpuWatchdogThread::Create(), |
|
sadrul
2017/03/23 20:00:20
Just use nullptr here?
jonross
2017/03/23 20:09:50
Actually now yeah. At one point a part of the test
|
| + nullptr, |
| + base::ThreadTaskRunnerHandle::Get(), |
| + gpu::GpuFeatureInfo()) {} |
| + |
| +} // namespace |
| + |
| +class GpuHostTest : public testing::Test { |
| + public: |
| + GpuHostTest() {} |
| + ~GpuHostTest() override {} |
| + |
| + GpuHost* gpu_host() { return gpu_host_.get(); } |
| + |
| + base::WeakPtr<GpuClient> AddGpuClient(); |
| + void DestroyHost(); |
| + |
| + // testing::Test |
| + void SetUp() override; |
| + |
| + private: |
| + base::MessageLoop message_loop_; |
| + |
| + base::WeakPtr<GpuClient> client_ref_; |
| + |
| + TestGpuHostDelegate gpu_host_delegate_; |
| + TestGpuService gpu_service_; |
| + ui::mojom::GpuServicePtr gpu_service_ptr_; |
| + std::unique_ptr<GpuHost> gpu_host_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(GpuHostTest); |
| +}; |
| + |
| +base::WeakPtr<GpuClient> GpuHostTest::AddGpuClient() { |
| + mojom::GpuRequest request; |
| + GpuClient* client = gpu_host_->AddInternal(std::move(request)); |
| + return client->weak_factory_.GetWeakPtr(); |
| +} |
| + |
| +void GpuHostTest::DestroyHost() { |
| + gpu_host_.reset(); |
| +} |
| + |
| +void GpuHostTest::SetUp() { |
| + testing::Test::SetUp(); |
| + gpu_host_ = base::MakeUnique<GpuHost>(&gpu_host_delegate_); |
| + |
| + ui::mojom::GpuServiceRequest request(&gpu_service_ptr_); |
| + gpu_service_.Bind(std::move(request)); |
| + gpu_host_->gpu_service_ = std::move(gpu_service_ptr_); |
| +} |
| + |
| +// Tests to verify, that if a GpuHost is deleted before GpuClient receives a |
| +// callback, that GpuClient is torn down and does not attempt to use GpuInfo |
| +// after deletion. This should not crash on asan-builds. |
| +TEST_F(GpuHostTest, GpuClientDestructionOrder) { |
| + base::WeakPtr<GpuClient> client_ref = AddGpuClient(); |
| + EXPECT_NE(nullptr, client_ref); |
| + DestroyHost(); |
| + EXPECT_EQ(nullptr, client_ref); |
| +} |
| + |
| +} // namespace test |
| +} // namespace ws |
| +} // namespace ui |