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

Unified Diff: content/browser/vr/vr_service_unittest.cc

Issue 829803003: Adding Chrome-side WebVR interface (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed the rest of sievers@ input Created 5 years, 9 months 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: 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..59a5dcdd00df31355e3ed10ac50d9193cd672975
--- /dev/null
+++ b/content/browser/vr/vr_service_unittest.cc
@@ -0,0 +1,153 @@
+// 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/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;
+
+ void InitializeProviders() {
+ service_->InitializeProviders();
+ }
+
+ void ReleaseProviderDevices() {
+ service_->ReleaseProviderDevices();
+ }
+
+ int ServiceConsumerCount() {
+ return service_->consumers_.size();
+ }
+
+ protected:
+ FakeVRDeviceProvider* provider_;
+ VRService* service_;
+
+ DISALLOW_COPY_AND_ASSIGN(VRServiceTest);
+};
+
+VRServiceTest::VRServiceTest() {
+
+}
+
+VRServiceTest::~VRServiceTest() {
+ delete service_;
no sievers 2015/03/26 19:55:15 Can you make |service_| scoped_ptr then?
+}
+
+void VRServiceTest::SetUp() {
+ provider_ = new FakeVRDeviceProvider();
no sievers 2015/03/26 19:55:15 Who deletes the provider?
+ service_ = new VRService(provider_);
+}
+
+TEST_F(VRServiceTest, InitializationTest) {
+ EXPECT_FALSE(provider_->IsInitialized());
+
+ // The VRService should initialize each of it's providers upon initialization.
+ InitializeProviders();
+ EXPECT_TRUE(provider_->IsInitialized());
+
+ // Similarly, upon ReleaseProviderDevices it should call ReleaseDevices on
+ // each of it's providers.
+ ReleaseProviderDevices();
+ EXPECT_FALSE(provider_->IsInitialized());
+
+ // Calling GetDevices should initialize the service if it hasn't been
+ // initialized yet or the providesr have been released.
+ std::vector<blink::WebVRDevice> webvr_devices;
+ service_->GetVRDevices(&webvr_devices);
+ EXPECT_TRUE(provider_->IsInitialized());
+}
+
+TEST_F(VRServiceTest, ServiceConsumerTest) {
+
+ {
+ VRServiceConsumer consumer(service_);
+
+ // 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(0, 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(1, ServiceConsumerCount());
+
+ InitializeProviders();
+
+ {
+ VRServiceConsumer consumer2(service_);
+ consumer2.GetServiceInstance();
+ EXPECT_EQ(2, ServiceConsumerCount());
+ }
+
+ // Consumers should remove themselves from the service when destroyed.
+ EXPECT_EQ(1, ServiceConsumerCount());
+ // Providers should not be released as long as there are one or more
+ // consumers attached to the service.
+ EXPECT_TRUE(provider_->IsInitialized());
+ }
+
+ // When all consumers have been removed the providers should be released.
+ EXPECT_EQ(0, ServiceConsumerCount());
+ EXPECT_FALSE(provider_->IsInitialized());
+}
+
+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((size_t)0, webvr_devices.size());
no sievers 2015/03/26 19:55:15 nit: 0u (or static_cast, style guide) here and bel
+
+ // GetDeviceByIndex should return NULL if an invalid index in queried.
+ VRDevice* queried_device = service_->GetDevice(1);
+ EXPECT_EQ(NULL, 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((size_t)1, 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((size_t)2, 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((size_t)1, webvr_devices.size());
+ // The WebVRDevice index should match the only remaining device id.
+ EXPECT_EQ(webvr_devices[0].index, device2->id());
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698