1 // Copyright 2012 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 "cc/resources/scoped_resource.h"
7 #include "cc/output/renderer.h"
8 #include "cc/test/fake_output_surface.h"
9 #include "cc/test/fake_output_surface_client.h"
10 #include "cc/test/tiled_layer_test_common.h"
11 #include "testing/gtest/include/gtest/gtest.h"
16 TEST(ScopedResourceTest
, NewScopedResource
) {
17 FakeOutputSurfaceClient output_surface_client
;
18 scoped_ptr
<OutputSurface
> output_surface(FakeOutputSurface::Create3d());
19 CHECK(output_surface
->BindToClient(&output_surface_client
));
21 scoped_ptr
<ResourceProvider
> resource_provider(
22 ResourceProvider::Create(output_surface
.get(), NULL
, 0, false, 1));
23 scoped_ptr
<ScopedResource
> texture
=
24 ScopedResource::create(resource_provider
.get());
26 // New scoped textures do not hold a texture yet.
27 EXPECT_EQ(0u, texture
->id());
29 // New scoped textures do not have a size yet.
30 EXPECT_EQ(gfx::Size(), texture
->size());
31 EXPECT_EQ(0u, texture
->bytes());
34 TEST(ScopedResourceTest
, CreateScopedResource
) {
35 FakeOutputSurfaceClient output_surface_client
;
36 scoped_ptr
<OutputSurface
> output_surface(FakeOutputSurface::Create3d());
37 CHECK(output_surface
->BindToClient(&output_surface_client
));
39 scoped_ptr
<ResourceProvider
> resource_provider(
40 ResourceProvider::Create(output_surface
.get(), NULL
, 0, false, 1));
41 scoped_ptr
<ScopedResource
> texture
=
42 ScopedResource::create(resource_provider
.get());
43 texture
->Allocate(gfx::Size(30, 30),
44 ResourceProvider::TextureUsageAny
,
47 // The texture has an allocated byte-size now.
48 size_t expected_bytes
= 30 * 30 * 4;
49 EXPECT_EQ(expected_bytes
, texture
->bytes());
51 EXPECT_LT(0u, texture
->id());
52 EXPECT_EQ(static_cast<unsigned>(RGBA_8888
), texture
->format());
53 EXPECT_EQ(gfx::Size(30, 30), texture
->size());
56 TEST(ScopedResourceTest
, ScopedResourceIsDeleted
) {
57 FakeOutputSurfaceClient output_surface_client
;
58 scoped_ptr
<OutputSurface
> output_surface(FakeOutputSurface::Create3d());
59 CHECK(output_surface
->BindToClient(&output_surface_client
));
61 scoped_ptr
<ResourceProvider
> resource_provider(
62 ResourceProvider::Create(output_surface
.get(), NULL
, 0, false, 1));
64 scoped_ptr
<ScopedResource
> texture
=
65 ScopedResource::create(resource_provider
.get());
67 EXPECT_EQ(0u, resource_provider
->num_resources());
68 texture
->Allocate(gfx::Size(30, 30),
69 ResourceProvider::TextureUsageAny
,
71 EXPECT_LT(0u, texture
->id());
72 EXPECT_EQ(1u, resource_provider
->num_resources());
75 EXPECT_EQ(0u, resource_provider
->num_resources());
77 scoped_ptr
<ScopedResource
> texture
=
78 ScopedResource::create(resource_provider
.get());
79 EXPECT_EQ(0u, resource_provider
->num_resources());
80 texture
->Allocate(gfx::Size(30, 30),
81 ResourceProvider::TextureUsageAny
,
83 EXPECT_LT(0u, texture
->id());
84 EXPECT_EQ(1u, resource_provider
->num_resources());
86 EXPECT_EQ(0u, resource_provider
->num_resources());
90 TEST(ScopedResourceTest
, LeakScopedResource
) {
91 FakeOutputSurfaceClient output_surface_client
;
92 scoped_ptr
<OutputSurface
> output_surface(FakeOutputSurface::Create3d());
93 CHECK(output_surface
->BindToClient(&output_surface_client
));
95 scoped_ptr
<ResourceProvider
> resource_provider(
96 ResourceProvider::Create(output_surface
.get(), NULL
, 0, false, 1));
98 scoped_ptr
<ScopedResource
> texture
=
99 ScopedResource::create(resource_provider
.get());
101 EXPECT_EQ(0u, resource_provider
->num_resources());
102 texture
->Allocate(gfx::Size(30, 30),
103 ResourceProvider::TextureUsageAny
,
105 EXPECT_LT(0u, texture
->id());
106 EXPECT_EQ(1u, resource_provider
->num_resources());
109 EXPECT_EQ(0u, texture
->id());
110 EXPECT_EQ(1u, resource_provider
->num_resources());
113 EXPECT_EQ(0u, texture
->id());
114 EXPECT_EQ(1u, resource_provider
->num_resources());
117 EXPECT_EQ(1u, resource_provider
->num_resources());