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 144 145 146 147 148 149 150
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/presentation/presentation_connection_callbacks.h"
#include <utility>
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/presentation/presentation.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_tester.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/modules/presentation/presentation_connection.h"
#include "third_party/blink/renderer/modules/presentation/presentation_request.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"
#include "third_party/blink/renderer/platform/testing/url_test_helpers.h"
constexpr char kPresentationUrl[] = "https://example.com";
constexpr char kPresentationId[] = "xyzzy";
namespace blink {
using mojom::blink::PresentationConnectionResult;
using mojom::blink::PresentationConnectionResultPtr;
using mojom::blink::PresentationConnectionState;
using mojom::blink::PresentationError;
using mojom::blink::PresentationErrorType;
using mojom::blink::PresentationInfo;
using mojom::blink::PresentationInfoPtr;
namespace {
static PresentationRequest* MakeRequest(V8TestingScope* scope) {
PresentationRequest* request =
PresentationRequest::Create(scope->GetExecutionContext(),
kPresentationUrl, scope->GetExceptionState());
EXPECT_FALSE(scope->GetExceptionState().HadException());
return request;
}
} // namespace
TEST(PresentationConnectionCallbacksTest, HandleSuccess) {
test::TaskEnvironment task_environment;
V8TestingScope scope;
auto* resolver =
MakeGarbageCollected<ScriptPromiseResolver<PresentationConnection>>(
scope.GetScriptState());
ScriptPromiseTester promise_tester(scope.GetScriptState(),
resolver->Promise());
PresentationConnectionCallbacks callbacks(resolver, MakeRequest(&scope));
// No connection currently exists.
EXPECT_FALSE(callbacks.connection_);
mojo::PendingRemote<mojom::blink::PresentationConnection> connection_remote;
mojo::PendingReceiver<mojom::blink::PresentationConnection>
connection_receiver = connection_remote.InitWithNewPipeAndPassReceiver();
PresentationConnectionResultPtr result = PresentationConnectionResult::New(
PresentationInfo::New(url_test_helpers::ToKURL(kPresentationUrl),
kPresentationId),
std::move(connection_remote), std::move(connection_receiver));
callbacks.HandlePresentationResponse(std::move(result), nullptr);
// New connection was created.
ControllerPresentationConnection* connection = callbacks.connection_.Get();
ASSERT_TRUE(connection);
EXPECT_EQ(connection->GetState(), PresentationConnectionState::CONNECTING);
// Connection must be closed before the next connection test.
connection->close();
scope.PerformMicrotaskCheckpoint();
EXPECT_TRUE(promise_tester.IsFulfilled());
}
TEST(PresentationConnectionCallbacksTest, HandleReconnect) {
test::TaskEnvironment task_environment;
V8TestingScope scope;
PresentationInfoPtr info = PresentationInfo::New(
url_test_helpers::ToKURL(kPresentationUrl), kPresentationId);
auto* resolver =
MakeGarbageCollected<ScriptPromiseResolver<PresentationConnection>>(
scope.GetScriptState());
ScriptPromiseTester promise_tester(scope.GetScriptState(),
resolver->Promise());
auto* connection = ControllerPresentationConnection::Create(
scope.GetExecutionContext(), *info, MakeRequest(&scope));
// Connection must be closed for reconnection to succeed.
connection->close();
PresentationConnectionCallbacks callbacks(resolver, connection);
mojo::PendingRemote<mojom::blink::PresentationConnection> connection_remote;
mojo::PendingReceiver<mojom::blink::PresentationConnection>
connection_receiver = connection_remote.InitWithNewPipeAndPassReceiver();
PresentationConnectionResultPtr result = PresentationConnectionResult::New(
std::move(info), std::move(connection_remote),
std::move(connection_receiver));
callbacks.HandlePresentationResponse(std::move(result), nullptr);
// Previous connection was returned.
ControllerPresentationConnection* new_connection =
callbacks.connection_.Get();
EXPECT_EQ(connection, new_connection);
EXPECT_EQ(new_connection->GetState(),
PresentationConnectionState::CONNECTING);
// Connection must be closed before the next connection test.
connection->close();
scope.PerformMicrotaskCheckpoint();
EXPECT_TRUE(promise_tester.IsFulfilled());
}
TEST(PresentationConnectionCallbacksTest, HandleError) {
test::TaskEnvironment task_environment;
V8TestingScope scope;
auto* resolver =
MakeGarbageCollected<ScriptPromiseResolver<PresentationConnection>>(
scope.GetScriptState());
ScriptPromiseTester promise_tester(scope.GetScriptState(),
resolver->Promise());
PresentationConnectionCallbacks callbacks(resolver, MakeRequest(&scope));
// No connection currently exists.
EXPECT_FALSE(callbacks.connection_);
callbacks.HandlePresentationResponse(
nullptr,
PresentationError::New(PresentationErrorType::NO_PRESENTATION_FOUND,
"Something bad happened"));
// No connection was created.
EXPECT_FALSE(callbacks.connection_);
scope.PerformMicrotaskCheckpoint();
EXPECT_TRUE(promise_tester.IsRejected());
}
} // namespace blink
|