Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: content/browser/gamepad/gamepad_provider_unittest.cc

Issue 8772004: Improve GamepadSeqLock impl Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "base/process_util.h" 6 #include "base/process_util.h"
7 #include "base/synchronization/waitable_event.h" 7 #include "base/synchronization/waitable_event.h"
8 #include "base/system_monitor/system_monitor.h" 8 #include "base/system_monitor/system_monitor.h"
9 #include "base/threading/platform_thread.h"
9 #include "content/browser/gamepad/gamepad_provider.h" 10 #include "content/browser/gamepad/gamepad_provider.h"
10 #include "content/common/gamepad_messages.h" 11 #include "content/common/gamepad_messages.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
12 13
13 namespace content { 14 namespace content {
14 15
15 namespace { 16 namespace {
16 17
17 using WebKit::WebGamepads; 18 using WebKit::WebGamepads;
18 19
19 class MockDataFetcher : public GamepadDataFetcher { 20 class MockDataFetcher : public GamepadDataFetcher {
20 public: 21 public:
21 MockDataFetcher(WebGamepads& test_data) : read_data_(false, false) { 22 explicit MockDataFetcher(WebGamepads const& test_data) {
darin (slow to review) 2011/12/08 22:10:48 nit: please stick with the more canonical "const F
dvyukov 2011/12/13 13:46:20 Done.
22 test_data_ = test_data; 23 test_data_ = test_data;
23 } 24 }
24 virtual void GetGamepadData(WebGamepads* pads, 25 virtual void GetGamepadData(WebGamepads* pads,
25 bool devices_changed_hint) OVERRIDE { 26 bool devices_changed_hint) OVERRIDE {
26 *pads = test_data_; 27 *pads = test_data_;
27 read_data_.Signal();
28 } 28 }
29 29
30 void WaitForDataRead() { return read_data_.Wait(); }
31
32 WebGamepads test_data_; 30 WebGamepads test_data_;
33 base::WaitableEvent read_data_;
34 }; 31 };
35 32
36 // Main test fixture 33 // Main test fixture
37 class GamepadProviderTest : public testing::Test { 34 class GamepadProviderTest : public testing::Test {
38 public: 35 public:
39 GamepadProvider* CreateProvider(WebGamepads& test_data) { 36 GamepadProvider* CreateProvider(WebGamepads& test_data) {
40 #if defined(OS_MACOSX) 37 #if defined(OS_MACOSX)
41 base::SystemMonitor::AllocateSystemIOPorts(); 38 base::SystemMonitor::AllocateSystemIOPorts();
42 #endif 39 #endif
43 system_monitor_.reset(new base::SystemMonitor); 40 system_monitor_.reset(new base::SystemMonitor);
(...skipping 21 matching lines...) Expand all
65 test_data.items[0].buttonsLength = 1; 62 test_data.items[0].buttonsLength = 1;
66 test_data.items[0].axesLength = 2; 63 test_data.items[0].axesLength = 2;
67 test_data.items[0].buttons[0] = 1.f; 64 test_data.items[0].buttons[0] = 1.f;
68 test_data.items[0].axes[0] = -1.f; 65 test_data.items[0].axes[0] = -1.f;
69 test_data.items[0].axes[1] = .5f; 66 test_data.items[0].axes[1] = .5f;
70 67
71 GamepadProvider* provider = CreateProvider(test_data); 68 GamepadProvider* provider = CreateProvider(test_data);
72 69
73 main_message_loop_.RunAllPending(); 70 main_message_loop_.RunAllPending();
74 71
75 mock_data_fetcher_->WaitForDataRead();
76
77 // Renderer-side, pull data out of poll buffer. 72 // Renderer-side, pull data out of poll buffer.
78 base::SharedMemoryHandle handle = 73 base::SharedMemoryHandle handle =
79 provider->GetRendererSharedMemoryHandle(base::GetCurrentProcessHandle()); 74 provider->GetRendererSharedMemoryHandle(base::GetCurrentProcessHandle());
80 scoped_ptr<base::SharedMemory> shared_memory( 75 scoped_ptr<base::SharedMemory> shared_memory(
81 new base::SharedMemory(handle, true)); 76 new base::SharedMemory(handle, true));
82 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); 77 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer)));
83 void* mem = shared_memory->memory(); 78 void* mem = shared_memory->memory();
84 79
85 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); 80 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem);
86 // See gamepad_hardware_buffer.h for details on the read discipline.
87 WebGamepads output; 81 WebGamepads output;
88 82 output.length = 0;
89 base::subtle::Atomic32 version; 83 for (;;) {
90 do { 84 hwbuf->gamepads.ReadTo(&output);
91 version = hwbuf->sequence.ReadBegin(); 85 if (output.length == 1)
92 memcpy(&output, &hwbuf->buffer, sizeof(output)); 86 break;
93 } while (hwbuf->sequence.ReadRetry(version)); 87 base::PlatformThread::YieldCurrentThread();
94 88 }
95 EXPECT_EQ(1u, output.length); 89 EXPECT_EQ(1u, output.length);
96 EXPECT_EQ(1u, output.items[0].buttonsLength); 90 EXPECT_EQ(1u, output.items[0].buttonsLength);
97 EXPECT_EQ(1.f, output.items[0].buttons[0]); 91 EXPECT_EQ(1.f, output.items[0].buttons[0]);
98 EXPECT_EQ(2u, output.items[0].axesLength); 92 EXPECT_EQ(2u, output.items[0].axesLength);
99 EXPECT_EQ(-1.f, output.items[0].axes[0]); 93 EXPECT_EQ(-1.f, output.items[0].axes[0]);
100 EXPECT_EQ(0.5f, output.items[0].axes[1]); 94 EXPECT_EQ(0.5f, output.items[0].axes[1]);
101 } 95 }
102 96
103 } // namespace 97 } // namespace
104 98
105 } // namespace content 99 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698