summaryrefslogtreecommitdiff
path: root/chromium/device/vr/vr_display_impl_unittest.cc
blob: 6735be754f8b1072483bde13ed9f6f8b16d53b31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2016 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 "device/vr/vr_display_impl.h"

#include <memory>

#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "device/vr/public/mojom/vr_service.mojom.h"
#include "device/vr/test/fake_vr_device.h"
#include "device/vr/test/fake_vr_service_client.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace device {

class VRDisplayImplTest : public testing::Test {
 public:
  VRDisplayImplTest() {}
  ~VRDisplayImplTest() override {}
  void onDisplaySynced() {}
  void onPresentComplete(
      device::mojom::XRPresentationConnectionPtr connection,
      mojom::XRSessionControllerPtr immersive_session_controller) {
    is_request_presenting_success_ = connection ? true : false;

    immersive_session_controller_ = std::move(immersive_session_controller);
  }

 protected:
  void SetUp() override {
    device_ = std::make_unique<FakeVRDevice>(1);
    device_->SetPose(mojom::VRPose::New());
    mojom::VRServiceClientPtr proxy;
    client_ = std::make_unique<FakeVRServiceClient>(mojo::MakeRequest(&proxy));
  }

  std::unique_ptr<VRDisplayImpl> MakeDisplay(
      mojom::XRSessionControllerPtr* controller) {
    mojom::VRMagicWindowProviderPtr session;
    auto display = std::make_unique<VRDisplayImpl>(
        device(), mojo::MakeRequest(&session), mojo::MakeRequest(controller));
    static_cast<mojom::XRSessionController*>(display.get())
        ->SetFrameDataRestricted(true);
    return display;
  }

  void RequestSession(VRDisplayImpl* display_impl) {
    device_->RequestSession(
        mojom::XRDeviceRuntimeSessionOptionsPtr(),
        base::BindOnce(&VRDisplayImplTest::onPresentComplete,
                       base::Unretained(this)));
  }

  void ExitPresent() {
    device_->StopSession();
    immersive_session_controller_ = nullptr;
  }

  bool presenting() { return device_->IsPresenting(); }
  VRDeviceBase* device() { return device_.get(); }
  FakeVRServiceClient* client() { return client_.get(); }

  base::MessageLoop message_loop_;
  bool is_request_presenting_success_ = false;
  std::unique_ptr<FakeVRDevice> device_;
  std::unique_ptr<FakeVRServiceClient> client_;
  mojom::XRSessionControllerPtr immersive_session_controller_;

  DISALLOW_COPY_AND_ASSIGN(VRDisplayImplTest);
};

TEST_F(VRDisplayImplTest, DevicePresentationIsolation) {
  mojom::XRSessionControllerPtr controller1;
  std::unique_ptr<VRDisplayImpl> display_1 = MakeDisplay(&controller1);
  static_cast<mojom::XRSessionController*>(display_1.get())
      ->SetFrameDataRestricted(false);
  mojom::XRSessionControllerPtr controller2;
  std::unique_ptr<VRDisplayImpl> display_2 = MakeDisplay(&controller2);
  static_cast<mojom::XRSessionController*>(display_2.get())
      ->SetFrameDataRestricted(false);

  // When not presenting either service should be able to access the device.
  EXPECT_FALSE(device()->HasExclusiveSession());

  bool was_called = false;
  auto callback = [](bool expect_null, bool* was_called,
                     mojom::XRFrameDataPtr data) {
    *was_called = true;
    EXPECT_EQ(expect_null, !data);
  };

  static_cast<mojom::VRMagicWindowProvider*>(display_1.get())
      ->GetFrameData(base::BindOnce(callback, false, &was_called));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(was_called);
  was_called = false;
  static_cast<mojom::VRMagicWindowProvider*>(display_2.get())
      ->GetFrameData(base::BindOnce(callback, false, &was_called));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(was_called);
  was_called = false;

  // Attempt to present.
  RequestSession(display_1.get());
  EXPECT_TRUE(is_request_presenting_success_);
  EXPECT_TRUE(presenting());
  EXPECT_TRUE(device()->HasExclusiveSession());

  // While a device is presenting, noone should have access to magic window.
  static_cast<mojom::VRMagicWindowProvider*>(display_1.get())
      ->GetFrameData(base::BindOnce(callback, true, &was_called));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(was_called);
  was_called = false;
  static_cast<mojom::VRMagicWindowProvider*>(display_2.get())
      ->GetFrameData(base::BindOnce(callback, true, &was_called));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(was_called);
  was_called = false;

  // Service 1 should be able to exit the presentation it initiated.
  ExitPresent();
  EXPECT_FALSE(presenting());
  EXPECT_FALSE(device()->HasExclusiveSession());

  // Once presentation had ended both services should be able to access the
  // device.
  static_cast<mojom::VRMagicWindowProvider*>(display_1.get())
      ->GetFrameData(base::BindOnce(callback, false, &was_called));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(was_called);
  was_called = false;
  static_cast<mojom::VRMagicWindowProvider*>(display_2.get())
      ->GetFrameData(base::BindOnce(callback, false, &was_called));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(was_called);
  was_called = false;
}

}