blob: 9e8dfe1ad8a8d236c2ba900b77cb9b58f8dae182 [file] [log] [blame]
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/src/painting/_network_image_web.dart';
import 'package:js/js.dart';
import 'package:js/js_util.dart' as js_util;
class DomDocument {}
extension DomDocumentExtension on DomDocument {
external DomEvent createEvent(String eventType);
external DomDocument get domDocument;
DomEvent createDomEvent(String type, String name) {
final DomEvent event = domDocument.createEvent(type);
event.initEvent(name, true, true);
return event;
external void objectDefineProperty(Object o, String symbol, dynamic desc);
void createGetter<T>(Object mock, String key, T Function() get) {
<dynamic, dynamic>{
'get': allowInterop(() => get())
class DomXMLHttpRequestMock {
external factory DomXMLHttpRequestMock({
void Function(String method, String url, bool async)? open,
String responseType = 'invalid',
int timeout = 10,
bool withCredentials = false,
void Function()? send,
void Function(String name, String value)? setRequestHeader,
void Function(String type, DomEventListener listener) addEventListener,
class TestHttpRequest {
TestHttpRequest() {
_mock = DomXMLHttpRequestMock(
open: allowInterop(open),
send: allowInterop(send),
setRequestHeader: allowInterop(setRequestHeader),
addEventListener: allowInterop(addEventListener),
createGetter(_mock, 'headers', () => headers);
createGetter(_mock, 'responseHeaders', () => responseHeaders);
createGetter(_mock, 'status', () => status);
createGetter(_mock, 'response', () => response);
late DomXMLHttpRequestMock _mock;
MockEvent? mockEvent;
Map<String, String> headers = <String, String>{};
int status = -1;
dynamic response;
Map<String, String> get responseHeaders => headers;
void open(String method, String url, bool async) {}
void send() {}
void setRequestHeader(String name, String value) {
headers[name] = value;
void addEventListener(String type, DomEventListener listener) {
if (type == mockEvent?.type) {
DomXMLHttpRequest getMock() => _mock as DomXMLHttpRequest;
class MockEvent {
MockEvent(this.type, this.event);
final String type;
final DomEvent event;