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

Side by Side 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, 8 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 unified diff | Download patch
OLDNEW
(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/scoped_ptr.h"
6 #include "content/browser/vr/test/fake_vr_device.h"
7 #include "content/browser/vr/test/fake_vr_device_provider.h"
8 #include "content/browser/vr/vr_device_provider.h"
9 #include "content/browser/vr/vr_service.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace content {
13
14 using blink::WebVRDevice;
15
16 class VRServiceTest : public testing::Test {
17 protected:
18 VRServiceTest();
19 ~VRServiceTest() override;
20
21 void SetUp() override;
22
23 void InitializeProviders() {
24 service_->InitializeProviders();
25 }
26
27 void ReleaseProviderDevices() {
28 service_->ReleaseProviderDevices();
29 }
30
31 int ServiceConsumerCount() {
32 return service_->consumers_.size();
33 }
34
35 protected:
36 FakeVRDeviceProvider* provider_;
37 VRService* service_;
38
39 DISALLOW_COPY_AND_ASSIGN(VRServiceTest);
40 };
41
42 VRServiceTest::VRServiceTest() {
43
44 }
45
46 VRServiceTest::~VRServiceTest() {
47 delete service_;
no sievers 2015/03/26 19:55:15 Can you make |service_| scoped_ptr then?
48 }
49
50 void VRServiceTest::SetUp() {
51 provider_ = new FakeVRDeviceProvider();
no sievers 2015/03/26 19:55:15 Who deletes the provider?
52 service_ = new VRService(provider_);
53 }
54
55 TEST_F(VRServiceTest, InitializationTest) {
56 EXPECT_FALSE(provider_->IsInitialized());
57
58 // The VRService should initialize each of it's providers upon initialization.
59 InitializeProviders();
60 EXPECT_TRUE(provider_->IsInitialized());
61
62 // Similarly, upon ReleaseProviderDevices it should call ReleaseDevices on
63 // each of it's providers.
64 ReleaseProviderDevices();
65 EXPECT_FALSE(provider_->IsInitialized());
66
67 // Calling GetDevices should initialize the service if it hasn't been
68 // initialized yet or the providesr have been released.
69 std::vector<blink::WebVRDevice> webvr_devices;
70 service_->GetVRDevices(&webvr_devices);
71 EXPECT_TRUE(provider_->IsInitialized());
72 }
73
74 TEST_F(VRServiceTest, ServiceConsumerTest) {
75
76 {
77 VRServiceConsumer consumer(service_);
78
79 // Constructing a consumer should not cause the service to initialize it's
80 // providers.
81 EXPECT_FALSE(provider_->IsInitialized());
82 // Constructing a consumer does not automatically add it to the service.
83 EXPECT_EQ(0, ServiceConsumerCount());
84
85 consumer.GetServiceInstance();
86 // Getting a service instance should not cause the service to initialize
87 // it's providers.
88 EXPECT_FALSE(provider_->IsInitialized());
89 // Getting a service instance should add the consumer to the service.
90 EXPECT_EQ(1, ServiceConsumerCount());
91
92 InitializeProviders();
93
94 {
95 VRServiceConsumer consumer2(service_);
96 consumer2.GetServiceInstance();
97 EXPECT_EQ(2, ServiceConsumerCount());
98 }
99
100 // Consumers should remove themselves from the service when destroyed.
101 EXPECT_EQ(1, ServiceConsumerCount());
102 // Providers should not be released as long as there are one or more
103 // consumers attached to the service.
104 EXPECT_TRUE(provider_->IsInitialized());
105 }
106
107 // When all consumers have been removed the providers should be released.
108 EXPECT_EQ(0, ServiceConsumerCount());
109 EXPECT_FALSE(provider_->IsInitialized());
110 }
111
112 TEST_F(VRServiceTest, GetDevicesBasicTest) {
113 std::vector<blink::WebVRDevice> webvr_devices;
114 service_->GetVRDevices(&webvr_devices);
115 // Calling GetVRDevices should initialize the providers.
116 EXPECT_TRUE(provider_->IsInitialized());
117 // Should successfully return zero devices when none are available.
118 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
119
120 // GetDeviceByIndex should return NULL if an invalid index in queried.
121 VRDevice* queried_device = service_->GetDevice(1);
122 EXPECT_EQ(NULL, queried_device);
123
124 scoped_ptr<FakeVRDevice> device1(new FakeVRDevice(provider_));
125 provider_->AddDevice(device1.get());
126 service_->GetVRDevices(&webvr_devices);
127 // Should have successfully returned one device.
128 EXPECT_EQ((size_t)1, webvr_devices.size());
129 // The WebVRDevice index should match the device id.
130 EXPECT_EQ(webvr_devices[0].index, device1->id());
131
132 scoped_ptr<FakeVRDevice> device2(new FakeVRDevice(provider_));
133 provider_->AddDevice(device2.get());
134 service_->GetVRDevices(&webvr_devices);
135 // Should have successfully returned two devices.
136 EXPECT_EQ((size_t)2, webvr_devices.size());
137 // NOTE: Returned WebVRDevices are not required to be in any particular order.
138
139 // Querying the WebVRDevice index should return the correct device.
140 queried_device = service_->GetDevice(device1->id());
141 EXPECT_EQ(device1.get(), queried_device);
142 queried_device = service_->GetDevice(device2->id());
143 EXPECT_EQ(device2.get(), queried_device);
144
145 provider_->RemoveDevice(device1.get());
146 service_->GetVRDevices(&webvr_devices);
147 // Should have successfully returned one device.
148 EXPECT_EQ((size_t)1, webvr_devices.size());
149 // The WebVRDevice index should match the only remaining device id.
150 EXPECT_EQ(webvr_devices[0].index, device2->id());
151 }
152
153 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698