Update UnusedResources lint suppressions.
[chromium-blink-merge.git] / ui / gl / gpu_timing_unittest.cc
blob9044394d697a15b31d4ceea35e6a4a7fc33cb0d3
1 // Copyright 2015 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.
5 #include "base/bind.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/gl/gl_context_stub_with_extensions.h"
9 #include "ui/gl/gl_implementation.h"
10 #include "ui/gl/gl_mock.h"
11 #include "ui/gl/gl_surface.h"
12 #include "ui/gl/gpu_preference.h"
13 #include "ui/gl/gpu_timing.h"
15 namespace gfx {
17 class GPUTimingTest : public testing::Test {
18 public:
19 void SetUp() override {
20 setup_ = false;
21 fake_cpu_time_ = 0;
23 CreateGPUTimingClient()->SetCpuTimeForTesting(base::Bind(&GetFakeCPUTime));
26 void TearDown() override {
27 context_ = nullptr;
30 void SetupGLContext(const char* gl_version, const char* gl_extensions) {
31 ASSERT_FALSE(setup_) << "Cannot setup GL context twice.";
32 gfx::SetGLGetProcAddressProc(gfx::MockGLInterface::GetGLProcAddress);
33 gfx::GLSurface::InitializeOneOffWithMockBindingsForTests();
34 gl_.reset(new ::testing::StrictMock< ::gfx::MockGLInterface>());
35 ::gfx::MockGLInterface::SetGLInterface(gl_.get());
37 context_ = new gfx::GLContextStubWithExtensions;
38 context_->AddExtensionsString(gl_extensions);
39 context_->SetGLVersionString(gl_version);
41 setup_ = true;
44 scoped_refptr<GPUTimingClient> CreateGPUTimingClient() {
45 if (!setup_) {
46 SetupGLContext("2.0", "");
48 return context_->CreateGPUTimingClient();
51 void SetFakeCPUTime(int64_t fake_cpu_time) {
52 fake_cpu_time_ = fake_cpu_time;
55 protected:
56 static int64_t GetFakeCPUTime() {
57 return fake_cpu_time_;
60 private:
61 static int64_t fake_cpu_time_;
63 bool setup_ = false;
64 scoped_ptr< ::testing::StrictMock< ::gfx::MockGLInterface> > gl_;
65 scoped_refptr<gfx::GLContextStubWithExtensions> context_;
68 int64_t GPUTimingTest::fake_cpu_time_ = 0;
70 TEST_F(GPUTimingTest, FakeTimerTest) {
71 // Tests that we can properly set fake cpu times.
72 SetFakeCPUTime(123);
74 scoped_refptr<GPUTimingClient> gpu_timing_client = CreateGPUTimingClient();
75 EXPECT_EQ(123, gpu_timing_client->GetCurrentCPUTime());
77 base::Callback<int64_t(void)> empty;
78 gpu_timing_client->SetCpuTimeForTesting(empty);
79 EXPECT_NE(123, gpu_timing_client->GetCurrentCPUTime());
82 TEST_F(GPUTimingTest, ForceTimeElapsedQuery) {
83 // Test that forcing time elapsed query affects all clients.
84 scoped_refptr<GPUTimingClient> client1 = CreateGPUTimingClient();
85 EXPECT_FALSE(client1->IsForceTimeElapsedQuery());
87 scoped_refptr<GPUTimingClient> client_force = CreateGPUTimingClient();
88 EXPECT_FALSE(client1->IsForceTimeElapsedQuery());
89 client_force->ForceTimeElapsedQuery();
90 EXPECT_TRUE(client1->IsForceTimeElapsedQuery());
92 EXPECT_TRUE(client1->IsForceTimeElapsedQuery());
94 scoped_refptr<GPUTimingClient> client2 = CreateGPUTimingClient();
95 EXPECT_TRUE(client2->IsForceTimeElapsedQuery());
98 } // namespace gpu