| 1 |
// Copyright (C) 2023 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
import QtQuick |
| 5 |
import QtGrpc; |
| 6 |
import QtProtobuf; |
| 7 |
import QtTest |
| 8 |
import QmlTestUri |
| 9 |
import qtgrpc.tests |
| 10 |
|
| 11 |
Item { |
| 12 |
id: root |
| 13 |
|
| 14 |
Timer { |
| 15 |
id: timer |
| 16 |
running: false |
| 17 |
repeat: false |
| 18 |
interval: 10000 |
| 19 |
onTriggered: testCase.when = true; |
| 20 |
} |
| 21 |
|
| 22 |
property simpleStringMessage messageArg; |
| 23 |
property simpleStringMessage messageResponse; |
| 24 |
|
| 25 |
property bool calbackCalled: false |
| 26 |
property var clientQml |
| 27 |
property var grpcChannel |
| 28 |
property var grpcChannelDeadline |
| 29 |
property var setResponse: function(value) { root.messageResponse = value; testCase.when = true; } |
| 30 |
property var errorCallback: function() { root.calbackCalled = true; testCase.when = true; } |
| 31 |
|
| 32 |
function createClientItem() { |
| 33 |
return Qt.createQmlObject("import QtQuick; import QtGrpc; \ |
| 34 |
import qtgrpc.tests; TestServiceClient {}", root) |
| 35 |
} |
| 36 |
|
| 37 |
function createGrpcChannelItem() { |
| 38 |
return Qt.createQmlObject("import QtQuick; import QtGrpc; GrpcHttp2Channel { \ |
| 39 |
hostUri: \"http://localhost:50051\"; \ |
| 40 |
options: GrpcChannelOptions { \ |
| 41 |
deadlineTimeout: { 2000 } \ |
| 42 |
metadata: GrpcMetadata { |
| 43 |
data: ({ \"common-meta-data\": \"test-channel-metadata\" }) \ |
| 44 |
}}}", root) |
| 45 |
} |
| 46 |
|
| 47 |
function createGrpcChannelWithDeadlineItem() { |
| 48 |
return Qt.createQmlObject("import QtQuick; import QtGrpc; GrpcHttp2Channel { \ |
| 49 |
hostUri: \"http://localhost:50051\"; \ |
| 50 |
options: GrpcChannelOptions { \ |
| 51 |
deadlineTimeout: { 1000 } } }", root) |
| 52 |
} |
| 53 |
|
| 54 |
TestCase { |
| 55 |
name: "qtgrpcClientRegistration" |
| 56 |
function test_1clientTypes_data() { |
| 57 |
return [ |
| 58 |
{ tag: "Grpc Client created", |
| 59 |
field: typeof clientQml, answer: "object" }, |
| 60 |
{ tag: "Grpc Http2 Channel created", |
| 61 |
field: typeof grpcChannel, answer: "object" }, |
| 62 |
{ tag: "Grpc Http2 Deadline Channel created", |
| 63 |
field: typeof grpcChannelDeadline, answer: "object" } |
| 64 |
] |
| 65 |
} |
| 66 |
|
| 67 |
function test_1clientTypes(data) { |
| 68 |
compare(data.field, data.answer) |
| 69 |
} |
| 70 |
|
| 71 |
function test_ChannelOptions_data() { |
| 72 |
return [ |
| 73 |
{ tag: "grpcChannelOptions URL is set", |
| 74 |
field: grpcChannel.hostUri, answer: "http://localhost:50051" }, |
| 75 |
{ tag: "grpcChannelOptions deadline is set", |
| 76 |
field: grpcChannelDeadline.options.deadlineTimeout, answer: 1000 } |
| 77 |
] |
| 78 |
} |
| 79 |
|
| 80 |
function test_ChannelOptions(data) { |
| 81 |
compare(data.field, data.answer) |
| 82 |
} |
| 83 |
|
| 84 |
function test_testMethodCall() { |
| 85 |
clientQml.testMethod(root.messageArg, root.setResponse, root.errorCallback); |
| 86 |
timer.start() |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
TestCase { |
| 91 |
id: testCase |
| 92 |
name: "qtgrpcClientTestCall" |
| 93 |
when: false |
| 94 |
|
| 95 |
function test_testMethodCallCheck() { |
| 96 |
verify(root.messageResponse == root.messageArg) |
| 97 |
verify(!root.calbackCalled) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
TestCase { |
| 102 |
id: unaryCallWithOptions |
| 103 |
name: "unaryCallWithOptions" |
| 104 |
property empty arg; |
| 105 |
property metadataMessage result; |
| 106 |
property bool errorOccurred: false; |
| 107 |
GrpcCallOptions { |
| 108 |
id: options |
| 109 |
metadata: GrpcMetadata { |
| 110 |
data: ({ "user-name": "localhost", |
| 111 |
"user-password": "qwerty"}) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
Timer { |
| 116 |
id: unaryCallWithOptionsTimeout |
| 117 |
running: false |
| 118 |
repeat: false |
| 119 |
interval: 10000 |
| 120 |
onTriggered: unaryCallWithOptionsCheck.when = true; |
| 121 |
} |
| 122 |
function test_unaryCallWithOptions() { |
| 123 |
clientQml.replyWithMetadata(unaryCallWithOptions.arg, |
| 124 |
function(value) { |
| 125 |
unaryCallWithOptions.result = value; |
| 126 |
unaryCallWithOptionsCheck.when = true; |
| 127 |
}, |
| 128 |
function() { |
| 129 |
unaryCallWithOptions.errorOccurred = true; |
| 130 |
unaryCallWithOptionsCheck.when = true; |
| 131 |
}, |
| 132 |
options) |
| 133 |
unaryCallWithOptionsTimeout.start() |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
TestCase { |
| 138 |
id: unaryCallWithOptionsCheck |
| 139 |
name: "unaryCallWithOptionsCheck" |
| 140 |
when: false |
| 141 |
|
| 142 |
function removeElementFromArray(array, element) { |
| 143 |
var index = array.indexOf(element) |
| 144 |
verify(index !== -1) |
| 145 |
|
| 146 |
array.splice(index, 1) |
| 147 |
} |
| 148 |
|
| 149 |
function test_unaryCallWithOptionsCheck() { |
| 150 |
verify(!unaryCallWithOptions.errorOccurred, "unaryCallWithOptions ended with error") |
| 151 |
|
| 152 |
var missingHeaders = Array() |
| 153 |
missingHeaders.push("user-name") |
| 154 |
missingHeaders.push("user-password") |
| 155 |
missingHeaders.push("common-meta-data") |
| 156 |
|
| 157 |
for (var i = 0; i < unaryCallWithOptions.result.valuesData.length; i++) { |
| 158 |
var md = unaryCallWithOptions.result.valuesData[i] |
| 159 |
if (md.key === "user-name" && md.value === "localhost") |
| 160 |
removeElementFromArray(missingHeaders, "user-name") |
| 161 |
if (md.key === "user-password" && md.value === "qwerty") |
| 162 |
removeElementFromArray(missingHeaders, "user-password") |
| 163 |
if (md.key === "common-meta-data" && md.value === "test-channel-metadata") |
| 164 |
removeElementFromArray(missingHeaders, "common-meta-data") |
| 165 |
} |
| 166 |
|
| 167 |
verify(missingHeaders.length === 0, |
| 168 |
"Missing headers from server: " + missingHeaders) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
Component.onCompleted: { |
| 174 |
clientQml = root.createClientItem() |
| 175 |
grpcChannel = root.createGrpcChannelItem() |
| 176 |
grpcChannelDeadline = root.createGrpcChannelWithDeadlineItem() |
| 177 |
clientQml.channel = grpcChannel.channel |
| 178 |
} |
| 179 |
} |