OLD | NEW |
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 "content/browser/gamepad/gamepad_provider.h" | 9 #include "content/browser/gamepad/gamepad_provider.h" |
10 #include "content/common/gamepad_messages.h" | 10 #include "content/common/gamepad_messages.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 protected: | 49 protected: |
50 GamepadProviderTest() { | 50 GamepadProviderTest() { |
51 } | 51 } |
52 | 52 |
53 MessageLoop main_message_loop_; | 53 MessageLoop main_message_loop_; |
54 scoped_ptr<base::SystemMonitor> system_monitor_; | 54 scoped_ptr<base::SystemMonitor> system_monitor_; |
55 MockDataFetcher* mock_data_fetcher_; | 55 MockDataFetcher* mock_data_fetcher_; |
56 scoped_refptr<GamepadProvider> provider_; | 56 scoped_refptr<GamepadProvider> provider_; |
57 }; | 57 }; |
58 | 58 |
59 TEST_F(GamepadProviderTest, BasicStartStop) { | |
60 WebGamepads test_data; | |
61 memset(&test_data, 0, sizeof(test_data)); | |
62 GamepadProvider* provider = CreateProvider(test_data); | |
63 provider->Start(); | |
64 provider->Stop(); | |
65 // Just ensure that there's no asserts on startup, shutdown, or destroy. | |
66 } | |
67 | |
68 TEST_F(GamepadProviderTest, PollingAccess) { | 59 TEST_F(GamepadProviderTest, PollingAccess) { |
69 WebGamepads test_data; | 60 WebGamepads test_data; |
70 test_data.length = 1; | 61 test_data.length = 1; |
71 test_data.items[0].connected = true; | 62 test_data.items[0].connected = true; |
72 test_data.items[0].timestamp = 0; | 63 test_data.items[0].timestamp = 0; |
73 test_data.items[0].buttonsLength = 1; | 64 test_data.items[0].buttonsLength = 1; |
74 test_data.items[0].axesLength = 2; | 65 test_data.items[0].axesLength = 2; |
75 test_data.items[0].buttons[0] = 1.f; | 66 test_data.items[0].buttons[0] = 1.f; |
76 test_data.items[0].axes[0] = -1.f; | 67 test_data.items[0].axes[0] = -1.f; |
77 test_data.items[0].axes[1] = .5f; | 68 test_data.items[0].axes[1] = .5f; |
78 | 69 |
79 GamepadProvider* provider = CreateProvider(test_data); | 70 GamepadProvider* provider = CreateProvider(test_data); |
80 provider->Start(); | |
81 | 71 |
82 main_message_loop_.RunAllPending(); | 72 main_message_loop_.RunAllPending(); |
83 | 73 |
84 mock_data_fetcher_->WaitForDataRead(); | 74 mock_data_fetcher_->WaitForDataRead(); |
85 | 75 |
86 // Renderer-side, pull data out of poll buffer. | 76 // Renderer-side, pull data out of poll buffer. |
87 base::SharedMemoryHandle handle = | 77 base::SharedMemoryHandle handle = |
88 provider->GetRendererSharedMemoryHandle(base::GetCurrentProcessHandle()); | 78 provider->GetRendererSharedMemoryHandle(base::GetCurrentProcessHandle()); |
89 scoped_ptr<base::SharedMemory> shared_memory( | 79 scoped_ptr<base::SharedMemory> shared_memory( |
90 new base::SharedMemory(handle, true)); | 80 new base::SharedMemory(handle, true)); |
91 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); | 81 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); |
92 void* mem = shared_memory->memory(); | 82 void* mem = shared_memory->memory(); |
93 | 83 |
94 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); | 84 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); |
95 // See gamepad_hardware_buffer.h for details on the read discipline. | 85 // See gamepad_hardware_buffer.h for details on the read discipline. |
96 WebGamepads output; | 86 WebGamepads output; |
97 | 87 |
98 base::subtle::Atomic32 version; | 88 base::subtle::Atomic32 version; |
99 do { | 89 do { |
100 version = hwbuf->sequence.ReadBegin(); | 90 version = hwbuf->sequence.ReadBegin(); |
101 memcpy(&output, &hwbuf->buffer, sizeof(output)); | 91 memcpy(&output, &hwbuf->buffer, sizeof(output)); |
102 } while (hwbuf->sequence.ReadRetry(version)); | 92 } while (hwbuf->sequence.ReadRetry(version)); |
103 | 93 |
104 EXPECT_EQ(1u, output.length); | 94 EXPECT_EQ(1u, output.length); |
105 EXPECT_EQ(1u, output.items[0].buttonsLength); | 95 EXPECT_EQ(1u, output.items[0].buttonsLength); |
106 EXPECT_EQ(1.f, output.items[0].buttons[0]); | 96 EXPECT_EQ(1.f, output.items[0].buttons[0]); |
107 EXPECT_EQ(2u, output.items[0].axesLength); | 97 EXPECT_EQ(2u, output.items[0].axesLength); |
108 EXPECT_EQ(-1.f, output.items[0].axes[0]); | 98 EXPECT_EQ(-1.f, output.items[0].axes[0]); |
109 EXPECT_EQ(0.5f, output.items[0].axes[1]); | 99 EXPECT_EQ(0.5f, output.items[0].axes[1]); |
110 | |
111 provider->Stop(); | |
112 } | 100 } |
113 | 101 |
114 } // namespace | 102 } // namespace |
115 | 103 |
116 } // namespace content | 104 } // namespace content |
OLD | NEW |