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.
4
5#include "cc/resources/scoped_resource.h"
6
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"
12
13namespace cc {
14namespace {
15
16TEST(ScopedResourceTest, NewScopedResource) {
17  FakeOutputSurfaceClient output_surface_client;
18  scoped_ptr<OutputSurface> output_surface(FakeOutputSurface::Create3d());
19  CHECK(output_surface->BindToClient(&output_surface_client));
20
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());
25
26  // New scoped textures do not hold a texture yet.
27  EXPECT_EQ(0u, texture->id());
28
29  // New scoped textures do not have a size yet.
30  EXPECT_EQ(gfx::Size(), texture->size());
31  EXPECT_EQ(0u, texture->bytes());
32}
33
34TEST(ScopedResourceTest, CreateScopedResource) {
35  FakeOutputSurfaceClient output_surface_client;
36  scoped_ptr<OutputSurface> output_surface(FakeOutputSurface::Create3d());
37  CHECK(output_surface->BindToClient(&output_surface_client));
38
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,
45                    RGBA_8888);
46
47  // The texture has an allocated byte-size now.
48  size_t expected_bytes = 30 * 30 * 4;
49  EXPECT_EQ(expected_bytes, texture->bytes());
50
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());
54}
55
56TEST(ScopedResourceTest, ScopedResourceIsDeleted) {
57  FakeOutputSurfaceClient output_surface_client;
58  scoped_ptr<OutputSurface> output_surface(FakeOutputSurface::Create3d());
59  CHECK(output_surface->BindToClient(&output_surface_client));
60
61  scoped_ptr<ResourceProvider> resource_provider(
62      ResourceProvider::Create(output_surface.get(), NULL, 0, false, 1));
63  {
64    scoped_ptr<ScopedResource> texture =
65        ScopedResource::Create(resource_provider.get());
66
67    EXPECT_EQ(0u, resource_provider->num_resources());
68    texture->Allocate(gfx::Size(30, 30),
69                      ResourceProvider::TextureUsageAny,
70                      RGBA_8888);
71    EXPECT_LT(0u, texture->id());
72    EXPECT_EQ(1u, resource_provider->num_resources());
73  }
74
75  EXPECT_EQ(0u, resource_provider->num_resources());
76  {
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,
82                      RGBA_8888);
83    EXPECT_LT(0u, texture->id());
84    EXPECT_EQ(1u, resource_provider->num_resources());
85    texture->Free();
86    EXPECT_EQ(0u, resource_provider->num_resources());
87  }
88}
89
90TEST(ScopedResourceTest, LeakScopedResource) {
91  FakeOutputSurfaceClient output_surface_client;
92  scoped_ptr<OutputSurface> output_surface(FakeOutputSurface::Create3d());
93  CHECK(output_surface->BindToClient(&output_surface_client));
94
95  scoped_ptr<ResourceProvider> resource_provider(
96      ResourceProvider::Create(output_surface.get(), NULL, 0, false, 1));
97  {
98    scoped_ptr<ScopedResource> texture =
99        ScopedResource::Create(resource_provider.get());
100
101    EXPECT_EQ(0u, resource_provider->num_resources());
102    texture->Allocate(gfx::Size(30, 30),
103                      ResourceProvider::TextureUsageAny,
104                      RGBA_8888);
105    EXPECT_LT(0u, texture->id());
106    EXPECT_EQ(1u, resource_provider->num_resources());
107
108    texture->Leak();
109    EXPECT_EQ(0u, texture->id());
110    EXPECT_EQ(1u, resource_provider->num_resources());
111
112    texture->Free();
113    EXPECT_EQ(0u, texture->id());
114    EXPECT_EQ(1u, resource_provider->num_resources());
115  }
116
117  EXPECT_EQ(1u, resource_provider->num_resources());
118}
119
120}  // namespace
121}  // namespace cc
122