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.

Commit 50f4cb8

Browse files
[webview_flutter] Implementation of the app facing WebViewCookieManager for v4 (#6344)
1 parent 686dcf4 commit 50f4cb8

File tree

5 files changed

+160
-1
lines changed

5 files changed

+160
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:webview_flutter_platform_interface/v4/webview_flutter_platform_interface.dart';
6+
7+
/// Manages cookies pertaining to all WebViews.
8+
class WebViewCookieManager {
9+
/// Constructs a [WebViewCookieManager].
10+
WebViewCookieManager()
11+
: this.fromPlatform(
12+
platform: PlatformWebViewCookieManager(
13+
const PlatformWebViewCookieManagerCreationParams(),
14+
),
15+
);
16+
17+
/// Constructs a [WebViewCookieManager] from creation params for a specific
18+
/// platform.
19+
WebViewCookieManager.fromPlatformCreationParams(
20+
PlatformWebViewCookieManagerCreationParams params,
21+
) : this.fromPlatform(platform: PlatformWebViewCookieManager(params));
22+
23+
/// Constructs a [WebViewCookieManager] from a specific platform
24+
/// implementation.
25+
WebViewCookieManager.fromPlatform({required this.platform});
26+
27+
/// Implementation of [PlatformWebViewCookieManager] for the current platform.
28+
final PlatformWebViewCookieManager platform;
29+
30+
/// Clears all cookies for all WebViews.
31+
///
32+
/// Returns true if cookies were present before clearing, else false.
33+
Future<bool> clearCookies() => platform.clearCookies();
34+
35+
/// Sets a cookie for all WebView instances.
36+
///
37+
/// This is a no op on iOS versions below 11.
38+
Future<void> setCookie(WebViewCookie cookie) => platform.setCookie(cookie);
39+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
library webview_flutter;
6+
7+
export 'package:webview_flutter_platform_interface/v4/webview_flutter_platform_interface.dart'
8+
show WebViewCookie;
9+
10+
export 'src/webview_cookie_manager.dart';

packages/webview_flutter/webview_flutter/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ dependencies:
2020
flutter:
2121
sdk: flutter
2222
webview_flutter_android: ^2.8.0
23-
webview_flutter_platform_interface: ^1.8.0
23+
webview_flutter_platform_interface: ^1.9.3
2424
webview_flutter_wkwebview: ^2.7.0
2525

2626
dev_dependencies:
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:mockito/annotations.dart';
7+
import 'package:mockito/mockito.dart';
8+
import 'package:webview_flutter/src/v4/src/webview_cookie_manager.dart';
9+
import 'package:webview_flutter_platform_interface/v4/webview_flutter_platform_interface.dart';
10+
11+
import 'webview_cookie_manager_test.mocks.dart';
12+
13+
@GenerateMocks(<Type>[PlatformWebViewCookieManager])
14+
void main() {
15+
group('WebViewCookieManager', () {
16+
test('clearCookies', () async {
17+
final MockPlatformWebViewCookieManager mockPlatformWebViewCookieManager =
18+
MockPlatformWebViewCookieManager();
19+
when(mockPlatformWebViewCookieManager.clearCookies()).thenAnswer(
20+
(_) => Future<bool>.value(false),
21+
);
22+
23+
final WebViewCookieManager cookieManager =
24+
WebViewCookieManager.fromPlatform(
25+
platform: mockPlatformWebViewCookieManager,
26+
);
27+
28+
await expectLater(cookieManager.clearCookies(), completion(false));
29+
});
30+
31+
test('setCookie', () async {
32+
final MockPlatformWebViewCookieManager mockPlatformWebViewCookieManager =
33+
MockPlatformWebViewCookieManager();
34+
35+
final WebViewCookieManager cookieManager =
36+
WebViewCookieManager.fromPlatform(
37+
platform: mockPlatformWebViewCookieManager,
38+
);
39+
40+
const WebViewCookie cookie = WebViewCookie(
41+
name: 'name',
42+
value: 'value',
43+
domain: 'domain',
44+
);
45+
46+
await cookieManager.setCookie(cookie);
47+
48+
final WebViewCookie capturedCookie = verify(
49+
mockPlatformWebViewCookieManager.setCookie(captureAny),
50+
).captured.single as WebViewCookie;
51+
expect(capturedCookie, cookie);
52+
});
53+
});
54+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Mocks generated by Mockito 5.3.0 from annotations
2+
// in webview_flutter/test/v4/webview_cookie_manager_test.dart.
3+
// Do not manually edit this file.
4+
5+
// ignore_for_file: no_leading_underscores_for_library_prefixes
6+
import 'dart:async' as _i4;
7+
8+
import 'package:mockito/mockito.dart' as _i1;
9+
import 'package:webview_flutter_platform_interface/v4/src/platform_webview_cookie_manager.dart'
10+
as _i3;
11+
import 'package:webview_flutter_platform_interface/v4/src/webview_platform.dart'
12+
as _i2;
13+
14+
// ignore_for_file: type=lint
15+
// ignore_for_file: avoid_redundant_argument_values
16+
// ignore_for_file: avoid_setters_without_getters
17+
// ignore_for_file: comment_references
18+
// ignore_for_file: implementation_imports
19+
// ignore_for_file: invalid_use_of_visible_for_testing_member
20+
// ignore_for_file: prefer_const_constructors
21+
// ignore_for_file: unnecessary_parenthesis
22+
// ignore_for_file: camel_case_types
23+
// ignore_for_file: subtype_of_sealed_class
24+
25+
class _FakePlatformWebViewCookieManagerCreationParams_0 extends _i1.SmartFake
26+
implements _i2.PlatformWebViewCookieManagerCreationParams {
27+
_FakePlatformWebViewCookieManagerCreationParams_0(
28+
Object parent, Invocation parentInvocation)
29+
: super(parent, parentInvocation);
30+
}
31+
32+
/// A class which mocks [PlatformWebViewCookieManager].
33+
///
34+
/// See the documentation for Mockito's code generation for more information.
35+
class MockPlatformWebViewCookieManager extends _i1.Mock
36+
implements _i3.PlatformWebViewCookieManager {
37+
MockPlatformWebViewCookieManager() {
38+
_i1.throwOnMissingStub(this);
39+
}
40+
41+
@override
42+
_i2.PlatformWebViewCookieManagerCreationParams get params =>
43+
(super.noSuchMethod(Invocation.getter(#params),
44+
returnValue: _FakePlatformWebViewCookieManagerCreationParams_0(
45+
this, Invocation.getter(#params)))
46+
as _i2.PlatformWebViewCookieManagerCreationParams);
47+
@override
48+
_i4.Future<bool> clearCookies() =>
49+
(super.noSuchMethod(Invocation.method(#clearCookies, []),
50+
returnValue: _i4.Future<bool>.value(false)) as _i4.Future<bool>);
51+
@override
52+
_i4.Future<void> setCookie(_i2.WebViewCookie? cookie) => (super.noSuchMethod(
53+
Invocation.method(#setCookie, [cookie]),
54+
returnValue: _i4.Future<void>.value(),
55+
returnValueForMissingStub: _i4.Future<void>.value()) as _i4.Future<void>);
56+
}

0 commit comments

Comments
 (0)