| Index: content/browser/vr/vr_service_unittest.cc
|
| diff --git a/content/browser/vr/vr_service_unittest.cc b/content/browser/vr/vr_service_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..98b6500d9c573600fcb7550b90020f3e774498f9
|
| --- /dev/null
|
| +++ b/content/browser/vr/vr_service_unittest.cc
|
| @@ -0,0 +1,136 @@
|
| +// Copyright 2015 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/linked_ptr.h"
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "content/browser/vr/test/fake_vr_device.h"
|
| +#include "content/browser/vr/test/fake_vr_device_provider.h"
|
| +#include "content/browser/vr/vr_device_provider.h"
|
| +#include "content/browser/vr/vr_service.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace content {
|
| +
|
| +using blink::WebVRDevice;
|
| +
|
| +class VRServiceTest : public testing::Test {
|
| + protected:
|
| + VRServiceTest();
|
| + ~VRServiceTest() override;
|
| +
|
| + void SetUp() override;
|
| +
|
| + size_t ServiceConsumerCount() {
|
| + return service_->consumers_.size();
|
| + }
|
| +
|
| + bool HasServiceInstance() {
|
| + return VRService::HasInstance();
|
| + }
|
| +
|
| + protected:
|
| + FakeVRDeviceProvider* provider_;
|
| + scoped_ptr<VRService> service_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(VRServiceTest);
|
| +};
|
| +
|
| +VRServiceTest::VRServiceTest() {
|
| +}
|
| +
|
| +VRServiceTest::~VRServiceTest() {
|
| +}
|
| +
|
| +void VRServiceTest::SetUp() {
|
| + scoped_ptr<FakeVRDeviceProvider> provider(new FakeVRDeviceProvider());
|
| + provider_ = provider.get();
|
| + service_.reset(new VRService(provider.Pass()));
|
| +}
|
| +
|
| +TEST_F(VRServiceTest, InitializationTest) {
|
| + EXPECT_FALSE(provider_->IsInitialized());
|
| +
|
| + // Calling GetDevices should initialize the service if it hasn't been
|
| + // initialized yet or the providesr have been released.
|
| + // The VRService should initialize each of it's providers upon it's own
|
| + // initialization.
|
| + std::vector<blink::WebVRDevice> webvr_devices;
|
| + service_->GetVRDevices(&webvr_devices);
|
| + EXPECT_TRUE(provider_->IsInitialized());
|
| +}
|
| +
|
| +TEST_F(VRServiceTest, ServiceConsumerTest) {
|
| +
|
| + {
|
| + VRServiceConsumer consumer;
|
| +
|
| + // Constructing a consumer should not cause the service to initialize it's
|
| + // providers.
|
| + EXPECT_FALSE(provider_->IsInitialized());
|
| + // Constructing a consumer does not automatically add it to the service.
|
| + EXPECT_EQ(0u, ServiceConsumerCount());
|
| +
|
| + consumer.GetServiceInstance();
|
| + // Getting a service instance should not cause the service to initialize
|
| + // it's providers.
|
| + EXPECT_FALSE(provider_->IsInitialized());
|
| + // Getting a service instance should add the consumer to the service.
|
| + EXPECT_EQ(1u, ServiceConsumerCount());
|
| +
|
| + std::vector<blink::WebVRDevice> webvr_devices;
|
| + service_->GetVRDevices(&webvr_devices);
|
| +
|
| + {
|
| + VRServiceConsumer consumer2;
|
| + consumer2.GetServiceInstance();
|
| + EXPECT_EQ(2u, ServiceConsumerCount());
|
| + }
|
| +
|
| + // Consumers should remove themselves from the service when destroyed.
|
| + EXPECT_EQ(1u, ServiceConsumerCount());
|
| + }
|
| +}
|
| +
|
| +TEST_F(VRServiceTest, GetDevicesBasicTest) {
|
| + std::vector<blink::WebVRDevice> webvr_devices;
|
| + service_->GetVRDevices(&webvr_devices);
|
| + // Calling GetVRDevices should initialize the providers.
|
| + EXPECT_TRUE(provider_->IsInitialized());
|
| + // Should successfully return zero devices when none are available.
|
| + EXPECT_EQ(0u, webvr_devices.size());
|
| +
|
| + // GetDeviceByIndex should return nullptr if an invalid index in queried.
|
| + VRDevice* queried_device = service_->GetDevice(1);
|
| + EXPECT_EQ(nullptr, queried_device);
|
| +
|
| + scoped_ptr<FakeVRDevice> device1(new FakeVRDevice(provider_));
|
| + provider_->AddDevice(device1.get());
|
| + service_->GetVRDevices(&webvr_devices);
|
| + // Should have successfully returned one device.
|
| + EXPECT_EQ(1u, webvr_devices.size());
|
| + // The WebVRDevice index should match the device id.
|
| + EXPECT_EQ(webvr_devices[0].index, device1->id());
|
| +
|
| + scoped_ptr<FakeVRDevice> device2(new FakeVRDevice(provider_));
|
| + provider_->AddDevice(device2.get());
|
| + service_->GetVRDevices(&webvr_devices);
|
| + // Should have successfully returned two devices.
|
| + EXPECT_EQ(2u, webvr_devices.size());
|
| + // NOTE: Returned WebVRDevices are not required to be in any particular order.
|
| +
|
| + // Querying the WebVRDevice index should return the correct device.
|
| + queried_device = service_->GetDevice(device1->id());
|
| + EXPECT_EQ(device1.get(), queried_device);
|
| + queried_device = service_->GetDevice(device2->id());
|
| + EXPECT_EQ(device2.get(), queried_device);
|
| +
|
| + provider_->RemoveDevice(device1.get());
|
| + service_->GetVRDevices(&webvr_devices);
|
| + // Should have successfully returned one device.
|
| + EXPECT_EQ(1u, webvr_devices.size());
|
| + // The WebVRDevice index should match the only remaining device id.
|
| + EXPECT_EQ(webvr_devices[0].index, device2->id());
|
| +}
|
| +
|
| +} // namespace content
|
|
|