Skip to content

Commit 405e465

Browse files
[webview_flutter] Enable warnings-as-errors on Android (#3356)
[webview_flutter] Enable warnings-as-errors on Android
1 parent b220ace commit 405e465

File tree

13 files changed

+155
-130
lines changed

13 files changed

+155
-130
lines changed

packages/webview_flutter/webview_flutter_android/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 3.3.2
22

3+
* Resolves compilations warnings.
34
* Updates compileSdkVersion to 33.
45
* Bumps androidx.webkit:webkit from 1.5.0 to 1.6.0.
56

packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/CookieManagerHostApiImpl.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,28 @@ public void clearCookies(GeneratedAndroidWebView.Result<Boolean> result) {
1414
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
1515
cookieManager.removeAllCookies(result::success);
1616
} else {
17-
final boolean hasCookies = cookieManager.hasCookies();
18-
if (hasCookies) {
19-
cookieManager.removeAllCookie();
20-
}
21-
result.success(hasCookies);
17+
result.success(removeCookiesPreL(cookieManager));
2218
}
2319
}
2420

2521
@Override
2622
public void setCookie(String url, String value) {
2723
CookieManager.getInstance().setCookie(url, value);
2824
}
25+
26+
/**
27+
* Removes all cookies from the given cookie manager, using the deprecated (pre-Lollipop)
28+
* implementation.
29+
*
30+
* @param cookieManager The cookie manager to clear all cookies from.
31+
* @return Whether any cookies were removed.
32+
*/
33+
@SuppressWarnings("deprecation")
34+
private boolean removeCookiesPreL(CookieManager cookieManager) {
35+
final boolean hasCookies = cookieManager.hasCookies();
36+
if (hasCookies) {
37+
cookieManager.removeAllCookie();
38+
}
39+
return hasCookies;
40+
}
2941
}

packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/GeneratedAndroidWebView.java

Lines changed: 97 additions & 104 deletions
Large diffs are not rendered by default.

packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebChromeClientHostApiImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ public boolean shouldOverrideUrlLoading(
130130
return true;
131131
}
132132

133+
// Legacy codepath for < N.
134+
@SuppressWarnings("deprecation")
133135
@Override
134136
public boolean shouldOverrideUrlLoading(WebView windowWebView, String url) {
135137
if (!webViewClient.shouldOverrideUrlLoading(view, url)) {

packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewClientHostApiImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public void onReceivedError(WebView view, WebResourceRequest request, WebResourc
5959
flutterApi.onReceivedRequestError(this, view, request, error, reply -> {});
6060
}
6161

62+
// Legacy codepath for < 23; newer versions use the variant above.
63+
@SuppressWarnings("deprecation")
6264
@Override
6365
public void onReceivedError(
6466
WebView view, int errorCode, String description, String failingUrl) {
@@ -72,6 +74,8 @@ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request
7274
return returnValueForShouldOverrideUrlLoading;
7375
}
7476

77+
// Legacy codepath for < 24; newer versions use the variant above.
78+
@SuppressWarnings("deprecation")
7579
@Override
7680
public boolean shouldOverrideUrlLoading(WebView view, String url) {
7781
flutterApi.urlLoading(this, view, url, reply -> {});
@@ -125,6 +129,8 @@ public void onReceivedError(
125129
flutterApi.onReceivedRequestError(this, view, request, error, reply -> {});
126130
}
127131

132+
// Legacy codepath for versions that don't support the variant above.
133+
@SuppressWarnings("deprecation")
128134
@Override
129135
public void onReceivedError(
130136
WebView view, int errorCode, String description, String failingUrl) {
@@ -140,6 +146,8 @@ public boolean shouldOverrideUrlLoading(
140146
return returnValueForShouldOverrideUrlLoading;
141147
}
142148

149+
// Legacy codepath for < Lollipop; newer versions use the variant above.
150+
@SuppressWarnings("deprecation")
143151
@Override
144152
public boolean shouldOverrideUrlLoading(WebView view, String url) {
145153
flutterApi.urlLoading(this, view, url, reply -> {});

packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/CookieManagerHostApiImplTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public void setup() {
3333
when(cookieManager.hasCookies()).thenReturn(true);
3434
doAnswer(
3535
answer -> {
36-
((ValueCallback<Boolean>) answer.getArgument(0)).onReceiveValue(true);
36+
@SuppressWarnings("unchecked")
37+
ValueCallback<Boolean> callback = (ValueCallback<Boolean>) answer.getArgument(0);
38+
(callback).onReceiveValue(true);
3739
return null;
3840
})
3941
.when(cookieManager)
@@ -59,6 +61,7 @@ public void setCookieShouldCallSetCookie() {
5961
public void clearCookiesShouldCallRemoveAllCookiesOnAndroidLAbove() {
6062
// Setup
6163
TestUtils.setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.LOLLIPOP);
64+
@SuppressWarnings("unchecked")
6265
GeneratedAndroidWebView.Result<Boolean> result = mock(GeneratedAndroidWebView.Result.class);
6366
CookieManagerHostApiImpl impl = new CookieManagerHostApiImpl();
6467
// Run
@@ -69,9 +72,11 @@ public void clearCookiesShouldCallRemoveAllCookiesOnAndroidLAbove() {
6972
}
7073

7174
@Test
75+
@SuppressWarnings("deprecation")
7276
public void clearCookiesShouldCallRemoveAllCookieBelowAndroidL() {
7377
// Setup
7478
TestUtils.setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.KITKAT_WATCH);
79+
@SuppressWarnings("unchecked")
7580
GeneratedAndroidWebView.Result<Boolean> result = mock(GeneratedAndroidWebView.Result.class);
7681
CookieManagerHostApiImpl impl = new CookieManagerHostApiImpl();
7782
// Run

packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/JavaScriptChannelTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.mockito.Mockito.verify;
1010

1111
import android.os.Handler;
12+
import android.os.Looper;
1213
import io.flutter.plugins.webviewflutter.JavaScriptChannelHostApiImpl.JavaScriptChannelCreator;
1314
import org.junit.After;
1415
import org.junit.Before;
@@ -47,7 +48,10 @@ public JavaScriptChannel createJavaScriptChannel(
4748

4849
hostApiImpl =
4950
new JavaScriptChannelHostApiImpl(
50-
instanceManager, javaScriptChannelCreator, mockFlutterApi, new Handler());
51+
instanceManager,
52+
javaScriptChannelCreator,
53+
mockFlutterApi,
54+
new Handler(Looper.myLooper()));
5155
hostApiImpl.create(0L, "aChannelName");
5256
}
5357

packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/RegistrarFlutterAssetManagerTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import static org.mockito.Mockito.when;
1212

1313
import android.content.res.AssetManager;
14-
import io.flutter.plugin.common.PluginRegistry.Registrar;
15-
import io.flutter.plugins.webviewflutter.FlutterAssetManager.RegistrarFlutterAssetManager;
1614
import java.io.IOException;
1715
import org.junit.Before;
1816
import org.junit.Test;
@@ -21,17 +19,19 @@
2119
@SuppressWarnings("deprecation")
2220
public class RegistrarFlutterAssetManagerTest {
2321
@Mock AssetManager mockAssetManager;
24-
@Mock Registrar mockRegistrar;
22+
@Mock io.flutter.plugin.common.PluginRegistry.Registrar mockRegistrar;
2523

26-
RegistrarFlutterAssetManager testRegistrarFlutterAssetManager;
24+
io.flutter.plugins.webviewflutter.FlutterAssetManager.RegistrarFlutterAssetManager
25+
testRegistrarFlutterAssetManager;
2726

2827
@Before
2928
public void setUp() {
3029
mockAssetManager = mock(AssetManager.class);
31-
mockRegistrar = mock(Registrar.class);
30+
mockRegistrar = mock(io.flutter.plugin.common.PluginRegistry.Registrar.class);
3231

3332
testRegistrarFlutterAssetManager =
34-
new RegistrarFlutterAssetManager(mockAssetManager, mockRegistrar);
33+
new io.flutter.plugins.webviewflutter.FlutterAssetManager.RegistrarFlutterAssetManager(
34+
mockAssetManager, mockRegistrar);
3535
}
3636

3737
@Test

packages/webview_flutter/webview_flutter_android/example/android/build.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ task clean(type: Delete) {
3535
gradle.projectsEvaluated {
3636
project(":webview_flutter_android") {
3737
tasks.withType(JavaCompile) {
38-
// TODO(stuartmorgan): Enable this. See
39-
// https://github.com/flutter/flutter/issues/91868
40-
//options.compilerArgs << "-Xlint:all" << "-Werror"
38+
options.compilerArgs << "-Xlint:all" << "-Werror"
4139
}
4240
}
4341
}

packages/webview_flutter/webview_flutter_android/lib/src/android_webview.g.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Copyright 2013 The Flutter Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
// Autogenerated from Pigeon (v4.2.14), do not edit directly.
4+
// Autogenerated from Pigeon (v9.0.4), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import
7+
78
import 'dart:async';
89
import 'dart:typed_data' show Float64List, Int32List, Int64List, Uint8List;
910

@@ -297,7 +298,6 @@ class _WebViewHostApiCodec extends StandardMessageCodec {
297298
switch (type) {
298299
case 128:
299300
return WebViewPoint.decode(readValue(buffer)!);
300-
301301
default:
302302
return super.readValueOfType(type, buffer);
303303
}
@@ -1394,10 +1394,8 @@ class _WebViewClientFlutterApiCodec extends StandardMessageCodec {
13941394
switch (type) {
13951395
case 128:
13961396
return WebResourceErrorData.decode(readValue(buffer)!);
1397-
13981397
case 129:
13991398
return WebResourceRequestData.decode(readValue(buffer)!);
1400-
14011399
default:
14021400
return super.readValueOfType(type, buffer);
14031401
}
@@ -1935,7 +1933,6 @@ class _FileChooserParamsFlutterApiCodec extends StandardMessageCodec {
19351933
switch (type) {
19361934
case 128:
19371935
return FileChooserModeEnumData.decode(readValue(buffer)!);
1938-
19391936
default:
19401937
return super.readValueOfType(type, buffer);
19411938
}

packages/webview_flutter/webview_flutter_android/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ dev_dependencies:
2929
flutter_test:
3030
sdk: flutter
3131
mockito: ^5.3.2
32-
pigeon: ^4.2.14
32+
pigeon: ^9.0.4

packages/webview_flutter/webview_flutter_android/test/android_webview_controller_test.mocks.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,12 @@ class MockAndroidNavigationDelegate extends _i1.Mock
349349
class MockAndroidWebViewController extends _i1.Mock
350350
implements _i8.AndroidWebViewController {
351351
@override
352+
int get webViewIdentifier => (super.noSuchMethod(
353+
Invocation.getter(#webViewIdentifier),
354+
returnValue: 0,
355+
returnValueForMissingStub: 0,
356+
) as int);
357+
@override
352358
_i3.PlatformWebViewControllerCreationParams get params => (super.noSuchMethod(
353359
Invocation.getter(#params),
354360
returnValue: _FakePlatformWebViewControllerCreationParams_4(
@@ -650,11 +656,11 @@ class MockAndroidWebViewController extends _i1.Mock
650656
@override
651657
_i9.Future<void> setOnShowFileSelector(
652658
_i9.Future<List<String>> Function(_i8.FileSelectorParams)?
653-
onShowFileSelectorCallback) =>
659+
onShowFileSelector) =>
654660
(super.noSuchMethod(
655661
Invocation.method(
656662
#setOnShowFileSelector,
657-
[onShowFileSelectorCallback],
663+
[onShowFileSelector],
658664
),
659665
returnValue: _i9.Future<void>.value(),
660666
returnValueForMissingStub: _i9.Future<void>.value(),

packages/webview_flutter/webview_flutter_android/test/test_android_webview.g.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2013 The Flutter Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
// Autogenerated from Pigeon (v4.2.14), do not edit directly.
4+
// Autogenerated from Pigeon (v9.0.4), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, unnecessary_import
77
// ignore_for_file: avoid_relative_lib_imports
@@ -64,7 +64,6 @@ class _TestWebViewHostApiCodec extends StandardMessageCodec {
6464
switch (type) {
6565
case 128:
6666
return WebViewPoint.decode(readValue(buffer)!);
67-
6867
default:
6968
return super.readValueOfType(type, buffer);
7069
}

0 commit comments

Comments
 (0)