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"
11 class HostSharedBitmapManagerTest
: public testing::Test
{
13 virtual void SetUp() { manager_
.reset(new HostSharedBitmapManager()); }
14 scoped_ptr
<HostSharedBitmapManager
> manager_
;
17 TEST_F(HostSharedBitmapManagerTest
, TestCreate
) {
18 gfx::Size
bitmap_size(1, 1);
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 base::SharedMemoryHandle handle
;
27 bitmap
->ShareToProcess(base::GetCurrentProcessHandle(), &handle
);
28 manager_
->ChildAllocatedSharedBitmap(
29 size_in_bytes
, handle
, base::GetCurrentProcessHandle(), id
);
31 scoped_ptr
<cc::SharedBitmap
> large_bitmap
;
32 large_bitmap
= manager_
->GetSharedBitmapFromId(gfx::Size(1024, 1024), id
);
33 EXPECT_TRUE(large_bitmap
.get() == NULL
);
35 scoped_ptr
<cc::SharedBitmap
> very_large_bitmap
;
37 manager_
->GetSharedBitmapFromId(gfx::Size(1, (1 << 30) | 1), id
);
38 EXPECT_TRUE(very_large_bitmap
.get() == NULL
);
40 scoped_ptr
<cc::SharedBitmap
> negative_size_bitmap
;
41 negative_size_bitmap
=
42 manager_
->GetSharedBitmapFromId(gfx::Size(-1, 1024), id
);
43 EXPECT_TRUE(negative_size_bitmap
.get() == NULL
);
45 cc::SharedBitmapId id2
= cc::SharedBitmap::GenerateId();
46 scoped_ptr
<cc::SharedBitmap
> invalid_bitmap
;
47 invalid_bitmap
= manager_
->GetSharedBitmapFromId(bitmap_size
, id2
);
48 EXPECT_TRUE(invalid_bitmap
.get() == NULL
);
50 scoped_ptr
<cc::SharedBitmap
> shared_bitmap
;
51 shared_bitmap
= manager_
->GetSharedBitmapFromId(bitmap_size
, id
);
52 ASSERT_TRUE(shared_bitmap
.get() != NULL
);
53 EXPECT_EQ(memcmp(shared_bitmap
->pixels(), bitmap
->memory(), 4), 0);
55 scoped_ptr
<cc::SharedBitmap
> large_bitmap2
;
56 large_bitmap2
= manager_
->GetSharedBitmapFromId(gfx::Size(1024, 1024), id
);
57 EXPECT_TRUE(large_bitmap2
.get() == NULL
);
59 scoped_ptr
<cc::SharedBitmap
> shared_bitmap2
;
60 shared_bitmap2
= manager_
->GetSharedBitmapFromId(bitmap_size
, id
);
61 EXPECT_TRUE(shared_bitmap2
->pixels() == shared_bitmap
->pixels());
62 shared_bitmap2
.reset();
63 EXPECT_EQ(memcmp(shared_bitmap
->pixels(), bitmap
->memory(), size_in_bytes
),
66 manager_
->ChildDeletedSharedBitmap(id
);
68 memset(bitmap
->memory(), 0, size_in_bytes
);
70 EXPECT_EQ(memcmp(shared_bitmap
->pixels(), bitmap
->memory(), size_in_bytes
),
73 shared_bitmap
.reset();
76 TEST_F(HostSharedBitmapManagerTest
, TestCreateForChild
) {
77 gfx::Size
bitmap_size(1, 1);
79 EXPECT_TRUE(cc::SharedBitmap::SizeInBytes(bitmap_size
, &size_in_bytes
));
80 cc::SharedBitmapId id
= cc::SharedBitmap::GenerateId();
81 base::SharedMemoryHandle handle
;
82 manager_
->AllocateSharedBitmapForChild(
83 base::GetCurrentProcessHandle(), size_in_bytes
, id
, &handle
);
85 EXPECT_TRUE(base::SharedMemory::IsHandleValid(handle
));
86 scoped_ptr
<base::SharedMemory
> bitmap(new base::SharedMemory(handle
, false));
87 EXPECT_TRUE(bitmap
->Map(size_in_bytes
));
88 memset(bitmap
->memory(), 0xff, size_in_bytes
);
90 scoped_ptr
<cc::SharedBitmap
> shared_bitmap
;
91 shared_bitmap
= manager_
->GetSharedBitmapFromId(bitmap_size
, id
);
92 EXPECT_TRUE(shared_bitmap
);
94 memcmp(bitmap
->memory(), shared_bitmap
->pixels(), size_in_bytes
) == 0);
97 TEST_F(HostSharedBitmapManagerTest
, RemoveProcess
) {
98 gfx::Size
bitmap_size(1, 1);
100 EXPECT_TRUE(cc::SharedBitmap::SizeInBytes(bitmap_size
, &size_in_bytes
));
101 scoped_ptr
<base::SharedMemory
> bitmap(new base::SharedMemory());
102 bitmap
->CreateAndMapAnonymous(size_in_bytes
);
103 memset(bitmap
->memory(), 0xff, size_in_bytes
);
104 cc::SharedBitmapId id
= cc::SharedBitmap::GenerateId();
106 base::SharedMemoryHandle handle
;
107 bitmap
->ShareToProcess(base::GetCurrentProcessHandle(), &handle
);
108 manager_
->ChildAllocatedSharedBitmap(
109 size_in_bytes
, handle
, base::GetCurrentProcessHandle(), id
);
111 manager_
->ProcessRemoved(base::kNullProcessHandle
);
113 scoped_ptr
<cc::SharedBitmap
> shared_bitmap
;
114 shared_bitmap
= manager_
->GetSharedBitmapFromId(bitmap_size
, id
);
115 ASSERT_TRUE(shared_bitmap
.get() != NULL
);
117 manager_
->ProcessRemoved(base::GetCurrentProcessHandle());
119 scoped_ptr
<cc::SharedBitmap
> shared_bitmap2
;
120 shared_bitmap2
= manager_
->GetSharedBitmapFromId(bitmap_size
, id
);
121 EXPECT_TRUE(shared_bitmap2
.get() == NULL
);
122 EXPECT_EQ(memcmp(shared_bitmap
->pixels(), bitmap
->memory(), size_in_bytes
),
125 shared_bitmap
.reset();
128 manager_
->ChildDeletedSharedBitmap(id
);
131 TEST_F(HostSharedBitmapManagerTest
, AddDuplicate
) {
132 gfx::Size
bitmap_size(1, 1);
133 size_t size_in_bytes
;
134 EXPECT_TRUE(cc::SharedBitmap::SizeInBytes(bitmap_size
, &size_in_bytes
));
135 scoped_ptr
<base::SharedMemory
> bitmap(new base::SharedMemory());
136 bitmap
->CreateAndMapAnonymous(size_in_bytes
);
137 memset(bitmap
->memory(), 0xff, size_in_bytes
);
138 cc::SharedBitmapId id
= cc::SharedBitmap::GenerateId();
140 base::SharedMemoryHandle handle
;
141 bitmap
->ShareToProcess(base::GetCurrentProcessHandle(), &handle
);
142 manager_
->ChildAllocatedSharedBitmap(
143 size_in_bytes
, handle
, base::GetCurrentProcessHandle(), id
);
145 scoped_ptr
<base::SharedMemory
> bitmap2(new base::SharedMemory());
146 bitmap2
->CreateAndMapAnonymous(size_in_bytes
);
147 memset(bitmap2
->memory(), 0x00, size_in_bytes
);
149 manager_
->ChildAllocatedSharedBitmap(
150 size_in_bytes
, bitmap2
->handle(), base::GetCurrentProcessHandle(), id
);
152 scoped_ptr
<cc::SharedBitmap
> shared_bitmap
;
153 shared_bitmap
= manager_
->GetSharedBitmapFromId(bitmap_size
, id
);
154 ASSERT_TRUE(shared_bitmap
.get() != NULL
);
155 EXPECT_EQ(memcmp(shared_bitmap
->pixels(), bitmap
->memory(), size_in_bytes
),
160 } // namespace content