BlinkGCPlugin: Factor out CheckFieldsVisitor from BlinkGCPlugin.cpp.
[chromium-blink-merge.git] / content / common / host_shared_bitmap_manager_unittest.cc
blob308402e36a58b40a00bb8b6f68dfdc4d669f729f
1 // Copyright (c) 2013 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 "content/common/host_shared_bitmap_manager.h"
6 #include "testing/gtest/include/gtest/gtest.h"
8 namespace content {
9 namespace {
11 class HostSharedBitmapManagerTest : public testing::Test {
12 protected:
13 void SetUp() override { manager_.reset(new HostSharedBitmapManager()); }
14 scoped_ptr<HostSharedBitmapManager> manager_;
17 TEST_F(HostSharedBitmapManagerTest, TestCreate) {
18 gfx::Size bitmap_size(1, 1);
19 size_t size_in_bytes;
20 EXPECT_TRUE(cc::SharedBitmap::SizeInBytes(bitmap_size, &size_in_bytes));
21 scoped_ptr<base::SharedMemory> bitmap(new base::SharedMemory());
22 bitmap->CreateAndMapAnonymous(size_in_bytes);
23 memset(bitmap->memory(), 0xff, size_in_bytes);
24 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
26 HostSharedBitmapManagerClient client(manager_.get());
27 base::SharedMemoryHandle handle;
28 bitmap->ShareToProcess(base::GetCurrentProcessHandle(), &handle);
29 client.ChildAllocatedSharedBitmap(size_in_bytes, handle,
30 base::GetCurrentProcessHandle(), id);
32 scoped_ptr<cc::SharedBitmap> large_bitmap;
33 large_bitmap = manager_->GetSharedBitmapFromId(gfx::Size(1024, 1024), id);
34 EXPECT_TRUE(large_bitmap.get() == NULL);
36 scoped_ptr<cc::SharedBitmap> very_large_bitmap;
37 very_large_bitmap =
38 manager_->GetSharedBitmapFromId(gfx::Size(1, (1 << 30) | 1), id);
39 EXPECT_TRUE(very_large_bitmap.get() == NULL);
41 scoped_ptr<cc::SharedBitmap> negative_size_bitmap;
42 negative_size_bitmap =
43 manager_->GetSharedBitmapFromId(gfx::Size(-1, 1024), id);
44 EXPECT_TRUE(negative_size_bitmap.get() == NULL);
46 cc::SharedBitmapId id2 = cc::SharedBitmap::GenerateId();
47 scoped_ptr<cc::SharedBitmap> invalid_bitmap;
48 invalid_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id2);
49 EXPECT_TRUE(invalid_bitmap.get() == NULL);
51 scoped_ptr<cc::SharedBitmap> shared_bitmap;
52 shared_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id);
53 ASSERT_TRUE(shared_bitmap.get() != NULL);
54 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), 4), 0);
56 scoped_ptr<cc::SharedBitmap> large_bitmap2;
57 large_bitmap2 = manager_->GetSharedBitmapFromId(gfx::Size(1024, 1024), id);
58 EXPECT_TRUE(large_bitmap2.get() == NULL);
60 scoped_ptr<cc::SharedBitmap> shared_bitmap2;
61 shared_bitmap2 = manager_->GetSharedBitmapFromId(bitmap_size, id);
62 EXPECT_TRUE(shared_bitmap2->pixels() == shared_bitmap->pixels());
63 shared_bitmap2.reset();
64 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), size_in_bytes),
65 0);
67 client.ChildDeletedSharedBitmap(id);
69 memset(bitmap->memory(), 0, size_in_bytes);
71 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), size_in_bytes),
72 0);
73 bitmap.reset();
74 shared_bitmap.reset();
77 TEST_F(HostSharedBitmapManagerTest, TestCreateForChild) {
78 gfx::Size bitmap_size(1, 1);
79 size_t size_in_bytes;
80 EXPECT_TRUE(cc::SharedBitmap::SizeInBytes(bitmap_size, &size_in_bytes));
81 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
82 HostSharedBitmapManagerClient client(manager_.get());
83 base::SharedMemoryHandle handle;
84 client.AllocateSharedBitmapForChild(base::GetCurrentProcessHandle(),
85 size_in_bytes, id, &handle);
87 EXPECT_TRUE(base::SharedMemory::IsHandleValid(handle));
88 scoped_ptr<base::SharedMemory> bitmap(new base::SharedMemory(handle, false));
89 EXPECT_TRUE(bitmap->Map(size_in_bytes));
90 memset(bitmap->memory(), 0xff, size_in_bytes);
92 scoped_ptr<cc::SharedBitmap> shared_bitmap;
93 shared_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id);
94 EXPECT_TRUE(shared_bitmap);
95 EXPECT_TRUE(
96 memcmp(bitmap->memory(), shared_bitmap->pixels(), size_in_bytes) == 0);
98 client.ChildDeletedSharedBitmap(id);
101 TEST_F(HostSharedBitmapManagerTest, RemoveProcess) {
102 gfx::Size bitmap_size(1, 1);
103 size_t size_in_bytes;
104 EXPECT_TRUE(cc::SharedBitmap::SizeInBytes(bitmap_size, &size_in_bytes));
105 scoped_ptr<base::SharedMemory> bitmap(new base::SharedMemory());
106 bitmap->CreateAndMapAnonymous(size_in_bytes);
107 memset(bitmap->memory(), 0xff, size_in_bytes);
108 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
110 base::SharedMemoryHandle handle;
111 scoped_ptr<HostSharedBitmapManagerClient> client(
112 new HostSharedBitmapManagerClient(manager_.get()));
113 bitmap->ShareToProcess(base::GetCurrentProcessHandle(), &handle);
114 client->ChildAllocatedSharedBitmap(size_in_bytes, handle,
115 base::GetCurrentProcessHandle(), id);
117 scoped_ptr<cc::SharedBitmap> shared_bitmap;
118 shared_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id);
119 ASSERT_TRUE(shared_bitmap.get() != NULL);
121 EXPECT_EQ(1u, manager_->AllocatedBitmapCount());
122 client.reset();
123 EXPECT_EQ(0u, manager_->AllocatedBitmapCount());
125 scoped_ptr<cc::SharedBitmap> shared_bitmap2;
126 shared_bitmap2 = manager_->GetSharedBitmapFromId(bitmap_size, id);
127 EXPECT_TRUE(shared_bitmap2.get() == NULL);
128 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), size_in_bytes),
131 shared_bitmap.reset();
134 TEST_F(HostSharedBitmapManagerTest, AddDuplicate) {
135 gfx::Size bitmap_size(1, 1);
136 size_t size_in_bytes;
137 EXPECT_TRUE(cc::SharedBitmap::SizeInBytes(bitmap_size, &size_in_bytes));
138 scoped_ptr<base::SharedMemory> bitmap(new base::SharedMemory());
139 bitmap->CreateAndMapAnonymous(size_in_bytes);
140 memset(bitmap->memory(), 0xff, size_in_bytes);
141 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
142 HostSharedBitmapManagerClient client(manager_.get());
144 base::SharedMemoryHandle handle;
145 bitmap->ShareToProcess(base::GetCurrentProcessHandle(), &handle);
146 client.ChildAllocatedSharedBitmap(size_in_bytes, handle,
147 base::GetCurrentProcessHandle(), id);
149 scoped_ptr<base::SharedMemory> bitmap2(new base::SharedMemory());
150 bitmap2->CreateAndMapAnonymous(size_in_bytes);
151 memset(bitmap2->memory(), 0x00, size_in_bytes);
153 client.ChildAllocatedSharedBitmap(size_in_bytes, bitmap2->handle(),
154 base::GetCurrentProcessHandle(), id);
156 scoped_ptr<cc::SharedBitmap> shared_bitmap;
157 shared_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id);
158 ASSERT_TRUE(shared_bitmap.get() != NULL);
159 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), size_in_bytes),
161 client.ChildDeletedSharedBitmap(id);
164 } // namespace
165 } // namespace content