| Index: gpu/command_buffer/common/discardable_handle_unittest.cc
 | 
| diff --git a/gpu/command_buffer/common/discardable_handle_unittest.cc b/gpu/command_buffer/common/discardable_handle_unittest.cc
 | 
| new file mode 100644
 | 
| index 0000000000000000000000000000000000000000..17623589c707461e12aec4cffcf1a8191de90c25
 | 
| --- /dev/null
 | 
| +++ b/gpu/command_buffer/common/discardable_handle_unittest.cc
 | 
| @@ -0,0 +1,141 @@
 | 
| +// Copyright (c) 2017 The Chromium Authors. All rights reserved.
 | 
| +// Use of this source code is governed by a BSD-style license that can be
 | 
| +// found in the LICENSE file.
 | 
| +
 | 
| +#include "gpu/command_buffer/common/discardable_handle.h"
 | 
| +#include "gpu/command_buffer/common/buffer.h"
 | 
| +#include "testing/gtest/include/gtest/gtest.h"
 | 
| +
 | 
| +namespace gpu {
 | 
| +namespace {
 | 
| +
 | 
| +scoped_refptr<Buffer> MakeBufferForTesting(size_t num_handles) {
 | 
| +  size_t size = sizeof(base::subtle::Atomic32) * num_handles;
 | 
| +  std::unique_ptr<base::SharedMemory> shared_mem(new base::SharedMemory);
 | 
| +  shared_mem->CreateAndMapAnonymous(size);
 | 
| +  return MakeBufferFromSharedMemory(std::move(shared_mem), size);
 | 
| +}
 | 
| +
 | 
| +}  // namespace
 | 
| +
 | 
| +TEST(DiscardableHandleTest, BasicUsage) {
 | 
| +  scoped_refptr<Buffer> buffer = MakeBufferForTesting(1);
 | 
| +
 | 
| +  uint32_t byte_offset = 0;
 | 
| +  int32_t shm_id = 1;
 | 
| +  DiscardableHandle handle;
 | 
| +  EXPECT_FALSE(handle.IsInitialized());
 | 
| +
 | 
| +  handle.InitializeWithNewData(buffer, byte_offset, shm_id);
 | 
| +  EXPECT_EQ(handle.shm_id(), shm_id);
 | 
| +  EXPECT_TRUE(handle.IsInitialized());
 | 
| +  EXPECT_TRUE(handle.IsLockedForTesting());
 | 
| +
 | 
| +  EXPECT_FALSE(handle.Delete());
 | 
| +  EXPECT_FALSE(handle.IsDeleted());
 | 
| +
 | 
| +  handle.Unlock();
 | 
| +  EXPECT_FALSE(handle.IsLockedForTesting());
 | 
| +
 | 
| +  EXPECT_TRUE(handle.Lock());
 | 
| +  EXPECT_TRUE(handle.IsLockedForTesting());
 | 
| +
 | 
| +  handle.Unlock();
 | 
| +  EXPECT_FALSE(handle.IsLockedForTesting());
 | 
| +
 | 
| +  EXPECT_TRUE(handle.Delete());
 | 
| +  EXPECT_TRUE(handle.IsDeleted());
 | 
| +  EXPECT_FALSE(handle.IsLockedForTesting());
 | 
| +
 | 
| +  EXPECT_FALSE(handle.Lock());
 | 
| +  EXPECT_TRUE(handle.IsDeleted());
 | 
| +  EXPECT_FALSE(handle.IsLockedForTesting());
 | 
| +}
 | 
| +
 | 
| +TEST(DiscardableHandleTest, MultiLock) {
 | 
| +  scoped_refptr<Buffer> buffer = MakeBufferForTesting(1);
 | 
| +
 | 
| +  uint32_t byte_offset = 0;
 | 
| +  int32_t shm_id = 1;
 | 
| +  DiscardableHandle handle;
 | 
| +  EXPECT_FALSE(handle.IsInitialized());
 | 
| +
 | 
| +  handle.InitializeWithNewData(buffer, byte_offset, shm_id);
 | 
| +  EXPECT_EQ(handle.shm_id(), shm_id);
 | 
| +  EXPECT_TRUE(handle.IsInitialized());
 | 
| +  EXPECT_TRUE(handle.IsLockedForTesting());
 | 
| +
 | 
| +  for (int i = 1; i < 10; ++i) {
 | 
| +    EXPECT_TRUE(handle.IsLockedForTesting());
 | 
| +    EXPECT_TRUE(handle.Lock());
 | 
| +  }
 | 
| +
 | 
| +  for (int i = 0; i < 10; ++i) {
 | 
| +    EXPECT_TRUE(handle.IsLockedForTesting());
 | 
| +    handle.Unlock();
 | 
| +  }
 | 
| +
 | 
| +  EXPECT_FALSE(handle.IsLockedForTesting());
 | 
| +}
 | 
| +
 | 
| +TEST(DiscardableHandleTest, Suballocations) {
 | 
| +  static const size_t num_elements = 10;
 | 
| +  scoped_refptr<Buffer> buffer = MakeBufferForTesting(num_elements);
 | 
| +
 | 
| +  DiscardableHandle handles[num_elements];
 | 
| +  for (int i = 0; i < num_elements; ++i) {
 | 
| +    EXPECT_FALSE(handles[i].IsInitialized());
 | 
| +    handles[i].InitializeWithNewData(buffer, sizeof(base::subtle::Atomic32) * i,
 | 
| +                                     i + 1);
 | 
| +    EXPECT_EQ(handles[i].shm_id(), i + 1);
 | 
| +    EXPECT_TRUE(handles[i].IsInitialized());
 | 
| +    EXPECT_TRUE(handles[i].IsLockedForTesting());
 | 
| +  }
 | 
| +
 | 
| +  for (int i = 0; i < num_elements; i += 2) {
 | 
| +    handles[i].Unlock();
 | 
| +  }
 | 
| +
 | 
| +  for (int i = 1; i < num_elements; i += 2) {
 | 
| +    handles[i].Lock();
 | 
| +  }
 | 
| +
 | 
| +  for (int i = 0; i < num_elements; ++i) {
 | 
| +    if (i % 2)
 | 
| +      EXPECT_TRUE(handles[i].IsLockedForTesting());
 | 
| +    else
 | 
| +      EXPECT_FALSE(handles[i].IsLockedForTesting());
 | 
| +  }
 | 
| +}
 | 
| +
 | 
| +TEST(DiscardableHandleTest, ExistingData) {
 | 
| +  scoped_refptr<Buffer> buffer = MakeBufferForTesting(1);
 | 
| +
 | 
| +  uint32_t byte_offset = 0;
 | 
| +  int32_t shm_id = 1;
 | 
| +  DiscardableHandle init_handle;
 | 
| +  EXPECT_FALSE(init_handle.IsInitialized());
 | 
| +
 | 
| +  init_handle.InitializeWithNewData(buffer, byte_offset, shm_id);
 | 
| +  EXPECT_EQ(init_handle.shm_id(), shm_id);
 | 
| +  EXPECT_TRUE(init_handle.IsInitialized());
 | 
| +  EXPECT_TRUE(init_handle.IsLockedForTesting());
 | 
| +
 | 
| +  init_handle.Unlock();
 | 
| +  EXPECT_FALSE(init_handle.IsLockedForTesting());
 | 
| +
 | 
| +  DiscardableHandle handle;
 | 
| +  EXPECT_FALSE(handle.IsInitialized());
 | 
| +
 | 
| +  handle.InitializeWithExistingData(buffer, init_handle.byte_offset(),
 | 
| +                                    init_handle.shm_id());
 | 
| +  EXPECT_EQ(handle.shm_id(), shm_id);
 | 
| +  EXPECT_TRUE(handle.IsInitialized());
 | 
| +  EXPECT_FALSE(handle.IsLockedForTesting());
 | 
| +
 | 
| +  EXPECT_TRUE(handle.Delete());
 | 
| +  EXPECT_TRUE(handle.IsDeleted());
 | 
| +  EXPECT_TRUE(init_handle.IsDeleted());
 | 
| +}
 | 
| +
 | 
| +}  // namespace gpu
 | 
| 
 |