-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[webview_flutter] Add support for payment requests on Android #9679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
2d07746
1a01f13
d098b47
8fa5fe1
381ae6f
5eee7b3
d43bde1
a20f0fb
103e29f
8c73517
5ed43ab
28f8889
3f68c0e
878fd38
b0388bc
5f7f689
ddea6d4
2b06e3d
69c8a3e
7337199
47b6b6f
1cb2a20
5575248
8ab4499
74e8aa4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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. | ||
|
||
package io.flutter.plugins.webviewflutter; | ||
|
||
import android.webkit.WebSettings; | ||
import androidx.annotation.NonNull; | ||
import androidx.webkit.WebSettingsCompat; | ||
import androidx.webkit.WebViewFeature; | ||
|
||
/** | ||
* Host api implementation for {@link WebSettingsCompat}. | ||
* | ||
* <p>Handles static methods for {@link WebSettingsCompat}. | ||
*/ | ||
public class WebSettingsCompatProxyApi extends PigeonApiWebSettingsCompat { | ||
public WebSettingsCompatProxyApi(@NonNull ProxyApiRegistrar pigeonRegistrar) { | ||
super(pigeonRegistrar); | ||
} | ||
|
||
@Override | ||
public void setPaymentRequestEnabled(@NonNull WebSettings webSettings, boolean enabled) { | ||
if (WebViewFeature.isFeatureSupported(WebViewFeature.PAYMENT_REQUEST)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this check being done internally on the native side, rather than being a straight pass-through? I would expect this to work the same way the SDK works, which is that clients are responsible for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stuartmorgan-g This was to address Android lint's RequiresFeature, but as you pointed out, since it's natural to use it after checking There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added a section about the payment request API usage to the README. |
||
WebSettingsCompat.setPaymentRequestEnabled(webSettings, enabled); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,24 @@ | ||||||||||||||||||||||||
// 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. | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
package io.flutter.plugins.webviewflutter; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
import androidx.annotation.NonNull; | ||||||||||||||||||||||||
import androidx.webkit.WebViewFeature; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* Host api implementation for {@link WebViewFeature}. | ||||||||||||||||||||||||
* | ||||||||||||||||||||||||
* <p>Handles creating {@link WebViewFeature}s that intercommunicate with a paired Dart object. | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for here:
Suggested change
|
||||||||||||||||||||||||
public class WebViewFeatureProxyApi extends PigeonApiWebViewFeature { | ||||||||||||||||||||||||
public WebViewFeatureProxyApi(@NonNull ProxyApiRegistrar pigeonRegistrar) { | ||||||||||||||||||||||||
super(pigeonRegistrar); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||
public boolean isFeatureSupported(@NonNull String feature) { | ||||||||||||||||||||||||
return WebViewFeature.isFeatureSupported(feature); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// 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. | ||
|
||
package io.flutter.plugins.webviewflutter; | ||
|
||
import static org.junit.Assert.fail; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.never; | ||
|
||
import android.webkit.WebSettings; | ||
import androidx.webkit.WebSettingsCompat; | ||
import androidx.webkit.WebViewFeature; | ||
import org.junit.Test; | ||
import org.mockito.MockedStatic; | ||
|
||
public class WebSettingsCompatTest { | ||
@Test | ||
public void setPaymentRequestEnabled_PAYMENT_REQUEST_is_supported() { | ||
final PigeonApiWebSettingsCompat api = | ||
new TestProxyApiRegistrar().getPigeonApiWebSettingsCompat(); | ||
|
||
final WebSettings webSettings = mock(WebSettings.class); | ||
|
||
try (MockedStatic<WebSettingsCompat> mockedStatic = mockStatic(WebSettingsCompat.class)) { | ||
try (MockedStatic<WebViewFeature> mockedWebViewFeature = mockStatic(WebViewFeature.class)) { | ||
mockedWebViewFeature | ||
.when(() -> WebViewFeature.isFeatureSupported(WebViewFeature.PAYMENT_REQUEST)) | ||
.thenReturn(true); | ||
api.setPaymentRequestEnabled(webSettings, true); | ||
mockedStatic.verify(() -> WebSettingsCompat.setPaymentRequestEnabled(webSettings, true)); | ||
} catch (Exception e) { | ||
fail(e.toString()); | ||
} | ||
} catch (Exception e) { | ||
fail(e.toString()); | ||
} | ||
} | ||
|
||
@Test | ||
public void setPaymentRequestEnabled_PAYMENT_REQUEST_is_NOT_supported() { | ||
final PigeonApiWebSettingsCompat api = | ||
new TestProxyApiRegistrar().getPigeonApiWebSettingsCompat(); | ||
|
||
final WebSettings webSettings = mock(WebSettings.class); | ||
|
||
try (MockedStatic<WebSettingsCompat> mockedStatic = mockStatic(WebSettingsCompat.class)) { | ||
try (MockedStatic<WebViewFeature> mockedWebViewFeature = mockStatic(WebViewFeature.class)) { | ||
mockedWebViewFeature | ||
.when(() -> WebViewFeature.isFeatureSupported(WebViewFeature.PAYMENT_REQUEST)) | ||
.thenReturn(false); | ||
api.setPaymentRequestEnabled(webSettings, true); | ||
mockedStatic.verify( | ||
() -> WebSettingsCompat.setPaymentRequestEnabled(webSettings, true), never()); | ||
} catch (Exception e) { | ||
fail(e.toString()); | ||
} | ||
} catch (Exception e) { | ||
fail(e.toString()); | ||
} | ||
} | ||
} |
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. | ||
|
||
package io.flutter.plugins.webviewflutter; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
import static org.mockito.Mockito.mockStatic; | ||
|
||
import androidx.webkit.WebViewFeature; | ||
import org.junit.Test; | ||
import org.mockito.MockedStatic; | ||
|
||
public class WebViewFeatureTest { | ||
@Test | ||
public void isFeatureSupported() { | ||
final PigeonApiWebViewFeature api = new TestProxyApiRegistrar().getPigeonApiWebViewFeature(); | ||
|
||
try (MockedStatic<WebViewFeature> mockedStatic = mockStatic(WebViewFeature.class)) { | ||
mockedStatic | ||
.when(() -> WebViewFeature.isFeatureSupported("PAYMENT_REQUEST")) | ||
.thenReturn(true); | ||
|
||
boolean result = api.isFeatureSupported("PAYMENT_REQUEST"); | ||
|
||
assertTrue(result); | ||
|
||
mockedStatic.verify(() -> WebViewFeature.isFeatureSupported("PAYMENT_REQUEST")); | ||
} catch (Exception e) { | ||
fail(e.toString()); | ||
} | ||
} | ||
|
||
@Test | ||
public void isFeatureSupportedReturnsFalse() { | ||
final PigeonApiWebViewFeature api = new TestProxyApiRegistrar().getPigeonApiWebViewFeature(); | ||
|
||
try (MockedStatic<WebViewFeature> mockedStatic = mockStatic(WebViewFeature.class)) { | ||
mockedStatic | ||
.when(() -> WebViewFeature.isFeatureSupported("UNSUPPORTED_FEATURE")) | ||
.thenReturn(false); | ||
|
||
boolean result = api.isFeatureSupported("UNSUPPORTED_FEATURE"); | ||
|
||
assertFalse(result); | ||
|
||
mockedStatic.verify(() -> WebViewFeature.isFeatureSupported("UNSUPPORTED_FEATURE")); | ||
} catch (Exception e) { | ||
fail(e.toString()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this was copied from the other api implementations which are actually incorrect. You can change this to: