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

Unified Diff: device/gamepad/gamepad_provider_unittest.cc

Issue 2563483006: Move gamepad_service out of content/ and into device/ (Closed)
Patch Set: Move gamepad_service out of content/ and into device/ Created 4 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 side-by-side diff with in-line comments
Download patch
Index: device/gamepad/gamepad_provider_unittest.cc
diff --git a/device/gamepad/gamepad_provider_unittest.cc b/device/gamepad/gamepad_provider_unittest.cc
index 180a804cd5ed77f21e4c23f5802e37f7f6447168..355eaf81324e260745f38a7ff5c120a78fa6352f 100644
--- a/device/gamepad/gamepad_provider_unittest.cc
+++ b/device/gamepad/gamepad_provider_unittest.cc
@@ -45,7 +45,6 @@ class GamepadProviderTest : public testing::Test, public GamepadTestHelper {
GamepadProvider* CreateProvider(const WebGamepads& test_data) {
mock_data_fetcher_ = new MockGamepadDataFetcher(test_data);
provider_.reset(new GamepadProvider(
- std::unique_ptr<GamepadSharedBuffer>(new MockGamepadSharedBuffer()),
nullptr, std::unique_ptr<GamepadDataFetcher>(mock_data_fetcher_)));
return provider_.get();
}
@@ -70,6 +69,7 @@ class GamepadProviderTest : public testing::Test, public GamepadTestHelper {
#endif
TEST_F(GamepadProviderTest, MAYBE_PollingAccess) {
WebGamepads test_data;
+ memset(&test_data, 0, sizeof(WebGamepads));
test_data.length = 1;
test_data.items[0].connected = true;
test_data.items[0].timestamp = 0;
@@ -93,18 +93,25 @@ TEST_F(GamepadProviderTest, MAYBE_PollingAccess) {
base::GetCurrentProcessHandle());
std::unique_ptr<base::SharedMemory> shared_memory(
new base::SharedMemory(handle, true));
- EXPECT_TRUE(shared_memory->Map(sizeof(WebGamepads)));
- void* mem = shared_memory->memory();
-
- WebGamepads* output = static_cast<WebGamepads*>(mem);
-
- EXPECT_EQ(1u, output->length);
- EXPECT_EQ(1u, output->items[0].buttonsLength);
- EXPECT_EQ(1.f, output->items[0].buttons[0].value);
- EXPECT_EQ(true, output->items[0].buttons[0].pressed);
- EXPECT_EQ(2u, output->items[0].axesLength);
- EXPECT_EQ(-1.f, output->items[0].axes[0]);
- EXPECT_EQ(0.5f, output->items[0].axes[1]);
+ EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer)));
+
+ WebGamepads output;
+ memset(&output, 0, sizeof(WebGamepads));
+ GamepadHardwareBuffer* buffer =
+ static_cast<GamepadHardwareBuffer*>(shared_memory->memory());
+ base::subtle::Atomic32 version;
+ do {
ke.he 2016/12/09 07:44:55 Before this patch, it is the "MockGamepadSharedBuf
blundell 2016/12/09 13:35:22 Seems like it would be worthwhile to make a helper
ke.he 2016/12/09 15:15:22 Done.
+ version = buffer->seqlock.ReadBegin();
+ memcpy(&output, &buffer->data, sizeof(WebGamepads));
+ } while (buffer->seqlock.ReadRetry(version));
+
+ EXPECT_EQ(1u, output.length);
+ EXPECT_EQ(1u, output.items[0].buttonsLength);
+ EXPECT_EQ(1.f, output.items[0].buttons[0].value);
+ EXPECT_EQ(true, output.items[0].buttons[0].pressed);
+ EXPECT_EQ(2u, output.items[0].axesLength);
+ EXPECT_EQ(-1.f, output.items[0].axes[0]);
+ EXPECT_EQ(0.5f, output.items[0].axes[1]);
}
// Tests that waiting for a user gesture works properly.
@@ -191,43 +198,64 @@ TEST_F(GamepadProviderTest, MAYBE_Sanitization) {
base::GetCurrentProcessHandle());
std::unique_ptr<base::SharedMemory> shared_memory(
new base::SharedMemory(handle, true));
- EXPECT_TRUE(shared_memory->Map(sizeof(WebGamepads)));
- void* mem = shared_memory->memory();
-
- WebGamepads* output = static_cast<WebGamepads*>(mem);
+ EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer)));
+
+ WebGamepads output;
+ memset(&output, 0, sizeof(WebGamepads));
+ GamepadHardwareBuffer* buffer =
+ static_cast<GamepadHardwareBuffer*>(shared_memory->memory());
+ base::subtle::Atomic32 version;
+ do {
+ version = buffer->seqlock.ReadBegin();
+ memcpy(&output, &buffer->data, sizeof(WebGamepads));
+ } while (buffer->seqlock.ReadRetry(version));
// Initial data should all be zeroed out due to sanitization, even though the
// gamepad reported input
- EXPECT_EQ(1u, output->length);
- EXPECT_EQ(1u, output->items[0].buttonsLength);
- EXPECT_EQ(0.f, output->items[0].buttons[0].value);
- EXPECT_FALSE(output->items[0].buttons[0].pressed);
- EXPECT_EQ(1u, output->items[0].axesLength);
- EXPECT_EQ(0.f, output->items[0].axes[0]);
+ EXPECT_EQ(1u, output.length);
+ EXPECT_EQ(1u, output.items[0].buttonsLength);
+ EXPECT_EQ(0.f, output.items[0].buttons[0].value);
+ EXPECT_FALSE(output.items[0].buttons[0].pressed);
+ EXPECT_EQ(1u, output.items[0].axesLength);
+ EXPECT_EQ(0.f, output.items[0].axes[0]);
// Zero out the inputs
mock_data_fetcher_->SetTestData(zero_data);
mock_data_fetcher_->WaitForDataReadAndCallbacksIssued();
+ // Read updated data from shared memory
+ memset(&output, 0, sizeof(WebGamepads));
+ do {
+ version = buffer->seqlock.ReadBegin();
+ memcpy(&output, &buffer->data, sizeof(WebGamepads));
+ } while (buffer->seqlock.ReadRetry(version));
+
// Should still read zero, which is now an accurate reflection of the data
- EXPECT_EQ(1u, output->length);
- EXPECT_EQ(1u, output->items[0].buttonsLength);
- EXPECT_EQ(0.f, output->items[0].buttons[0].value);
- EXPECT_FALSE(output->items[0].buttons[0].pressed);
- EXPECT_EQ(1u, output->items[0].axesLength);
- EXPECT_EQ(0.f, output->items[0].axes[0]);
+ EXPECT_EQ(1u, output.length);
+ EXPECT_EQ(1u, output.items[0].buttonsLength);
+ EXPECT_EQ(0.f, output.items[0].buttons[0].value);
+ EXPECT_FALSE(output.items[0].buttons[0].pressed);
+ EXPECT_EQ(1u, output.items[0].axesLength);
+ EXPECT_EQ(0.f, output.items[0].axes[0]);
// Re-set the active inputs
mock_data_fetcher_->SetTestData(active_data);
mock_data_fetcher_->WaitForDataReadAndCallbacksIssued();
+ // Read updated data from shared memory
+ memset(&output, 0, sizeof(WebGamepads));
+ do {
+ version = buffer->seqlock.ReadBegin();
+ memcpy(&output, &buffer->data, sizeof(WebGamepads));
+ } while (buffer->seqlock.ReadRetry(version));
+
// Should now accurately reflect the reported data.
- EXPECT_EQ(1u, output->length);
- EXPECT_EQ(1u, output->items[0].buttonsLength);
- EXPECT_EQ(1.f, output->items[0].buttons[0].value);
- EXPECT_TRUE(output->items[0].buttons[0].pressed);
- EXPECT_EQ(1u, output->items[0].axesLength);
- EXPECT_EQ(-1.f, output->items[0].axes[0]);
+ EXPECT_EQ(1u, output.length);
+ EXPECT_EQ(1u, output.items[0].buttonsLength);
+ EXPECT_EQ(1.f, output.items[0].buttons[0].value);
+ EXPECT_TRUE(output.items[0].buttons[0].pressed);
+ EXPECT_EQ(1u, output.items[0].axesLength);
+ EXPECT_EQ(-1.f, output.items[0].axes[0]);
}
} // namespace

Powered by Google App Engine
This is Rietveld 408576698