Skip to content

Commit 8fa5fe1

Browse files
committed
Add native unit tests for payment request feature
1 parent d098b47 commit 8fa5fe1

File tree

5 files changed

+206
-0
lines changed

5 files changed

+206
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
package io.flutter.plugins.webviewflutter;
6+
7+
import static org.junit.Assert.fail;
8+
import static org.mockito.Mockito.mock;
9+
import static org.mockito.Mockito.mockStatic;
10+
11+
import android.webkit.WebSettings;
12+
13+
import androidx.webkit.WebSettingsCompat;
14+
15+
import org.junit.Test;
16+
import org.mockito.MockedStatic;
17+
18+
public class WebSettingsCompatTest {
19+
@Test
20+
public void setPaymentRequestEnabled() {
21+
final PigeonApiWebSettingsCompat api = new TestProxyApiRegistrar().getPigeonApiWebSettingsCompat();
22+
23+
final WebSettings webSettings = mock(WebSettings.class);
24+
try (MockedStatic<WebSettingsCompat> mockedStatic = mockStatic(WebSettingsCompat.class)) {
25+
api.setPaymentRequestEnabled(webSettings, true);
26+
mockedStatic.verify(() -> WebSettingsCompat.setPaymentRequestEnabled(webSettings, true));
27+
} catch (Exception e) {
28+
fail(e.toString());
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
package io.flutter.plugins.webviewflutter;
6+
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertTrue;
9+
import static org.junit.Assert.fail;
10+
import static org.mockito.Mockito.mockStatic;
11+
12+
import androidx.webkit.WebViewFeature;
13+
14+
import org.junit.Test;
15+
import org.mockito.MockedStatic;
16+
17+
public class WebViewFeatureTest {
18+
@Test
19+
public void isFeatureSupported() {
20+
final PigeonApiWebViewFeature api = new TestProxyApiRegistrar().getPigeonApiWebViewFeature();
21+
22+
try (MockedStatic<WebViewFeature> mockedStatic = mockStatic(WebViewFeature.class)) {
23+
mockedStatic.when(() -> WebViewFeature.isFeatureSupported("PAYMENT_REQUEST")).thenReturn(true);
24+
25+
boolean result = api.isFeatureSupported("PAYMENT_REQUEST");
26+
27+
assertTrue(result);
28+
29+
mockedStatic.verify(() -> WebViewFeature.isFeatureSupported("PAYMENT_REQUEST"));
30+
} catch (Exception e) {
31+
fail(e.toString());
32+
}
33+
}
34+
35+
@Test
36+
public void isFeatureSupportedReturnsFalse() {
37+
final PigeonApiWebViewFeature api = new TestProxyApiRegistrar().getPigeonApiWebViewFeature();
38+
39+
try (MockedStatic<WebViewFeature> mockedStatic = mockStatic(WebViewFeature.class)) {
40+
mockedStatic.when(() -> WebViewFeature.isFeatureSupported("UNSUPPORTED_FEATURE")).thenReturn(false);
41+
42+
boolean result = api.isFeatureSupported("UNSUPPORTED_FEATURE");
43+
44+
assertFalse(result);
45+
46+
mockedStatic.verify(() -> WebViewFeature.isFeatureSupported("UNSUPPORTED_FEATURE"));
47+
} catch (Exception e) {
48+
fail(e.toString());
49+
}
50+
}
51+
}

packages/webview_flutter/webview_flutter_android/test/android_webview_controller_test.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ void main() {
8989
android_webview.WebViewClient? mockWebViewClient,
9090
android_webview.WebStorage? mockWebStorage,
9191
android_webview.WebSettings? mockSettings,
92+
Future<bool> Function(String)? isWebViewFeatureSupported,
93+
Future<void> Function(android_webview.WebSettings, bool)?
94+
setPaymentRequestEnabled,
9295
}) {
9396
final android_webview.WebView nonNullMockWebView =
9497
mockWebView ?? MockWebView();
@@ -247,6 +250,10 @@ void main() {
247250
postMessage,
248251
}) =>
249252
mockJavaScriptChannel ?? MockJavaScriptChannel(),
253+
isWebViewFeatureSupported:
254+
isWebViewFeatureSupported ?? (_) async => false,
255+
setPaymentRequestEnabled:
256+
setPaymentRequestEnabled ?? (_, __) async {},
250257
));
251258

252259
when(nonNullMockWebView.settings)
@@ -1710,6 +1717,48 @@ void main() {
17101717
expect(controller.webViewIdentifier, 0);
17111718
});
17121719

1720+
test('isWebViewFeatureSupported', () async {
1721+
String? captured;
1722+
const bool expectedIsWebViewFeatureEnabled = true;
1723+
1724+
final AndroidWebViewController controller = createControllerWithMocks(
1725+
isWebViewFeatureSupported: (String feature) async {
1726+
captured = feature;
1727+
return expectedIsWebViewFeatureEnabled;
1728+
},
1729+
);
1730+
1731+
final bool result = await controller.isWebViewFeatureSupported(
1732+
WebViewFeatureType.paymentRequest,
1733+
);
1734+
1735+
expect(WebViewFeatureConstants.paymentRequest, captured);
1736+
expect(expectedIsWebViewFeatureEnabled, result);
1737+
});
1738+
1739+
test('setPaymentRequestEnabled', () async {
1740+
android_webview.WebSettings? capturedSettings;
1741+
bool? capturedEnabled;
1742+
const bool expectedEnabled = true;
1743+
1744+
final MockWebView mockWebView = MockWebView();
1745+
final MockWebSettings mockSettings = MockWebSettings();
1746+
final AndroidWebViewController controller = createControllerWithMocks(
1747+
mockWebView: mockWebView,
1748+
mockSettings: mockSettings,
1749+
setPaymentRequestEnabled:
1750+
(android_webview.WebSettings settings, bool enabled) async {
1751+
capturedSettings = settings;
1752+
capturedEnabled = enabled;
1753+
},
1754+
);
1755+
1756+
await controller.setPaymentRequestEnabled(expectedEnabled);
1757+
1758+
expect(mockSettings, capturedSettings);
1759+
expect(expectedEnabled, capturedEnabled);
1760+
});
1761+
17131762
group('AndroidWebViewWidget', () {
17141763
testWidgets('Builds Android view using supplied parameters',
17151764
(WidgetTester tester) async {

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,29 @@ class MockAndroidWebViewController extends _i1.Mock
10181018
returnValue: _i8.Future<void>.value(),
10191019
returnValueForMissingStub: _i8.Future<void>.value(),
10201020
) as _i8.Future<void>);
1021+
1022+
@override
1023+
_i8.Future<bool> isWebViewFeatureSupported(
1024+
_i7.WebViewFeatureType? featureType) =>
1025+
(super.noSuchMethod(
1026+
Invocation.method(
1027+
#isWebViewFeatureSupported,
1028+
[featureType],
1029+
),
1030+
returnValue: _i8.Future<bool>.value(false),
1031+
returnValueForMissingStub: _i8.Future<bool>.value(false),
1032+
) as _i8.Future<bool>);
1033+
1034+
@override
1035+
_i8.Future<void> setPaymentRequestEnabled(bool? enabled) =>
1036+
(super.noSuchMethod(
1037+
Invocation.method(
1038+
#setPaymentRequestEnabled,
1039+
[enabled],
1040+
),
1041+
returnValue: _i8.Future<void>.value(),
1042+
returnValueForMissingStub: _i8.Future<void>.value(),
1043+
) as _i8.Future<void>);
10211044
}
10221045

10231046
/// A class which mocks [AndroidWebViewProxy].
@@ -1829,6 +1852,36 @@ class MockAndroidWebViewProxy extends _i1.Mock
18291852
Invocation.getter(#instanceWebStorage),
18301853
),
18311854
) as _i2.WebStorage Function());
1855+
1856+
@override
1857+
_i8.Future<bool> Function(String) get isWebViewFeatureSupported =>
1858+
(super.noSuchMethod(
1859+
Invocation.getter(#isWebViewFeatureSupported),
1860+
returnValue: (String __p0) => _i8.Future<bool>.value(false),
1861+
returnValueForMissingStub: (String __p0) =>
1862+
_i8.Future<bool>.value(false),
1863+
) as _i8.Future<bool> Function(String));
1864+
1865+
@override
1866+
_i8.Future<void> Function(
1867+
_i2.WebSettings,
1868+
bool,
1869+
) get setPaymentRequestEnabled => (super.noSuchMethod(
1870+
Invocation.getter(#setPaymentRequestEnabled),
1871+
returnValue: (
1872+
_i2.WebSettings __p0,
1873+
bool __p1,
1874+
) =>
1875+
_i8.Future<void>.value(),
1876+
returnValueForMissingStub: (
1877+
_i2.WebSettings __p0,
1878+
bool __p1,
1879+
) =>
1880+
_i8.Future<void>.value(),
1881+
) as _i8.Future<void> Function(
1882+
_i2.WebSettings,
1883+
bool,
1884+
));
18321885
}
18331886

18341887
/// A class which mocks [AndroidWebViewWidgetCreationParams].

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,4 +709,26 @@ class MockAndroidWebViewController extends _i1.Mock
709709
returnValue: _i5.Future<void>.value(),
710710
returnValueForMissingStub: _i5.Future<void>.value(),
711711
) as _i5.Future<void>);
712+
713+
@override
714+
_i5.Future<bool> isWebViewFeatureSupported(
715+
_i6.WebViewFeatureType? featureType) =>
716+
(super.noSuchMethod(
717+
Invocation.method(
718+
#isWebViewFeatureSupported,
719+
[featureType],
720+
),
721+
returnValue: _i5.Future<bool>.value(false),
722+
) as _i5.Future<bool>);
723+
724+
@override
725+
_i5.Future<void> setPaymentRequestEnabled(bool? enabled) =>
726+
(super.noSuchMethod(
727+
Invocation.method(
728+
#setPaymentRequestEnabled,
729+
[enabled],
730+
),
731+
returnValue: _i5.Future<void>.value(),
732+
returnValueForMissingStub: _i5.Future<void>.value(),
733+
) as _i5.Future<void>);
712734
}

0 commit comments

Comments
 (0)