Skip to content

Commit

Permalink
[webview_flutter] Enable warnings-as-errors on Android (flutter#3356)
Browse files Browse the repository at this point in the history
[webview_flutter] Enable warnings-as-errors on Android
  • Loading branch information
stuartmorgan authored and nploi committed Jul 16, 2023
1 parent a3cbb20 commit 825b051
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 130 deletions.
@@ -1,5 +1,6 @@
## 3.3.2

* Resolves compilations warnings.
* Updates compileSdkVersion to 33.
* Bumps androidx.webkit:webkit from 1.5.0 to 1.6.0.

Expand Down
Expand Up @@ -14,16 +14,28 @@ public void clearCookies(GeneratedAndroidWebView.Result<Boolean> result) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeAllCookies(result::success);
} else {
final boolean hasCookies = cookieManager.hasCookies();
if (hasCookies) {
cookieManager.removeAllCookie();
}
result.success(hasCookies);
result.success(removeCookiesPreL(cookieManager));
}
}

@Override
public void setCookie(String url, String value) {
CookieManager.getInstance().setCookie(url, value);
}

/**
* Removes all cookies from the given cookie manager, using the deprecated (pre-Lollipop)
* implementation.
*
* @param cookieManager The cookie manager to clear all cookies from.
* @return Whether any cookies were removed.
*/
@SuppressWarnings("deprecation")
private boolean removeCookiesPreL(CookieManager cookieManager) {
final boolean hasCookies = cookieManager.hasCookies();
if (hasCookies) {
cookieManager.removeAllCookie();
}
return hasCookies;
}
}

Large diffs are not rendered by default.

Expand Up @@ -130,6 +130,8 @@ public boolean shouldOverrideUrlLoading(
return true;
}

// Legacy codepath for < N.
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView windowWebView, String url) {
if (!webViewClient.shouldOverrideUrlLoading(view, url)) {
Expand Down
Expand Up @@ -59,6 +59,8 @@ public void onReceivedError(WebView view, WebResourceRequest request, WebResourc
flutterApi.onReceivedRequestError(this, view, request, error, reply -> {});
}

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

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

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

// Legacy codepath for < Lollipop; newer versions use the variant above.
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
flutterApi.urlLoading(this, view, url, reply -> {});
Expand Down
Expand Up @@ -33,7 +33,9 @@ public void setup() {
when(cookieManager.hasCookies()).thenReturn(true);
doAnswer(
answer -> {
((ValueCallback<Boolean>) answer.getArgument(0)).onReceiveValue(true);
@SuppressWarnings("unchecked")
ValueCallback<Boolean> callback = (ValueCallback<Boolean>) answer.getArgument(0);
(callback).onReceiveValue(true);
return null;
})
.when(cookieManager)
Expand All @@ -59,6 +61,7 @@ public void setCookieShouldCallSetCookie() {
public void clearCookiesShouldCallRemoveAllCookiesOnAndroidLAbove() {
// Setup
TestUtils.setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.LOLLIPOP);
@SuppressWarnings("unchecked")
GeneratedAndroidWebView.Result<Boolean> result = mock(GeneratedAndroidWebView.Result.class);
CookieManagerHostApiImpl impl = new CookieManagerHostApiImpl();
// Run
Expand All @@ -69,9 +72,11 @@ public void clearCookiesShouldCallRemoveAllCookiesOnAndroidLAbove() {
}

@Test
@SuppressWarnings("deprecation")
public void clearCookiesShouldCallRemoveAllCookieBelowAndroidL() {
// Setup
TestUtils.setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.KITKAT_WATCH);
@SuppressWarnings("unchecked")
GeneratedAndroidWebView.Result<Boolean> result = mock(GeneratedAndroidWebView.Result.class);
CookieManagerHostApiImpl impl = new CookieManagerHostApiImpl();
// Run
Expand Down
Expand Up @@ -9,6 +9,7 @@
import static org.mockito.Mockito.verify;

import android.os.Handler;
import android.os.Looper;
import io.flutter.plugins.webviewflutter.JavaScriptChannelHostApiImpl.JavaScriptChannelCreator;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -47,7 +48,10 @@ public JavaScriptChannel createJavaScriptChannel(

hostApiImpl =
new JavaScriptChannelHostApiImpl(
instanceManager, javaScriptChannelCreator, mockFlutterApi, new Handler());
instanceManager,
javaScriptChannelCreator,
mockFlutterApi,
new Handler(Looper.myLooper()));
hostApiImpl.create(0L, "aChannelName");
}

Expand Down
Expand Up @@ -11,8 +11,6 @@
import static org.mockito.Mockito.when;

import android.content.res.AssetManager;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugins.webviewflutter.FlutterAssetManager.RegistrarFlutterAssetManager;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -21,17 +19,19 @@
@SuppressWarnings("deprecation")
public class RegistrarFlutterAssetManagerTest {
@Mock AssetManager mockAssetManager;
@Mock Registrar mockRegistrar;
@Mock io.flutter.plugin.common.PluginRegistry.Registrar mockRegistrar;

RegistrarFlutterAssetManager testRegistrarFlutterAssetManager;
io.flutter.plugins.webviewflutter.FlutterAssetManager.RegistrarFlutterAssetManager
testRegistrarFlutterAssetManager;

@Before
public void setUp() {
mockAssetManager = mock(AssetManager.class);
mockRegistrar = mock(Registrar.class);
mockRegistrar = mock(io.flutter.plugin.common.PluginRegistry.Registrar.class);

testRegistrarFlutterAssetManager =
new RegistrarFlutterAssetManager(mockAssetManager, mockRegistrar);
new io.flutter.plugins.webviewflutter.FlutterAssetManager.RegistrarFlutterAssetManager(
mockAssetManager, mockRegistrar);
}

@Test
Expand Down
Expand Up @@ -35,9 +35,7 @@ task clean(type: Delete) {
gradle.projectsEvaluated {
project(":webview_flutter_android") {
tasks.withType(JavaCompile) {
// TODO(stuartmorgan): Enable this. See
// https://github.com/flutter/flutter/issues/91868
//options.compilerArgs << "-Xlint:all" << "-Werror"
options.compilerArgs << "-Xlint:all" << "-Werror"
}
}
}
@@ -1,9 +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.
// Autogenerated from Pigeon (v4.2.14), do not edit directly.
// Autogenerated from Pigeon (v9.0.4), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// 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

import 'dart:async';
import 'dart:typed_data' show Float64List, Int32List, Int64List, Uint8List;

Expand Down Expand Up @@ -297,7 +298,6 @@ class _WebViewHostApiCodec extends StandardMessageCodec {
switch (type) {
case 128:
return WebViewPoint.decode(readValue(buffer)!);

default:
return super.readValueOfType(type, buffer);
}
Expand Down Expand Up @@ -1394,10 +1394,8 @@ class _WebViewClientFlutterApiCodec extends StandardMessageCodec {
switch (type) {
case 128:
return WebResourceErrorData.decode(readValue(buffer)!);

case 129:
return WebResourceRequestData.decode(readValue(buffer)!);

default:
return super.readValueOfType(type, buffer);
}
Expand Down Expand Up @@ -1935,7 +1933,6 @@ class _FileChooserParamsFlutterApiCodec extends StandardMessageCodec {
switch (type) {
case 128:
return FileChooserModeEnumData.decode(readValue(buffer)!);

default:
return super.readValueOfType(type, buffer);
}
Expand Down
Expand Up @@ -29,4 +29,4 @@ dev_dependencies:
flutter_test:
sdk: flutter
mockito: ^5.3.2
pigeon: ^4.2.14
pigeon: ^9.0.4
Expand Up @@ -349,6 +349,12 @@ class MockAndroidNavigationDelegate extends _i1.Mock
class MockAndroidWebViewController extends _i1.Mock
implements _i8.AndroidWebViewController {
@override
int get webViewIdentifier => (super.noSuchMethod(
Invocation.getter(#webViewIdentifier),
returnValue: 0,
returnValueForMissingStub: 0,
) as int);
@override
_i3.PlatformWebViewControllerCreationParams get params => (super.noSuchMethod(
Invocation.getter(#params),
returnValue: _FakePlatformWebViewControllerCreationParams_4(
Expand Down Expand Up @@ -650,11 +656,11 @@ class MockAndroidWebViewController extends _i1.Mock
@override
_i9.Future<void> setOnShowFileSelector(
_i9.Future<List<String>> Function(_i8.FileSelectorParams)?
onShowFileSelectorCallback) =>
onShowFileSelector) =>
(super.noSuchMethod(
Invocation.method(
#setOnShowFileSelector,
[onShowFileSelectorCallback],
[onShowFileSelector],
),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
Expand Down
@@ -1,7 +1,7 @@
// 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.
// Autogenerated from Pigeon (v4.2.14), do not edit directly.
// Autogenerated from Pigeon (v9.0.4), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, unnecessary_import
// ignore_for_file: avoid_relative_lib_imports
Expand Down Expand Up @@ -64,7 +64,6 @@ class _TestWebViewHostApiCodec extends StandardMessageCodec {
switch (type) {
case 128:
return WebViewPoint.decode(readValue(buffer)!);

default:
return super.readValueOfType(type, buffer);
}
Expand Down

0 comments on commit 825b051

Please sign in to comment.