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 void SetUp() override
{ 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);
96 manager_
->ChildDeletedSharedBitmap(id
);
99 TEST_F(HostSharedBitmapManagerTest
, RemoveProcess
) {
100 gfx::Size
bitmap_size(1, 1);
101 size_t size_in_bytes
;
102 EXPECT_TRUE(cc::SharedBitmap::SizeInBytes(bitmap_size
, &size_in_bytes
));
103 scoped_ptr
<base::SharedMemory
> bitmap(new base::SharedMemory());
104 bitmap
->CreateAndMapAnonymous(size_in_bytes
);
105 memset(bitmap
->memory(), 0xff, size_in_bytes
);
106 cc::SharedBitmapId id
= cc::SharedBitmap::GenerateId();
108 base::SharedMemoryHandle handle
;
109 bitmap
->ShareToProcess(base::GetCurrentProcessHandle(), &handle
);
110 manager_
->ChildAllocatedSharedBitmap(
111 size_in_bytes
, handle
, base::GetCurrentProcessHandle(), id
);
113 manager_
->ProcessRemoved(base::kNullProcessHandle
);
115 scoped_ptr
<cc::SharedBitmap
> shared_bitmap
;
116 shared_bitmap
= manager_
->GetSharedBitmapFromId(bitmap_size
, id
);
117 ASSERT_TRUE(shared_bitmap
.get() != NULL
);
119 manager_
->ProcessRemoved(base::GetCurrentProcessHandle());
121 scoped_ptr
<cc::SharedBitmap
> shared_bitmap2
;
122 shared_bitmap2
= manager_
->GetSharedBitmapFromId(bitmap_size
, id
);
123 EXPECT_TRUE(shared_bitmap2
.get() == NULL
);
124 EXPECT_EQ(memcmp(shared_bitmap
->pixels(), bitmap
->memory(), size_in_bytes
),
127 shared_bitmap
.reset();
130 manager_
->ChildDeletedSharedBitmap(id
);
133 TEST_F(HostSharedBitmapManagerTest
, AddDuplicate
) {
134 gfx::Size
bitmap_size(1, 1);
135 size_t size_in_bytes
;
136 EXPECT_TRUE(cc::SharedBitmap::SizeInBytes(bitmap_size
, &size_in_bytes
));
137 scoped_ptr
<base::SharedMemory
> bitmap(new base::SharedMemory());
138 bitmap
->CreateAndMapAnonymous(size_in_bytes
);
139 memset(bitmap
->memory(), 0xff, size_in_bytes
);
140 cc::SharedBitmapId id
= cc::SharedBitmap::GenerateId();
142 base::SharedMemoryHandle handle
;
143 bitmap
->ShareToProcess(base::GetCurrentProcessHandle(), &handle
);
144 manager_
->ChildAllocatedSharedBitmap(
145 size_in_bytes
, handle
, base::GetCurrentProcessHandle(), id
);
147 scoped_ptr
<base::SharedMemory
> bitmap2(new base::SharedMemory());
148 bitmap2
->CreateAndMapAnonymous(size_in_bytes
);
149 memset(bitmap2
->memory(), 0x00, size_in_bytes
);
151 manager_
->ChildAllocatedSharedBitmap(
152 size_in_bytes
, bitmap2
->handle(), base::GetCurrentProcessHandle(), id
);
154 scoped_ptr
<cc::SharedBitmap
> shared_bitmap
;
155 shared_bitmap
= manager_
->GetSharedBitmapFromId(bitmap_size
, id
);
156 ASSERT_TRUE(shared_bitmap
.get() != NULL
);
157 EXPECT_EQ(memcmp(shared_bitmap
->pixels(), bitmap
->memory(), size_in_bytes
),
159 manager_
->ChildDeletedSharedBitmap(id
);
163 } // namespace content