Chromium Code Reviews| 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 protected: | |
| 29 linked_ptr<FakeVRDeviceProvider> provider_; | |
|
no sievers
2015/03/27 01:18:27
nit: can you just use scoped_ptr for clear ownersh
| |
| 30 scoped_ptr<VRService> service_; | |
| 31 | |
| 32 DISALLOW_COPY_AND_ASSIGN(VRServiceTest); | |
| 33 }; | |
| 34 | |
| 35 VRServiceTest::VRServiceTest() { | |
| 36 } | |
| 37 | |
| 38 VRServiceTest::~VRServiceTest() { | |
| 39 } | |
| 40 | |
| 41 void VRServiceTest::SetUp() { | |
| 42 provider_.reset(new FakeVRDeviceProvider()); | |
| 43 service_.reset(new VRService(provider_)); | |
| 44 } | |
| 45 | |
| 46 TEST_F(VRServiceTest, InitializationTest) { | |
| 47 EXPECT_FALSE(provider_->IsInitialized()); | |
| 48 | |
| 49 // Calling GetDevices should initialize the service if it hasn't been | |
| 50 // initialized yet or the providesr have been released. | |
| 51 // The VRService should initialize each of it's providers upon it's own | |
| 52 // initialization. | |
| 53 std::vector<blink::WebVRDevice> webvr_devices; | |
| 54 service_->GetVRDevices(&webvr_devices); | |
| 55 EXPECT_TRUE(provider_->IsInitialized()); | |
| 56 } | |
| 57 | |
| 58 TEST_F(VRServiceTest, ServiceConsumerTest) { | |
| 59 | |
| 60 { | |
| 61 VRServiceConsumer consumer; | |
| 62 | |
| 63 // Constructing a consumer should not cause the service to initialize it's | |
| 64 // providers. | |
| 65 EXPECT_FALSE(provider_->IsInitialized()); | |
| 66 // Constructing a consumer does not automatically add it to the service. | |
| 67 EXPECT_EQ(0u, ServiceConsumerCount()); | |
| 68 | |
| 69 consumer.GetServiceInstance(); | |
| 70 // Getting a service instance should not cause the service to initialize | |
| 71 // it's providers. | |
| 72 EXPECT_FALSE(provider_->IsInitialized()); | |
| 73 // Getting a service instance should add the consumer to the service. | |
| 74 EXPECT_EQ(1u, ServiceConsumerCount()); | |
| 75 | |
| 76 std::vector<blink::WebVRDevice> webvr_devices; | |
| 77 service_->GetVRDevices(&webvr_devices); | |
| 78 | |
| 79 { | |
| 80 VRServiceConsumer consumer2; | |
| 81 consumer2.GetServiceInstance(); | |
| 82 EXPECT_EQ(2u, ServiceConsumerCount()); | |
| 83 } | |
| 84 | |
| 85 // Consumers should remove themselves from the service when destroyed. | |
| 86 EXPECT_EQ(1u, ServiceConsumerCount()); | |
| 87 // Providers should not be released as long as there are one or more | |
| 88 // consumers attached to the service. | |
| 89 EXPECT_TRUE(provider_->IsInitialized()); | |
| 90 } | |
| 91 | |
| 92 // When all consumers have been removed the providers should be released. | |
| 93 EXPECT_EQ(0u, ServiceConsumerCount()); | |
| 94 EXPECT_FALSE(provider_->IsInitialized()); | |
| 95 } | |
| 96 | |
| 97 TEST_F(VRServiceTest, GetDevicesBasicTest) { | |
| 98 std::vector<blink::WebVRDevice> webvr_devices; | |
| 99 service_->GetVRDevices(&webvr_devices); | |
| 100 // Calling GetVRDevices should initialize the providers. | |
| 101 EXPECT_TRUE(provider_->IsInitialized()); | |
| 102 // Should successfully return zero devices when none are available. | |
| 103 EXPECT_EQ(0u, webvr_devices.size()); | |
| 104 | |
| 105 // GetDeviceByIndex should return NULL if an invalid index in queried. | |
| 106 VRDevice* queried_device = service_->GetDevice(1); | |
| 107 EXPECT_EQ(NULL, queried_device); | |
| 108 | |
| 109 scoped_ptr<FakeVRDevice> device1(new FakeVRDevice(provider_.get())); | |
| 110 provider_->AddDevice(device1.get()); | |
| 111 service_->GetVRDevices(&webvr_devices); | |
| 112 // Should have successfully returned one device. | |
| 113 EXPECT_EQ(1u, webvr_devices.size()); | |
| 114 // The WebVRDevice index should match the device id. | |
| 115 EXPECT_EQ(webvr_devices[0].index, device1->id()); | |
| 116 | |
| 117 scoped_ptr<FakeVRDevice> device2(new FakeVRDevice(provider_.get())); | |
| 118 provider_->AddDevice(device2.get()); | |
| 119 service_->GetVRDevices(&webvr_devices); | |
| 120 // Should have successfully returned two devices. | |
| 121 EXPECT_EQ(2u, webvr_devices.size()); | |
| 122 // NOTE: Returned WebVRDevices are not required to be in any particular order. | |
| 123 | |
| 124 // Querying the WebVRDevice index should return the correct device. | |
| 125 queried_device = service_->GetDevice(device1->id()); | |
| 126 EXPECT_EQ(device1.get(), queried_device); | |
| 127 queried_device = service_->GetDevice(device2->id()); | |
| 128 EXPECT_EQ(device2.get(), queried_device); | |
| 129 | |
| 130 provider_->RemoveDevice(device1.get()); | |
| 131 service_->GetVRDevices(&webvr_devices); | |
| 132 // Should have successfully returned one device. | |
| 133 EXPECT_EQ(1u, webvr_devices.size()); | |
| 134 // The WebVRDevice index should match the only remaining device id. | |
| 135 EXPECT_EQ(webvr_devices[0].index, device2->id()); | |
| 136 } | |
| 137 | |
| 138 } // namespace content | |
| OLD | NEW |