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

Side by Side Diff: device/vr/vr_service_impl_unittest.cc

Issue 2329893002: Isolate a presenting VR device from pages other than the one presenting. (Closed)
Patch Set: Unit tests Created 4 years, 3 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
« no previous file with comments | « device/vr/vr_service_impl.cc ('k') | third_party/WebKit/Source/modules/vr/VRController.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/vr/vr_service_impl.h" 5 #include "device/vr/vr_service_impl.h"
6 6
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "device/vr/test/fake_vr_device.h" 9 #include "device/vr/test/fake_vr_device.h"
10 #include "device/vr/test/fake_vr_device_provider.h" 10 #include "device/vr/test/fake_vr_device_provider.h"
11 #include "device/vr/vr_device_manager.h" 11 #include "device/vr/vr_device_manager.h"
12 #include "device/vr/vr_service.mojom.h" 12 #include "device/vr/vr_service.mojom.h"
13 #include "testing/gmock/include/gmock/gmock.h" 13 #include "testing/gmock/include/gmock/gmock.h"
14 14
15 using ::testing::_; 15 using ::testing::_;
16 using ::testing::Mock; 16 using ::testing::Mock;
17 17
18 namespace device { 18 namespace device {
19 19
20 class MockVRServiceClient : public VRServiceClient { 20 class MockVRServiceClient : public VRServiceClient {
21 public: 21 public:
22 MOCK_METHOD1(OnDisplayChanged, void(const VRDisplay& display)); 22 MOCK_METHOD1(OnDisplayChanged, void(const VRDisplay& display));
23 void OnDisplayChanged(VRDisplayPtr display) override { 23 void OnDisplayChanged(VRDisplayPtr display) override {
24 OnDisplayChanged(*display); 24 OnDisplayChanged(*display);
25 last_display_ = std::move(display); 25 last_display_ = std::move(display);
26 } 26 }
27 27
28 MOCK_METHOD1(OnExitPresent, void(uint32_t index));
29
28 const VRDisplayPtr& LastDisplay() { return last_display_; } 30 const VRDisplayPtr& LastDisplay() { return last_display_; }
29 31
30 private: 32 private:
31 VRDisplayPtr last_display_; 33 VRDisplayPtr last_display_;
32 }; 34 };
33 35
34 class VRServiceTestBinding { 36 class VRServiceTestBinding {
35 public: 37 public:
36 VRServiceTestBinding() { 38 VRServiceTestBinding() {
37 auto request = mojo::GetProxy(&service_ptr_); 39 auto request = mojo::GetProxy(&service_ptr_);
38 service_impl_.reset(new VRServiceImpl()); 40 service_impl_.reset(new VRServiceImpl());
39 service_impl_->Bind(std::move(request)); 41 service_impl_->Bind(std::move(request));
40 42
41 VRServiceClientPtr client_ptr; 43 VRServiceClientPtr client_ptr;
42 client_binding_.reset(new mojo::Binding<VRServiceClient>( 44 client_binding_.reset(new mojo::Binding<VRServiceClient>(
43 &mock_client_, mojo::GetProxy(&client_ptr))); 45 &mock_client_, mojo::GetProxy(&client_ptr)));
44 service_impl_->SetClient(std::move(client_ptr)); 46 service_impl_->SetClient(std::move(client_ptr));
45 } 47 }
46 48
47 void Close() { 49 void Close() {
48 service_ptr_.reset(); 50 service_ptr_.reset();
49 service_impl_.reset(); 51 service_impl_.reset();
50 } 52 }
51 53
52 MockVRServiceClient& client() { return mock_client_; } 54 MockVRServiceClient& client() { return mock_client_; }
55 VRServiceImpl* service() { return service_impl_.get(); }
53 56
54 private: 57 private:
55 std::unique_ptr<VRServiceImpl> service_impl_; 58 std::unique_ptr<VRServiceImpl> service_impl_;
56 mojo::InterfacePtr<VRService> service_ptr_; 59 mojo::InterfacePtr<VRService> service_ptr_;
57 60
58 MockVRServiceClient mock_client_; 61 MockVRServiceClient mock_client_;
59 std::unique_ptr<mojo::Binding<VRServiceClient>> client_binding_; 62 std::unique_ptr<mojo::Binding<VRServiceClient>> client_binding_;
60 63
61 DISALLOW_COPY_AND_ASSIGN(VRServiceTestBinding); 64 DISALLOW_COPY_AND_ASSIGN(VRServiceTestBinding);
62 }; 65 };
(...skipping 11 matching lines...) Expand all
74 } 77 }
75 78
76 void TearDown() override { base::RunLoop().RunUntilIdle(); } 79 void TearDown() override { base::RunLoop().RunUntilIdle(); }
77 80
78 std::unique_ptr<VRServiceTestBinding> BindService() { 81 std::unique_ptr<VRServiceTestBinding> BindService() {
79 return std::unique_ptr<VRServiceTestBinding>(new VRServiceTestBinding()); 82 return std::unique_ptr<VRServiceTestBinding>(new VRServiceTestBinding());
80 } 83 }
81 84
82 size_t ServiceCount() { return device_manager_->services_.size(); } 85 size_t ServiceCount() { return device_manager_->services_.size(); }
83 86
87 bool presenting() { return !!device_manager_->presenting_service_; }
88
84 base::MessageLoop message_loop_; 89 base::MessageLoop message_loop_;
85 FakeVRDeviceProvider* provider_; 90 FakeVRDeviceProvider* provider_;
86 std::unique_ptr<VRDeviceManager> device_manager_; 91 std::unique_ptr<VRDeviceManager> device_manager_;
87 92
88 DISALLOW_COPY_AND_ASSIGN(VRServiceImplTest); 93 DISALLOW_COPY_AND_ASSIGN(VRServiceImplTest);
89 }; 94 };
90 95
91 // Ensure that services are registered with the device manager as they are 96 // Ensure that services are registered with the device manager as they are
92 // created and removed from the device manager as their connections are closed. 97 // created and removed from the device manager as their connections are closed.
93 TEST_F(VRServiceImplTest, DeviceManagerRegistration) { 98 TEST_F(VRServiceImplTest, DeviceManagerRegistration) {
(...skipping 25 matching lines...) Expand all
119 EXPECT_CALL(service_2->client(), OnDisplayChanged(_)); 124 EXPECT_CALL(service_2->client(), OnDisplayChanged(_));
120 125
121 std::unique_ptr<FakeVRDevice> device(new FakeVRDevice(provider_)); 126 std::unique_ptr<FakeVRDevice> device(new FakeVRDevice(provider_));
122 device_manager_->OnDeviceChanged(device->GetVRDevice()); 127 device_manager_->OnDeviceChanged(device->GetVRDevice());
123 128
124 base::RunLoop().RunUntilIdle(); 129 base::RunLoop().RunUntilIdle();
125 130
126 EXPECT_EQ(device->id(), service_1->client().LastDisplay()->index); 131 EXPECT_EQ(device->id(), service_1->client().LastDisplay()->index);
127 EXPECT_EQ(device->id(), service_2->client().LastDisplay()->index); 132 EXPECT_EQ(device->id(), service_2->client().LastDisplay()->index);
128 } 133 }
134
135 // Ensure that presenting devices cannot be accessed by other services
136 TEST_F(VRServiceImplTest, DevicePresentationIsolation) {
137 std::unique_ptr<VRServiceTestBinding> service_1 = BindService();
138 std::unique_ptr<VRServiceTestBinding> service_2 = BindService();
139
140 std::unique_ptr<FakeVRDevice> device(new FakeVRDevice(provider_));
141 provider_->AddDevice(device.get());
142
143 // Ensure the device manager has seen the fake device
144 device_manager_->GetVRDevices();
145
146 // When not presenting either service should be able to access the device
147 EXPECT_EQ(device.get(), VRDeviceManager::GetAllowedDevice(
148 service_1->service(), device->id()));
149 EXPECT_EQ(device.get(), VRDeviceManager::GetAllowedDevice(
150 service_2->service(), device->id()));
151
152 // Begin presenting to the fake device with service 1
153 EXPECT_TRUE(
154 device_manager_->RequestPresent(service_1->service(), device->id()));
155
156 EXPECT_TRUE(presenting());
157
158 // Service 2 should not be able to present to the device while service 1
159 // is still presenting.
160 EXPECT_FALSE(
161 device_manager_->RequestPresent(service_2->service(), device->id()));
162
163 // Only the presenting service should be able to access the device
164 EXPECT_EQ(device.get(), VRDeviceManager::GetAllowedDevice(
165 service_1->service(), device->id()));
166 EXPECT_EQ(nullptr, VRDeviceManager::GetAllowedDevice(service_2->service(),
167 device->id()));
168
169 // Service 2 should not be able to exit presentation to the device
170 device_manager_->ExitPresent(service_2->service(), device->id());
171 EXPECT_TRUE(presenting());
172
173 // Service 1 should be able to exit the presentation it initiated.
174 device_manager_->ExitPresent(service_1->service(), device->id());
175 EXPECT_FALSE(presenting());
176
177 // Once presention had ended both services should be able to access the device
178 EXPECT_EQ(device.get(), VRDeviceManager::GetAllowedDevice(
179 service_1->service(), device->id()));
180 EXPECT_EQ(device.get(), VRDeviceManager::GetAllowedDevice(
181 service_2->service(), device->id()));
129 } 182 }
183 }
OLDNEW
« no previous file with comments | « device/vr/vr_service_impl.cc ('k') | third_party/WebKit/Source/modules/vr/VRController.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698