| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include <stddef.h> | |
| 6 #include <string.h> | |
| 7 | |
| 8 #include "content/common/host_shared_bitmap_manager.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace content { | |
| 12 namespace { | |
| 13 | |
| 14 class HostSharedBitmapManagerTest : public testing::Test { | |
| 15 protected: | |
| 16 void SetUp() override { manager_.reset(new HostSharedBitmapManager()); } | |
| 17 std::unique_ptr<HostSharedBitmapManager> manager_; | |
| 18 }; | |
| 19 | |
| 20 TEST_F(HostSharedBitmapManagerTest, TestCreate) { | |
| 21 gfx::Size bitmap_size(1, 1); | |
| 22 size_t size_in_bytes; | |
| 23 EXPECT_TRUE(cc::SharedBitmap::SizeInBytes(bitmap_size, &size_in_bytes)); | |
| 24 std::unique_ptr<base::SharedMemory> bitmap(new base::SharedMemory()); | |
| 25 bitmap->CreateAndMapAnonymous(size_in_bytes); | |
| 26 memset(bitmap->memory(), 0xff, size_in_bytes); | |
| 27 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | |
| 28 | |
| 29 HostSharedBitmapManagerClient client(manager_.get()); | |
| 30 base::SharedMemoryHandle handle; | |
| 31 bitmap->ShareToProcess(base::GetCurrentProcessHandle(), &handle); | |
| 32 client.ChildAllocatedSharedBitmap(size_in_bytes, handle, id); | |
| 33 | |
| 34 std::unique_ptr<cc::SharedBitmap> large_bitmap; | |
| 35 large_bitmap = manager_->GetSharedBitmapFromId(gfx::Size(1024, 1024), id); | |
| 36 EXPECT_TRUE(large_bitmap.get() == NULL); | |
| 37 | |
| 38 std::unique_ptr<cc::SharedBitmap> very_large_bitmap; | |
| 39 very_large_bitmap = | |
| 40 manager_->GetSharedBitmapFromId(gfx::Size(1, (1 << 30) | 1), id); | |
| 41 EXPECT_TRUE(very_large_bitmap.get() == NULL); | |
| 42 | |
| 43 std::unique_ptr<cc::SharedBitmap> negative_size_bitmap; | |
| 44 negative_size_bitmap = | |
| 45 manager_->GetSharedBitmapFromId(gfx::Size(-1, 1024), id); | |
| 46 EXPECT_TRUE(negative_size_bitmap.get() == NULL); | |
| 47 | |
| 48 cc::SharedBitmapId id2 = cc::SharedBitmap::GenerateId(); | |
| 49 std::unique_ptr<cc::SharedBitmap> invalid_bitmap; | |
| 50 invalid_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id2); | |
| 51 EXPECT_TRUE(invalid_bitmap.get() == NULL); | |
| 52 | |
| 53 std::unique_ptr<cc::SharedBitmap> shared_bitmap; | |
| 54 shared_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id); | |
| 55 ASSERT_TRUE(shared_bitmap.get() != NULL); | |
| 56 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), 4), 0); | |
| 57 | |
| 58 std::unique_ptr<cc::SharedBitmap> large_bitmap2; | |
| 59 large_bitmap2 = manager_->GetSharedBitmapFromId(gfx::Size(1024, 1024), id); | |
| 60 EXPECT_TRUE(large_bitmap2.get() == NULL); | |
| 61 | |
| 62 std::unique_ptr<cc::SharedBitmap> shared_bitmap2; | |
| 63 shared_bitmap2 = manager_->GetSharedBitmapFromId(bitmap_size, id); | |
| 64 EXPECT_TRUE(shared_bitmap2->pixels() == shared_bitmap->pixels()); | |
| 65 shared_bitmap2.reset(); | |
| 66 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), size_in_bytes), | |
| 67 0); | |
| 68 | |
| 69 client.ChildDeletedSharedBitmap(id); | |
| 70 | |
| 71 memset(bitmap->memory(), 0, size_in_bytes); | |
| 72 | |
| 73 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), size_in_bytes), | |
| 74 0); | |
| 75 bitmap.reset(); | |
| 76 shared_bitmap.reset(); | |
| 77 } | |
| 78 | |
| 79 TEST_F(HostSharedBitmapManagerTest, TestCreateForChild) { | |
| 80 gfx::Size bitmap_size(1, 1); | |
| 81 size_t size_in_bytes; | |
| 82 EXPECT_TRUE(cc::SharedBitmap::SizeInBytes(bitmap_size, &size_in_bytes)); | |
| 83 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | |
| 84 HostSharedBitmapManagerClient client(manager_.get()); | |
| 85 base::SharedMemoryHandle handle; | |
| 86 client.AllocateSharedBitmapForChild(base::GetCurrentProcessHandle(), | |
| 87 size_in_bytes, id, &handle); | |
| 88 | |
| 89 EXPECT_TRUE(base::SharedMemory::IsHandleValid(handle)); | |
| 90 std::unique_ptr<base::SharedMemory> bitmap( | |
| 91 new base::SharedMemory(handle, false)); | |
| 92 EXPECT_TRUE(bitmap->Map(size_in_bytes)); | |
| 93 memset(bitmap->memory(), 0xff, size_in_bytes); | |
| 94 | |
| 95 std::unique_ptr<cc::SharedBitmap> shared_bitmap; | |
| 96 shared_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id); | |
| 97 EXPECT_TRUE(shared_bitmap); | |
| 98 EXPECT_TRUE( | |
| 99 memcmp(bitmap->memory(), shared_bitmap->pixels(), size_in_bytes) == 0); | |
| 100 | |
| 101 client.ChildDeletedSharedBitmap(id); | |
| 102 } | |
| 103 | |
| 104 TEST_F(HostSharedBitmapManagerTest, RemoveProcess) { | |
| 105 gfx::Size bitmap_size(1, 1); | |
| 106 size_t size_in_bytes; | |
| 107 EXPECT_TRUE(cc::SharedBitmap::SizeInBytes(bitmap_size, &size_in_bytes)); | |
| 108 std::unique_ptr<base::SharedMemory> bitmap(new base::SharedMemory()); | |
| 109 bitmap->CreateAndMapAnonymous(size_in_bytes); | |
| 110 memset(bitmap->memory(), 0xff, size_in_bytes); | |
| 111 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | |
| 112 | |
| 113 base::SharedMemoryHandle handle; | |
| 114 std::unique_ptr<HostSharedBitmapManagerClient> client( | |
| 115 new HostSharedBitmapManagerClient(manager_.get())); | |
| 116 bitmap->ShareToProcess(base::GetCurrentProcessHandle(), &handle); | |
| 117 client->ChildAllocatedSharedBitmap(size_in_bytes, handle, id); | |
| 118 | |
| 119 std::unique_ptr<cc::SharedBitmap> shared_bitmap; | |
| 120 shared_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id); | |
| 121 ASSERT_TRUE(shared_bitmap.get() != NULL); | |
| 122 | |
| 123 EXPECT_EQ(1u, manager_->AllocatedBitmapCount()); | |
| 124 client.reset(); | |
| 125 EXPECT_EQ(0u, manager_->AllocatedBitmapCount()); | |
| 126 | |
| 127 std::unique_ptr<cc::SharedBitmap> shared_bitmap2; | |
| 128 shared_bitmap2 = manager_->GetSharedBitmapFromId(bitmap_size, id); | |
| 129 EXPECT_TRUE(shared_bitmap2.get() == NULL); | |
| 130 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), size_in_bytes), | |
| 131 0); | |
| 132 | |
| 133 shared_bitmap.reset(); | |
| 134 } | |
| 135 | |
| 136 TEST_F(HostSharedBitmapManagerTest, AddDuplicate) { | |
| 137 gfx::Size bitmap_size(1, 1); | |
| 138 size_t size_in_bytes; | |
| 139 EXPECT_TRUE(cc::SharedBitmap::SizeInBytes(bitmap_size, &size_in_bytes)); | |
| 140 std::unique_ptr<base::SharedMemory> bitmap(new base::SharedMemory()); | |
| 141 bitmap->CreateAndMapAnonymous(size_in_bytes); | |
| 142 memset(bitmap->memory(), 0xff, size_in_bytes); | |
| 143 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | |
| 144 HostSharedBitmapManagerClient client(manager_.get()); | |
| 145 | |
| 146 base::SharedMemoryHandle handle; | |
| 147 bitmap->ShareToProcess(base::GetCurrentProcessHandle(), &handle); | |
| 148 client.ChildAllocatedSharedBitmap(size_in_bytes, handle, id); | |
| 149 | |
| 150 std::unique_ptr<base::SharedMemory> bitmap2(new base::SharedMemory()); | |
| 151 bitmap2->CreateAndMapAnonymous(size_in_bytes); | |
| 152 memset(bitmap2->memory(), 0x00, size_in_bytes); | |
| 153 | |
| 154 client.ChildAllocatedSharedBitmap(size_in_bytes, bitmap2->handle(), id); | |
| 155 | |
| 156 std::unique_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), | |
| 160 0); | |
| 161 client.ChildDeletedSharedBitmap(id); | |
| 162 } | |
| 163 | |
| 164 } // namespace | |
| 165 } // namespace content | |
| OLD | NEW |