summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/service/resource_test.cc
blob: e86f679e1e0d03a22e6c148e871ad9fb3db56504 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2009 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.

// Tests for the ResourceMap.

#include "gpu/command_buffer/service/precompile.h"
#include "gpu/command_buffer/service/resource.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace gpu {

// Mock resource implementation that checks for leaks.
class ResourceMock : public Resource {
 public:
  ResourceMock() : Resource() {
    ++instance_count_;
  }
  virtual ~ResourceMock() {
    --instance_count_;
  }

  // Returns the instance count. The instance count is increased in the
  // constructor and decreased in the destructor, to track leaks. The reason is
  // that we can't mock the destructor, though we want to make sure the mock is
  // destroyed.
  static int instance_count() { return instance_count_; }
 private:
  static int instance_count_;
  DISALLOW_COPY_AND_ASSIGN(ResourceMock);
};
int ResourceMock::instance_count_ = 0;

// Test fixture for ResourceMap test. Creates a ResourceMap using a mock
// Resource, and checks for ResourceMock leaks.
class ResourceMapTest : public testing::Test {
 protected:
  typedef ResourceMap<ResourceMock> Map;
  virtual void SetUp() {
    instance_count_ = ResourceMock::instance_count();
    map_.reset(new Map());
  }
  virtual void TearDown() {
    CheckLeaks();
  }

  // Makes sure we didn't leak any ResourceMock object.
  void CheckLeaks() {
    EXPECT_EQ(instance_count_, ResourceMock::instance_count());
  }

  Map *map() const { return map_.get(); }
 private:
  int instance_count_;
  scoped_ptr<Map> map_;
};

TEST_F(ResourceMapTest, TestMap) {
  // check that initial mapping is empty.
  EXPECT_EQ(NULL, map()->Get(0));
  EXPECT_EQ(NULL, map()->Get(1));
  EXPECT_EQ(NULL, map()->Get(392));

  // create a new resource, assign it to an ID.
  ResourceMock *resource = new ResourceMock();
  map()->Assign(123, resource);
  EXPECT_EQ(resource, map()->Get(123));

  // Destroy the resource, making sure the object is deleted.
  EXPECT_EQ(true, map()->Destroy(123));
  EXPECT_EQ(false, map()->Destroy(123));  // destroying again should fail.
  resource = NULL;
  CheckLeaks();

  // create a new resource, add it to the map, and make sure it gets deleted
  // when we assign a new resource to that ID.
  resource = new ResourceMock();
  map()->Assign(1, resource);
  resource = new ResourceMock();
  map()->Assign(1, resource);
  EXPECT_EQ(resource, map()->Get(1));  // check that we have the new resource.
  EXPECT_EQ(true, map()->Destroy(1));
  CheckLeaks();

  // Adds 3 resources, then call DestroyAllResources().
  resource = new ResourceMock();
  map()->Assign(1, resource);
  resource = new ResourceMock();
  map()->Assign(2, resource);
  resource = new ResourceMock();
  map()->Assign(3, resource);
  map()->DestroyAllResources();
  EXPECT_EQ(NULL, map()->Get(1));
  EXPECT_EQ(NULL, map()->Get(2));
  EXPECT_EQ(NULL, map()->Get(3));
  CheckLeaks();
}

}  // namespace gpu