OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/memory/linked_ptr.h" |
| 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "content/browser/vr/test/fake_vr_device.h" |
| 8 #include "content/browser/vr/test/fake_vr_device_provider.h" |
| 9 #include "content/browser/vr/vr_device_provider.h" |
| 10 #include "content/browser/vr/vr_service.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 using blink::WebVRDevice; |
| 16 |
| 17 class VRServiceTest : public testing::Test { |
| 18 protected: |
| 19 VRServiceTest(); |
| 20 ~VRServiceTest() override; |
| 21 |
| 22 void SetUp() override; |
| 23 |
| 24 size_t ServiceConsumerCount() { |
| 25 return service_->consumers_.size(); |
| 26 } |
| 27 |
| 28 bool HasServiceInstance() { |
| 29 return VRService::HasInstance(); |
| 30 } |
| 31 |
| 32 protected: |
| 33 FakeVRDeviceProvider* provider_; |
| 34 scoped_ptr<VRService> service_; |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(VRServiceTest); |
| 37 }; |
| 38 |
| 39 VRServiceTest::VRServiceTest() { |
| 40 } |
| 41 |
| 42 VRServiceTest::~VRServiceTest() { |
| 43 } |
| 44 |
| 45 void VRServiceTest::SetUp() { |
| 46 scoped_ptr<FakeVRDeviceProvider> provider(new FakeVRDeviceProvider()); |
| 47 provider_ = provider.get(); |
| 48 service_.reset(new VRService(provider.Pass())); |
| 49 } |
| 50 |
| 51 TEST_F(VRServiceTest, InitializationTest) { |
| 52 EXPECT_FALSE(provider_->IsInitialized()); |
| 53 |
| 54 // Calling GetDevices should initialize the service if it hasn't been |
| 55 // initialized yet or the providesr have been released. |
| 56 // The VRService should initialize each of it's providers upon it's own |
| 57 // initialization. |
| 58 std::vector<blink::WebVRDevice> webvr_devices; |
| 59 service_->GetVRDevices(&webvr_devices); |
| 60 EXPECT_TRUE(provider_->IsInitialized()); |
| 61 } |
| 62 |
| 63 TEST_F(VRServiceTest, ServiceConsumerTest) { |
| 64 |
| 65 { |
| 66 VRServiceConsumer consumer; |
| 67 |
| 68 // Constructing a consumer should not cause the service to initialize it's |
| 69 // providers. |
| 70 EXPECT_FALSE(provider_->IsInitialized()); |
| 71 // Constructing a consumer does not automatically add it to the service. |
| 72 EXPECT_EQ(0u, ServiceConsumerCount()); |
| 73 |
| 74 consumer.GetServiceInstance(); |
| 75 // Getting a service instance should not cause the service to initialize |
| 76 // it's providers. |
| 77 EXPECT_FALSE(provider_->IsInitialized()); |
| 78 // Getting a service instance should add the consumer to the service. |
| 79 EXPECT_EQ(1u, ServiceConsumerCount()); |
| 80 |
| 81 std::vector<blink::WebVRDevice> webvr_devices; |
| 82 service_->GetVRDevices(&webvr_devices); |
| 83 |
| 84 { |
| 85 VRServiceConsumer consumer2; |
| 86 consumer2.GetServiceInstance(); |
| 87 EXPECT_EQ(2u, ServiceConsumerCount()); |
| 88 } |
| 89 |
| 90 // Consumers should remove themselves from the service when destroyed. |
| 91 EXPECT_EQ(1u, ServiceConsumerCount()); |
| 92 } |
| 93 } |
| 94 |
| 95 TEST_F(VRServiceTest, GetDevicesBasicTest) { |
| 96 std::vector<blink::WebVRDevice> webvr_devices; |
| 97 service_->GetVRDevices(&webvr_devices); |
| 98 // Calling GetVRDevices should initialize the providers. |
| 99 EXPECT_TRUE(provider_->IsInitialized()); |
| 100 // Should successfully return zero devices when none are available. |
| 101 EXPECT_EQ(0u, webvr_devices.size()); |
| 102 |
| 103 // GetDeviceByIndex should return NULL if an invalid index in queried. |
| 104 VRDevice* queried_device = service_->GetDevice(1); |
| 105 EXPECT_EQ(NULL, queried_device); |
| 106 |
| 107 scoped_ptr<FakeVRDevice> device1(new FakeVRDevice(provider_)); |
| 108 provider_->AddDevice(device1.get()); |
| 109 service_->GetVRDevices(&webvr_devices); |
| 110 // Should have successfully returned one device. |
| 111 EXPECT_EQ(1u, webvr_devices.size()); |
| 112 // The WebVRDevice index should match the device id. |
| 113 EXPECT_EQ(webvr_devices[0].index, device1->id()); |
| 114 |
| 115 scoped_ptr<FakeVRDevice> device2(new FakeVRDevice(provider_)); |
| 116 provider_->AddDevice(device2.get()); |
| 117 service_->GetVRDevices(&webvr_devices); |
| 118 // Should have successfully returned two devices. |
| 119 EXPECT_EQ(2u, webvr_devices.size()); |
| 120 // NOTE: Returned WebVRDevices are not required to be in any particular order. |
| 121 |
| 122 // Querying the WebVRDevice index should return the correct device. |
| 123 queried_device = service_->GetDevice(device1->id()); |
| 124 EXPECT_EQ(device1.get(), queried_device); |
| 125 queried_device = service_->GetDevice(device2->id()); |
| 126 EXPECT_EQ(device2.get(), queried_device); |
| 127 |
| 128 provider_->RemoveDevice(device1.get()); |
| 129 service_->GetVRDevices(&webvr_devices); |
| 130 // Should have successfully returned one device. |
| 131 EXPECT_EQ(1u, webvr_devices.size()); |
| 132 // The WebVRDevice index should match the only remaining device id. |
| 133 EXPECT_EQ(webvr_devices[0].index, device2->id()); |
| 134 } |
| 135 |
| 136 } // namespace content |
OLD | NEW |