| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/process_util.h" | 5 #include "base/process_util.h" |
| 6 #include "ipc/ipc_platform_file.h" | 6 #include "media/video/capture/screen/shared_buffer.h" |
| 7 #include "remoting/capturer/shared_buffer.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 8 |
| 10 const uint32 kBufferSize = 4096; | 9 const uint32 kBufferSize = 4096; |
| 11 const int kPattern = 0x12345678; | 10 const int kPattern = 0x12345678; |
| 12 | 11 |
| 13 const int kIdZero = 0; | 12 const int kIdZero = 0; |
| 14 const int kIdOne = 1; | 13 const int kIdOne = 1; |
| 15 | 14 |
| 16 namespace remoting { | 15 namespace media { |
| 17 | 16 |
| 18 TEST(SharedBufferTest, Basic) { | 17 TEST(SharedBufferTest, Basic) { |
| 19 scoped_refptr<SharedBuffer> source(new SharedBuffer(kBufferSize)); | 18 scoped_refptr<SharedBuffer> source(new SharedBuffer(kBufferSize)); |
| 20 | 19 |
| 21 // Make sure that the buffer is allocated, the size is recorded correctly and | 20 // Make sure that the buffer is allocated, the size is recorded correctly and |
| 22 // its ID is reset. | 21 // its ID is reset. |
| 23 EXPECT_TRUE(source->ptr() != NULL); | 22 EXPECT_TRUE(source->ptr() != NULL); |
| 24 EXPECT_EQ(source->id(), kIdZero); | 23 EXPECT_EQ(source->id(), kIdZero); |
| 25 EXPECT_EQ(source->size(), kBufferSize); | 24 EXPECT_EQ(source->size(), kBufferSize); |
| 26 | 25 |
| 27 // See if setting of the ID works. | 26 // See if setting of the ID works. |
| 28 source->set_id(kIdOne); | 27 source->set_id(kIdOne); |
| 29 EXPECT_EQ(source->id(), kIdOne); | 28 EXPECT_EQ(source->id(), kIdOne); |
| 30 | 29 |
| 31 #if defined(OS_POSIX) | 30 base::SharedMemoryHandle copied_handle; |
| 32 base::PlatformFile source_handle = source->handle().fd; | 31 EXPECT_TRUE(source->ShareToProcess( |
| 33 #else // !defined(OS_POSIX) | 32 base::GetCurrentProcessHandle(), &copied_handle)); |
| 34 base::PlatformFile source_handle = source->handle(); | |
| 35 #endif // !defined(OS_POSIX) | |
| 36 | |
| 37 // Duplicate the source handle. | |
| 38 IPC::PlatformFileForTransit copied_handle = IPC::GetFileHandleForProcess( | |
| 39 source_handle, base::GetCurrentProcessHandle(), false); | |
| 40 | 33 |
| 41 scoped_refptr<SharedBuffer> dest( | 34 scoped_refptr<SharedBuffer> dest( |
| 42 new SharedBuffer(kIdZero, copied_handle, kBufferSize)); | 35 new SharedBuffer(kIdZero, copied_handle, kBufferSize)); |
| 43 | 36 |
| 44 // Make sure that the buffer is allocated, the size is recorded correctly and | 37 // Make sure that the buffer is allocated, the size is recorded correctly and |
| 45 // its ID is reset. | 38 // its ID is reset. |
| 46 EXPECT_TRUE(dest->ptr() != NULL); | 39 EXPECT_TRUE(dest->ptr() != NULL); |
| 47 EXPECT_EQ(dest->id(), kIdZero); | 40 EXPECT_EQ(dest->id(), kIdZero); |
| 48 EXPECT_EQ(dest->size(), kBufferSize); | 41 EXPECT_EQ(dest->size(), kBufferSize); |
| 49 | 42 |
| 50 // Verify that the memory contents are the same for the two buffers. | 43 // Verify that the memory contents are the same for the two buffers. |
| 51 int* source_ptr = reinterpret_cast<int*>(source->ptr()); | 44 int* source_ptr = reinterpret_cast<int*>(source->ptr()); |
| 52 *source_ptr = kPattern; | 45 *source_ptr = kPattern; |
| 53 int* dest_ptr = reinterpret_cast<int*>(dest->ptr()); | 46 int* dest_ptr = reinterpret_cast<int*>(dest->ptr()); |
| 54 EXPECT_EQ(*source_ptr, *dest_ptr); | 47 EXPECT_EQ(*source_ptr, *dest_ptr); |
| 55 | 48 |
| 56 // Check that the destination buffer is still mapped even when the source | 49 // Check that the destination buffer is still mapped even when the source |
| 57 // buffer is destroyed. | 50 // buffer is destroyed. |
| 58 source = NULL; | 51 source = NULL; |
| 59 EXPECT_EQ(0x12345678, *dest_ptr); | 52 EXPECT_EQ(0x12345678, *dest_ptr); |
| 60 } | 53 } |
| 61 | 54 |
| 62 } // namespace remoting | 55 } // namespace media |
| OLD | NEW |