summaryrefslogtreecommitdiffstats
path: root/content/browser/gamepad/gamepad_provider_unittest.cc
blob: 69cee11993052d2210abdbb68490ea29a4934d73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) 2012 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 "base/memory/scoped_ptr.h"
#include "base/process_util.h"
#include "base/synchronization/waitable_event.h"
#include "base/system_monitor/system_monitor.h"
#include "base/threading/platform_thread.h"
#include "content/browser/gamepad/data_fetcher.h"
#include "content/browser/gamepad/gamepad_provider.h"
#include "content/common/gamepad_hardware_buffer.h"
#include "content/common/gamepad_messages.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGamepads.h"

namespace content {

namespace {

using WebKit::WebGamepads;

class MockDataFetcher : public GamepadDataFetcher {
 public:
  explicit MockDataFetcher(const WebGamepads& test_data)
      : test_data_(test_data) {
  }
  virtual void GetGamepadData(WebGamepads* pads,
                              bool devices_changed_hint) OVERRIDE {
    *pads = test_data_;
  }

  WebGamepads test_data_;
};

// Main test fixture
class GamepadProviderTest : public testing::Test {
 public:
  GamepadProvider* CreateProvider(const WebGamepads& test_data) {
#if defined(OS_MACOSX)
    base::SystemMonitor::AllocateSystemIOPorts();
#endif
    system_monitor_.reset(new base::SystemMonitor);
    mock_data_fetcher_ = new MockDataFetcher(test_data);
    provider_.reset(new GamepadProvider);
    provider_->SetDataFetcher(mock_data_fetcher_);
    return provider_.get();
  }

 protected:
  GamepadProviderTest() {
  }

  MessageLoop main_message_loop_;
  scoped_ptr<base::SystemMonitor> system_monitor_;
  MockDataFetcher* mock_data_fetcher_;
  scoped_ptr<GamepadProvider> provider_;
};

// Crashes. http://crbug.com/106163
TEST_F(GamepadProviderTest, DISABLED_PollingAccess) {
  WebGamepads test_data;
  test_data.length = 1;
  test_data.items[0].connected = true;
  test_data.items[0].timestamp = 0;
  test_data.items[0].buttonsLength = 1;
  test_data.items[0].axesLength = 2;
  test_data.items[0].buttons[0] = 1.f;
  test_data.items[0].axes[0] = -1.f;
  test_data.items[0].axes[1] = .5f;

  GamepadProvider* provider = CreateProvider(test_data);

  main_message_loop_.RunAllPending();

  // Renderer-side, pull data out of poll buffer.
  base::SharedMemoryHandle handle =
      provider->GetRendererSharedMemoryHandle(base::GetCurrentProcessHandle());
  scoped_ptr<base::SharedMemory> shared_memory(
      new base::SharedMemory(handle, true));
  EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer)));
  void* mem = shared_memory->memory();

  GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem);
  WebGamepads output;
  output.length = 0;
  for (;;) {
    hwbuf->gamepads.ReadTo(&output);
    if (output.length == 1)
      break;
    base::PlatformThread::YieldCurrentThread();
  }
  EXPECT_EQ(1u, output.length);
  EXPECT_EQ(1u, output.items[0].buttonsLength);
  EXPECT_EQ(1.f, output.items[0].buttons[0]);
  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]);
}

}  // namespace

}  // namespace content