Chromium Code Reviews| 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 handle, base::GetCurrentProcessHandle(), id); | |
| 29 | |
| 30 scoped_ptr<cc::SharedBitmap> large_bitmap; | |
| 31 large_bitmap = manager_->GetSharedBitmapFromId(gfx::Size(1024, 1024), id); | |
|
danakj
2014/02/14 20:16:48
Is it not super weird that this size doesn't match
jbauman
2014/02/18 19:31:35
It's not valid at all, so I'm testing that mapping
danakj
2014/02/18 19:57:29
But on posix you do end up mapping something that
| |
| 32 #if !defined(OS_POSIX) | |
| 33 EXPECT_TRUE(large_bitmap.get() == NULL); | |
|
danakj
2014/02/14 20:16:48
This is 4mb of memory, we can't map 4 megabytes on
| |
| 34 #endif | |
| 35 // On POSIX, the entire bitmap could be mapped, but there could be a SIGBUS | |
| 36 // if the address that's accessed is too large. | |
| 37 | |
| 38 cc::SharedBitmapId id2 = cc::SharedBitmap::GenerateId(); | |
| 39 scoped_ptr<cc::SharedBitmap> invalid_bitmap; | |
| 40 invalid_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id2); | |
| 41 EXPECT_TRUE(invalid_bitmap.get() == NULL); | |
| 42 | |
| 43 scoped_ptr<cc::SharedBitmap> shared_bitmap; | |
| 44 shared_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id); | |
| 45 ASSERT_TRUE(shared_bitmap.get() != NULL); | |
| 46 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), 4), 0); | |
| 47 | |
| 48 scoped_ptr<cc::SharedBitmap> large_bitmap2; | |
| 49 large_bitmap2 = manager_->GetSharedBitmapFromId(gfx::Size(1024, 1024), id); | |
| 50 #if !defined(OS_POSIX) | |
| 51 EXPECT_TRUE(large_bitmap2.get() == NULL); | |
| 52 #endif | |
| 53 | |
| 54 scoped_ptr<cc::SharedBitmap> shared_bitmap2; | |
| 55 shared_bitmap2 = manager_->GetSharedBitmapFromId(bitmap_size, id); | |
| 56 EXPECT_TRUE(shared_bitmap2->pixels() == shared_bitmap->pixels()); | |
| 57 shared_bitmap2.reset(); | |
| 58 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), size_in_bytes), | |
| 59 0); | |
| 60 | |
| 61 manager_->ChildDeletedSharedBitmap(id); | |
| 62 | |
| 63 memset(bitmap->memory(), 0, size_in_bytes); | |
| 64 | |
| 65 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), size_in_bytes), | |
| 66 0); | |
| 67 bitmap.reset(); | |
| 68 shared_bitmap.reset(); | |
| 69 } | |
| 70 | |
| 71 TEST_F(HostSharedBitmapManagerTest, TestCreateForChild) { | |
| 72 gfx::Size bitmap_size(1, 1); | |
| 73 size_t size_in_bytes = cc::SharedBitmap::GetSizeInBytes(bitmap_size); | |
| 74 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | |
| 75 base::SharedMemoryHandle handle; | |
| 76 manager_->AllocateSharedBitmapForChild( | |
| 77 base::GetCurrentProcessHandle(), size_in_bytes, id, &handle); | |
| 78 | |
| 79 EXPECT_TRUE(base::SharedMemory::IsHandleValid(handle)); | |
| 80 scoped_ptr<base::SharedMemory> bitmap(new base::SharedMemory(handle, false)); | |
| 81 EXPECT_TRUE(bitmap->Map(size_in_bytes)); | |
| 82 memset(bitmap->memory(), 0xff, size_in_bytes); | |
| 83 | |
| 84 scoped_ptr<cc::SharedBitmap> shared_bitmap; | |
| 85 shared_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id); | |
| 86 EXPECT_TRUE(shared_bitmap); | |
| 87 EXPECT_TRUE( | |
| 88 memcmp(bitmap->memory(), shared_bitmap->pixels(), size_in_bytes) == 0); | |
| 89 } | |
| 90 | |
| 91 TEST_F(HostSharedBitmapManagerTest, RemoveProcess) { | |
| 92 gfx::Size bitmap_size(1, 1); | |
| 93 size_t size_in_bytes = cc::SharedBitmap::GetSizeInBytes(bitmap_size); | |
| 94 scoped_ptr<base::SharedMemory> bitmap(new base::SharedMemory()); | |
| 95 bitmap->CreateAndMapAnonymous(size_in_bytes); | |
| 96 memset(bitmap->memory(), 0xff, size_in_bytes); | |
| 97 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | |
| 98 | |
| 99 base::SharedMemoryHandle handle; | |
| 100 bitmap->ShareToProcess(base::GetCurrentProcessHandle(), &handle); | |
| 101 manager_->ChildAllocatedSharedBitmap( | |
| 102 handle, base::GetCurrentProcessHandle(), id); | |
| 103 | |
| 104 manager_->ProcessRemoved(base::kNullProcessHandle); | |
| 105 | |
| 106 scoped_ptr<cc::SharedBitmap> shared_bitmap; | |
| 107 shared_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id); | |
| 108 ASSERT_TRUE(shared_bitmap.get() != NULL); | |
| 109 | |
| 110 manager_->ProcessRemoved(base::GetCurrentProcessHandle()); | |
| 111 | |
| 112 scoped_ptr<cc::SharedBitmap> shared_bitmap2; | |
| 113 shared_bitmap2 = manager_->GetSharedBitmapFromId(bitmap_size, id); | |
| 114 EXPECT_TRUE(shared_bitmap2.get() == NULL); | |
| 115 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), size_in_bytes), | |
| 116 0); | |
| 117 | |
| 118 shared_bitmap.reset(); | |
| 119 | |
| 120 // Should no-op. | |
| 121 manager_->ChildDeletedSharedBitmap(id); | |
| 122 } | |
| 123 | |
| 124 TEST_F(HostSharedBitmapManagerTest, AddDuplicate) { | |
| 125 gfx::Size bitmap_size(1, 1); | |
| 126 size_t size_in_bytes = cc::SharedBitmap::GetSizeInBytes(bitmap_size); | |
| 127 scoped_ptr<base::SharedMemory> bitmap(new base::SharedMemory()); | |
| 128 bitmap->CreateAndMapAnonymous(size_in_bytes); | |
| 129 memset(bitmap->memory(), 0xff, size_in_bytes); | |
| 130 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | |
| 131 | |
| 132 base::SharedMemoryHandle handle; | |
| 133 bitmap->ShareToProcess(base::GetCurrentProcessHandle(), &handle); | |
| 134 manager_->ChildAllocatedSharedBitmap( | |
| 135 handle, base::GetCurrentProcessHandle(), id); | |
|
danakj
2014/02/14 20:16:48
What about two different processes using the same
jbauman
2014/02/18 19:31:35
I don't think we have two valid process handles he
danakj
2014/02/18 19:57:29
I was hoping there's some way to generate fake pro
| |
| 136 | |
| 137 scoped_ptr<base::SharedMemory> bitmap2(new base::SharedMemory()); | |
| 138 bitmap2->CreateAndMapAnonymous(size_in_bytes); | |
| 139 memset(bitmap2->memory(), 0x00, size_in_bytes); | |
| 140 | |
| 141 manager_->ChildAllocatedSharedBitmap( | |
| 142 bitmap2->handle(), base::GetCurrentProcessHandle(), id); | |
| 143 | |
| 144 scoped_ptr<cc::SharedBitmap> shared_bitmap; | |
| 145 shared_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id); | |
| 146 ASSERT_TRUE(shared_bitmap.get() != NULL); | |
| 147 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), size_in_bytes), | |
| 148 0); | |
| 149 } | |
| 150 | |
| 151 } // namespace | |
| 152 } // namespace content | |
| OLD | NEW |