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( | |
|
danakj
2014/01/30 22:59:01
This test skips AllocateSharedBitmapForChild, can
| |
| 28 handle, base::GetCurrentProcessHandle(), id); | |
| 29 | |
| 30 scoped_ptr<cc::SharedBitmap> large_bitmap; | |
| 31 large_bitmap = manager_->GetSharedBitmapFromId(gfx::Size(1024, 1024), id); | |
| 32 #if !defined(OS_POSIX) | |
| 33 EXPECT_TRUE(large_bitmap.get() == NULL); | |
| 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, RemoveProcess) { | |
| 72 gfx::Size bitmap_size(1, 1); | |
| 73 size_t size_in_bytes = cc::SharedBitmap::GetSizeInBytes(bitmap_size); | |
| 74 scoped_ptr<base::SharedMemory> bitmap(new base::SharedMemory()); | |
| 75 bitmap->CreateAndMapAnonymous(size_in_bytes); | |
| 76 memset(bitmap->memory(), 0xff, size_in_bytes); | |
| 77 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | |
| 78 | |
| 79 base::SharedMemoryHandle handle; | |
| 80 bitmap->ShareToProcess(base::GetCurrentProcessHandle(), &handle); | |
| 81 manager_->ChildAllocatedSharedBitmap( | |
| 82 handle, base::GetCurrentProcessHandle(), id); | |
| 83 | |
| 84 manager_->ProcessRemoved(base::kNullProcessHandle); | |
| 85 | |
| 86 scoped_ptr<cc::SharedBitmap> shared_bitmap; | |
| 87 shared_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id); | |
| 88 ASSERT_TRUE(shared_bitmap.get() != NULL); | |
| 89 | |
| 90 manager_->ProcessRemoved(base::GetCurrentProcessHandle()); | |
| 91 | |
| 92 scoped_ptr<cc::SharedBitmap> shared_bitmap2; | |
| 93 shared_bitmap2 = manager_->GetSharedBitmapFromId(bitmap_size, id); | |
| 94 EXPECT_TRUE(shared_bitmap2.get() == NULL); | |
| 95 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), size_in_bytes), | |
| 96 0); | |
| 97 | |
| 98 shared_bitmap.reset(); | |
| 99 | |
| 100 // Should no-op. | |
| 101 manager_->ChildDeletedSharedBitmap(id); | |
| 102 } | |
| 103 | |
| 104 TEST_F(HostSharedBitmapManagerTest, AddDuplicate) { | |
| 105 gfx::Size bitmap_size(1, 1); | |
| 106 size_t size_in_bytes = cc::SharedBitmap::GetSizeInBytes(bitmap_size); | |
| 107 scoped_ptr<base::SharedMemory> bitmap(new base::SharedMemory()); | |
| 108 bitmap->CreateAndMapAnonymous(size_in_bytes); | |
| 109 memset(bitmap->memory(), 0xff, size_in_bytes); | |
| 110 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | |
| 111 | |
| 112 base::SharedMemoryHandle handle; | |
| 113 bitmap->ShareToProcess(base::GetCurrentProcessHandle(), &handle); | |
| 114 manager_->ChildAllocatedSharedBitmap( | |
| 115 handle, base::GetCurrentProcessHandle(), id); | |
| 116 | |
| 117 scoped_ptr<base::SharedMemory> bitmap2(new base::SharedMemory()); | |
| 118 bitmap2->CreateAndMapAnonymous(size_in_bytes); | |
| 119 memset(bitmap2->memory(), 0x00, size_in_bytes); | |
| 120 | |
| 121 manager_->ChildAllocatedSharedBitmap( | |
| 122 bitmap2->handle(), base::GetCurrentProcessHandle(), id); | |
| 123 | |
| 124 scoped_ptr<cc::SharedBitmap> shared_bitmap; | |
| 125 shared_bitmap = manager_->GetSharedBitmapFromId(bitmap_size, id); | |
| 126 ASSERT_TRUE(shared_bitmap.get() != NULL); | |
| 127 EXPECT_EQ(memcmp(shared_bitmap->pixels(), bitmap->memory(), size_in_bytes), | |
| 128 0); | |
| 129 } | |
| 130 | |
| 131 } // namespace | |
| 132 } // namespace content | |
| OLD | NEW |