summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/presentation/presentation_connection_callbacks_test.cc
blob: 3f3f49c4e503428eb35019638c6f070a7357e701 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2020 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 "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/gmock/include/gmock/gmock.h"  // MockFunctionScope
#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_function.h"  // MockFunctionScope
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/core/v8/script_value.h"  // MockFunctionScope
#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/bindings/script_state.h"  // MockFunctionScope
#include "third_party/blink/renderer/platform/heap/heap.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"  // MockFunctionScope
#include "third_party/blink/renderer/platform/testing/url_test_helpers.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"  // MockFunctionScope
#include "third_party/blink/renderer/platform/wtf/vector.h"  // MockFunctionScope

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 {

// TODO(crbug.com/1120218): Consolidate with PaymentRequestMockFunctionScope and
// move to shared folder
class MockFunctionScope {
  STACK_ALLOCATED();

 public:
  explicit MockFunctionScope(ScriptState* script_state)
      : script_state_(script_state) {}
  ~MockFunctionScope() {
    v8::MicrotasksScope::PerformCheckpoint(script_state_->GetIsolate());
    for (MockFunction* mock_function : mock_functions_)
      testing::Mock::VerifyAndClearExpectations(mock_function);
  }

  v8::Local<v8::Function> ExpectCall() {
    mock_functions_.push_back(
        MakeGarbageCollected<MockFunction>(script_state_));
    EXPECT_CALL(*mock_functions_.back(), Call(testing::_));
    return mock_functions_.back()->Bind();
  }

  v8::Local<v8::Function> ExpectNoCall() {
    mock_functions_.push_back(
        MakeGarbageCollected<MockFunction>(script_state_));
    EXPECT_CALL(*mock_functions_.back(), Call(testing::_)).Times(0);
    return mock_functions_.back()->Bind();
  }

 private:
  class MockFunction : public ScriptFunction {
   public:
    explicit MockFunction(ScriptState* script_state)
        : ScriptFunction(script_state) {
      ON_CALL(*this, Call(testing::_)).WillByDefault(testing::ReturnArg<0>());
    }
    v8::Local<v8::Function> Bind() { return BindToV8Function(); }
    MOCK_METHOD1(Call, ScriptValue(ScriptValue));
  };

  ScriptState* script_state_;
  Vector<Persistent<MockFunction>> mock_functions_;
};

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) {
  V8TestingScope scope;
  MockFunctionScope funcs(scope.GetScriptState());
  auto* resolver =
      MakeGarbageCollected<ScriptPromiseResolver>(scope.GetScriptState());
  resolver->Promise().Then(funcs.ExpectCall(), funcs.ExpectNoCall());

  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);
}

TEST(PresentationConnectionCallbacksTest, HandleReconnect) {
  V8TestingScope scope;
  MockFunctionScope funcs(scope.GetScriptState());
  PresentationInfoPtr info = PresentationInfo::New(
      url_test_helpers::ToKURL(kPresentationUrl), kPresentationId);
  auto* resolver =
      MakeGarbageCollected<ScriptPromiseResolver>(scope.GetScriptState());
  resolver->Promise().Then(funcs.ExpectCall(), funcs.ExpectNoCall());

  auto* connection = ControllerPresentationConnection::Take(
      resolver, *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);
}

TEST(PresentationConnectionCallbacksTest, HandleError) {
  V8TestingScope scope;
  MockFunctionScope funcs(scope.GetScriptState());
  auto* resolver =
      MakeGarbageCollected<ScriptPromiseResolver>(scope.GetScriptState());
  resolver->Promise().Then(funcs.ExpectNoCall(), funcs.ExpectCall());

  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_);
}

}  // namespace blink