Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[webview_flutter] Implementation of the app facing WebViewCookieManager for v4 #6344

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2013 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:webview_flutter_platform_interface/v4/webview_flutter_platform_interface.dart';

/// Manages cookies pertaining to all WebViews.
class WebViewCookieManager {
/// Constructs a [WebViewCookieManager].
WebViewCookieManager()
: this.fromPlatform(
platform: PlatformWebViewCookieManager(
const PlatformWebViewCookieManagerCreationParams(),
),
);

/// Constructs a [WebViewCookieManager] from creation params for a specific
/// platform.
WebViewCookieManager.fromPlatformCreationParams(
PlatformWebViewCookieManagerCreationParams params,
) : this.fromPlatform(platform: PlatformWebViewCookieManager(params));

/// Constructs a [WebViewCookieManager] from a specific platform
/// implementation.
WebViewCookieManager.fromPlatform({required this.platform});

/// Implementation of [PlatformWebViewCookieManager] for the current platform.
final PlatformWebViewCookieManager platform;

/// Clears all cookies for all WebViews.
///
/// Returns true if cookies were present before clearing, else false.
Future<bool> clearCookies() => platform.clearCookies();

/// Sets a cookie for all WebView instances.
///
/// This is a no op on iOS versions below 11.
Future<void> setCookie(WebViewCookie cookie) => platform.setCookie(cookie);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2013 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.

library webview_flutter;

export 'package:webview_flutter_platform_interface/v4/webview_flutter_platform_interface.dart'
show WebViewCookie;

export 'src/webview_cookie_manager.dart';
2 changes: 1 addition & 1 deletion packages/webview_flutter/webview_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies:
flutter:
sdk: flutter
webview_flutter_android: ^2.8.0
webview_flutter_platform_interface: ^1.8.0
webview_flutter_platform_interface: ^1.9.3
webview_flutter_wkwebview: ^2.7.0

dev_dependencies:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2013 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_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:webview_flutter/src/v4/src/webview_cookie_manager.dart';
import 'package:webview_flutter_platform_interface/v4/webview_flutter_platform_interface.dart';

import 'webview_cookie_manager_test.mocks.dart';

@GenerateMocks(<Type>[PlatformWebViewCookieManager])
void main() {
group('WebViewCookieManager', () {
test('clearCookies', () async {
final MockPlatformWebViewCookieManager mockPlatformWebViewCookieManager =
MockPlatformWebViewCookieManager();
when(mockPlatformWebViewCookieManager.clearCookies()).thenAnswer(
(_) => Future<bool>.value(false),
);

final WebViewCookieManager cookieManager =
WebViewCookieManager.fromPlatform(
platform: mockPlatformWebViewCookieManager,
);

await expectLater(cookieManager.clearCookies(), completion(false));
});

test('setCookie', () async {
final MockPlatformWebViewCookieManager mockPlatformWebViewCookieManager =
MockPlatformWebViewCookieManager();

final WebViewCookieManager cookieManager =
WebViewCookieManager.fromPlatform(
platform: mockPlatformWebViewCookieManager,
);

const WebViewCookie cookie = WebViewCookie(
name: 'name',
value: 'value',
domain: 'domain',
);

await cookieManager.setCookie(cookie);

final WebViewCookie capturedCookie = verify(
mockPlatformWebViewCookieManager.setCookie(captureAny),
).captured.single as WebViewCookie;
expect(capturedCookie, cookie);
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Mocks generated by Mockito 5.3.0 from annotations
// in webview_flutter/test/v4/webview_cookie_manager_test.dart.
// Do not manually edit this file.

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i4;

import 'package:mockito/mockito.dart' as _i1;
import 'package:webview_flutter_platform_interface/v4/src/platform_webview_cookie_manager.dart'
as _i3;
import 'package:webview_flutter_platform_interface/v4/src/webview_platform.dart'
as _i2;

// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
// ignore_for_file: avoid_setters_without_getters
// ignore_for_file: comment_references
// ignore_for_file: implementation_imports
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types
// ignore_for_file: subtype_of_sealed_class

class _FakePlatformWebViewCookieManagerCreationParams_0 extends _i1.SmartFake
implements _i2.PlatformWebViewCookieManagerCreationParams {
_FakePlatformWebViewCookieManagerCreationParams_0(
Object parent, Invocation parentInvocation)
: super(parent, parentInvocation);
}

/// A class which mocks [PlatformWebViewCookieManager].
///
/// See the documentation for Mockito's code generation for more information.
class MockPlatformWebViewCookieManager extends _i1.Mock
implements _i3.PlatformWebViewCookieManager {
MockPlatformWebViewCookieManager() {
_i1.throwOnMissingStub(this);
}

@override
_i2.PlatformWebViewCookieManagerCreationParams get params =>
(super.noSuchMethod(Invocation.getter(#params),
returnValue: _FakePlatformWebViewCookieManagerCreationParams_0(
this, Invocation.getter(#params)))
as _i2.PlatformWebViewCookieManagerCreationParams);
@override
_i4.Future<bool> clearCookies() =>
(super.noSuchMethod(Invocation.method(#clearCookies, []),
returnValue: _i4.Future<bool>.value(false)) as _i4.Future<bool>);
@override
_i4.Future<void> setCookie(_i2.WebViewCookie? cookie) => (super.noSuchMethod(
Invocation.method(#setCookie, [cookie]),
returnValue: _i4.Future<void>.value(),
returnValueForMissingStub: _i4.Future<void>.value()) as _i4.Future<void>);
}