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