Commit a99f125f authored by Chris Mumford's avatar Chris Mumford Committed by Commit Bot

Added Badging API Web Platform Tests.

The [Badging API](https://wicg.github.io/badging/) is a new API
allowing the setting of application/document badges. This change
converts the Badging API web tests into Web Platform Tests.

This change adds a no-op MockBadgeService to the content_shell.
This is necessary because the RenderProcessHostImpl will terminate
the render process if unknown Mojo messages are received.

Bug: 1051684
Change-Id: I61e29b8f197a754fea489ebb2d0dcd41484f6cf4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2088297Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarMike West <mkwst@chromium.org>
Reviewed-by: default avatarMatt Giuca <mgiuca@chromium.org>
Commit-Queue: Chris Mumford <cmumford@google.com>
Cr-Commit-Position: refs/heads/master@{#754676}
parent 4d7aa599
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
#include "content/shell/common/web_test/web_test_switches.h" #include "content/shell/common/web_test/web_test_switches.h"
#include "content/shell/renderer/web_test/blink_test_helpers.h" #include "content/shell/renderer/web_test/blink_test_helpers.h"
#include "content/test/data/mojo_web_test_helper_test.mojom.h" #include "content/test/data/mojo_web_test_helper_test.mojom.h"
#include "content/test/mock_badge_service.h"
#include "content/test/mock_clipboard_host.h" #include "content/test/mock_clipboard_host.h"
#include "content/test/mock_platform_notification_service.h" #include "content/test/mock_platform_notification_service.h"
#include "device/bluetooth/public/mojom/test/fake_bluetooth.mojom.h" #include "device/bluetooth/public/mojom/test/fake_bluetooth.mojom.h"
...@@ -376,6 +377,8 @@ void WebTestContentBrowserClient::RegisterBrowserInterfaceBindersForFrame( ...@@ -376,6 +377,8 @@ void WebTestContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
map->Add<mojom::MojoWebTestHelper>(base::BindRepeating(&BindWebTestHelper)); map->Add<mojom::MojoWebTestHelper>(base::BindRepeating(&BindWebTestHelper));
map->Add<blink::mojom::ClipboardHost>(base::BindRepeating( map->Add<blink::mojom::ClipboardHost>(base::BindRepeating(
&WebTestContentBrowserClient::BindClipboardHost, base::Unretained(this))); &WebTestContentBrowserClient::BindClipboardHost, base::Unretained(this)));
map->Add<blink::mojom::BadgeService>(base::BindRepeating(
&WebTestContentBrowserClient::BindBadgeService, base::Unretained(this)));
} }
bool WebTestContentBrowserClient::CanAcceptUntrustedExchangesIfNeeded() { bool WebTestContentBrowserClient::CanAcceptUntrustedExchangesIfNeeded() {
...@@ -414,6 +417,14 @@ void WebTestContentBrowserClient::BindClipboardHost( ...@@ -414,6 +417,14 @@ void WebTestContentBrowserClient::BindClipboardHost(
mock_clipboard_host_->Bind(std::move(receiver)); mock_clipboard_host_->Bind(std::move(receiver));
} }
void WebTestContentBrowserClient::BindBadgeService(
RenderFrameHost* render_frame_host,
mojo::PendingReceiver<blink::mojom::BadgeService> receiver) {
if (!mock_badge_service_)
mock_badge_service_ = std::make_unique<MockBadgeService>();
mock_badge_service_->Bind(std::move(receiver));
}
std::unique_ptr<LoginDelegate> WebTestContentBrowserClient::CreateLoginDelegate( std::unique_ptr<LoginDelegate> WebTestContentBrowserClient::CreateLoginDelegate(
const net::AuthChallengeInfo& auth_info, const net::AuthChallengeInfo& auth_info,
content::WebContents* web_contents, content::WebContents* web_contents,
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "mojo/public/cpp/bindings/pending_receiver.h" #include "mojo/public/cpp/bindings/pending_receiver.h"
#include "services/service_manager/public/cpp/binder_map.h" #include "services/service_manager/public/cpp/binder_map.h"
#include "services/service_manager/public/cpp/binder_registry.h" #include "services/service_manager/public/cpp/binder_registry.h"
#include "third_party/blink/public/mojom/badging/badging.mojom.h"
#include "third_party/blink/public/mojom/clipboard/clipboard.mojom.h" #include "third_party/blink/public/mojom/clipboard/clipboard.mojom.h"
#include "third_party/blink/public/mojom/permissions/permission_automation.mojom-forward.h" #include "third_party/blink/public/mojom/permissions/permission_automation.mojom-forward.h"
...@@ -37,6 +38,7 @@ namespace content { ...@@ -37,6 +38,7 @@ namespace content {
class FakeBluetoothChooser; class FakeBluetoothChooser;
class FakeBluetoothChooserFactory; class FakeBluetoothChooserFactory;
class FakeBluetoothDelegate; class FakeBluetoothDelegate;
class MockBadgeService;
class MockClipboardHost; class MockClipboardHost;
class MockPlatformNotificationService; class MockPlatformNotificationService;
class WebTestBrowserContext; class WebTestBrowserContext;
...@@ -123,6 +125,10 @@ class WebTestContentBrowserClient : public ShellContentBrowserClient { ...@@ -123,6 +125,10 @@ class WebTestContentBrowserClient : public ShellContentBrowserClient {
RenderFrameHost* render_frame_host, RenderFrameHost* render_frame_host,
mojo::PendingReceiver<blink::mojom::ClipboardHost> receiver); mojo::PendingReceiver<blink::mojom::ClipboardHost> receiver);
void BindBadgeService(
RenderFrameHost* render_frame_host,
mojo::PendingReceiver<blink::mojom::BadgeService> receiver);
void BindClientHintsControllerDelegate( void BindClientHintsControllerDelegate(
mojo::PendingReceiver<client_hints::mojom::ClientHints> receiver); mojo::PendingReceiver<client_hints::mojom::ClientHints> receiver);
...@@ -148,6 +154,7 @@ class WebTestContentBrowserClient : public ShellContentBrowserClient { ...@@ -148,6 +154,7 @@ class WebTestContentBrowserClient : public ShellContentBrowserClient {
std::unique_ptr<FakeBluetoothChooserFactory> fake_bluetooth_chooser_factory_; std::unique_ptr<FakeBluetoothChooserFactory> fake_bluetooth_chooser_factory_;
std::unique_ptr<FakeBluetoothDelegate> fake_bluetooth_delegate_; std::unique_ptr<FakeBluetoothDelegate> fake_bluetooth_delegate_;
std::unique_ptr<MockClipboardHost> mock_clipboard_host_; std::unique_ptr<MockClipboardHost> mock_clipboard_host_;
std::unique_ptr<MockBadgeService> mock_badge_service_;
}; };
} // namespace content } // namespace content
......
...@@ -267,6 +267,8 @@ jumbo_static_library("test_support") { ...@@ -267,6 +267,8 @@ jumbo_static_library("test_support") {
"io_thread_shared_url_loader_factory_owner.h", "io_thread_shared_url_loader_factory_owner.h",
"mock_background_sync_controller.cc", "mock_background_sync_controller.cc",
"mock_background_sync_controller.h", "mock_background_sync_controller.h",
"mock_badge_service.cc",
"mock_badge_service.h",
"mock_clipboard_host.cc", "mock_clipboard_host.cc",
"mock_clipboard_host.h", "mock_clipboard_host.h",
"mock_keyboard.cc", "mock_keyboard.cc",
......
...@@ -18,6 +18,9 @@ per-file mock_platform_notification_service.*=file://content/browser/notificatio ...@@ -18,6 +18,9 @@ per-file mock_platform_notification_service.*=file://content/browser/notificatio
# DirectWrite Font Code # DirectWrite Font Code
per-file dwrite_*=drott@chromium.org per-file dwrite_*=drott@chromium.org
# Badging related files.
per-file mock_badge_service.*=file://chrome/browser/badging/OWNERS
# Anyone can add rules to include new test files. # Anyone can add rules to include new test files.
per-file BUILD.gn=* per-file BUILD.gn=*
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/test/mock_badge_service.h"
#include <utility>
namespace content {
MockBadgeService::MockBadgeService() = default;
MockBadgeService::~MockBadgeService() = default;
void MockBadgeService::Bind(
mojo::PendingReceiver<blink::mojom::BadgeService> receiver) {
receivers_.Add(this, std::move(receiver));
}
void MockBadgeService::Reset() {}
void MockBadgeService::SetBadge(blink::mojom::BadgeValuePtr value) {}
void MockBadgeService::ClearBadge() {}
} // namespace content
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_TEST_MOCK_BADGE_SERVICE_H_
#define CONTENT_TEST_MOCK_BADGE_SERVICE_H_
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "third_party/blink/public/mojom/badging/badging.mojom.h"
namespace content {
// Implements a mock BadgeService. This implementation does nothing, but is
// required because inbound Mojo messages which do not have a registered
// handler are considered an error, and the render process is terminated.
//
// This is to support Web Platform Tests. Blink web tests do not require
// this as they use a JavaScript implementation of the BadgeService that
// intercepts the outbound Mojo calls before they leave the renderer.
class MockBadgeService : public blink::mojom::BadgeService {
public:
MockBadgeService();
MockBadgeService(const MockBadgeService&) = delete;
MockBadgeService& operator=(const MockBadgeService&) = delete;
~MockBadgeService() override;
void Bind(mojo::PendingReceiver<blink::mojom::BadgeService> receiver);
void Reset();
// blink::mojom::BadgeService:
void SetBadge(blink::mojom::BadgeValuePtr value) override;
void ClearBadge() override;
private:
mojo::ReceiverSet<blink::mojom::BadgeService> receivers_;
};
} // namespace content
#endif // CONTENT_TEST_MOCK_BADGE_SERVICE_H_
# Badging # Badging
This module contains the implementation of the [Badging API]. The implementation This module contains the implementation of the [Badging API].
is under [active development].
[Badging API]: https://github.com/WICG/badging [Badging API]: https://github.com/WICG/badging
[active development]: https://crbug.com/719176 [active development]: https://crbug.com/719176
...@@ -23,4 +22,10 @@ associated app's badge to |contents|. ...@@ -23,4 +22,10 @@ associated app's badge to |contents|.
### Testing ### Testing
`web_tests/badging/*.html` tests that the API accepts/rejects the appropriate `web_tests/badging/*.html` tests that the API accepts/rejects the appropriate
inputs (with a mock Mojo service). inputs (with a mock Mojo service that verifies that the interface sends the
correct Mojo messages". These tests duplicate some of the tests from WPT (below)
but with internal verification.
`web_tests/external/wpt/badging/*.html` tests that the API accepts/rejects the
appropriate inputs. These tests do not test the internal state of the badge
(because they have no visibility into it), only success/failure.
<!DOCTYPE html>
<html>
<head>
<title>Tests the values that the Badge API doesn't support.</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/third_party/blink/public/mojom/badging/badging.mojom.js"></script>
<script src="resources/mock-badge-service.js"></script>
</head>
<body>
<script>
// Negative value not allowed.
badge_test(() => navigator.setAppBadge(-1), undefined,
'TypeError');
// Value too large (2^53).
badge_test(() => navigator.setAppBadge(Number.MAX_SAFE_INTEGER + 1),
undefined,
'TypeError');
// Illegal numeric values.
badge_test(() => navigator.setAppBadge(Infinity), undefined,
'TypeError');
badge_test(() => navigator.setAppBadge(-Infinity), undefined,
'TypeError');
badge_test(() => navigator.setAppBadge(NaN), undefined,
'TypeError');
// Other values that can't convert to a long.
badge_test(() => navigator.setAppBadge("Foo"), undefined,
'TypeError');
badge_test(() => navigator.setAppBadge({}), undefined,
'TypeError');
</script>
</body>
</html>
<!DOCTYPE html>
<title>Badging: Unsupported values</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
promise_test(t => {
return promise_rejects_js(t, TypeError, navigator.setAppBadge(-1));
}, "Negative value not allowed");
promise_test(t => {
return promise_rejects_js(t, TypeError, navigator.setAppBadge(
Number.MAX_SAFE_INTEGER + 1));
}, "Value too large (2^53)");
promise_test(t => {
return promise_rejects_js(t, TypeError, navigator.setAppBadge(Infinity));
}, "Positive infinity");
promise_test(t => {
return promise_rejects_js(t, TypeError, navigator.setAppBadge(-Infinity));
}, "Negative infinity");
promise_test(t => {
return promise_rejects_js(t, TypeError, navigator.setAppBadge(NaN));
}, "NaN");
promise_test(t => {
return promise_rejects_js(t, TypeError, navigator.setAppBadge("Foo"));
}, 'Cannot convert to long: "Foo"');
promise_test(t => {
return promise_rejects_js(t, TypeError, navigator.setAppBadge({}));
}, "Cannot convert to long: object");
</script>
<!DOCTYPE html>
<title>Badging: Supported values</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
promise_test(async t => {
const result = await navigator.setAppBadge();
assert_equals(result, undefined);
}, "No parameter should show a flag with no numeric value.");
promise_test(async t => {
const result = await navigator.setAppBadge(undefined);
assert_equals(result, undefined);
}, "undefined should show a flag with no numeric value.");
promise_test(async t => {
const result = await navigator.setAppBadge(1);
assert_equals(result, undefined);
}, "An integer value of 3 should show the badge vale 3.");
promise_test(async t => {
const result = await navigator.setAppBadge(10.6);
assert_equals(result, undefined);
}, "Non-whole number should round down to nearest integer (10).");
promise_test(async t => {
const result = await navigator.setAppBadge(Number.MAX_SAFE_INTEGER);
assert_equals(result, undefined);
}, "Maximum allowed value (2^53 - 1) should display saturated value: '99+'.");
promise_test(async t => {
const result = await navigator.setAppBadge(0);
assert_equals(result, undefined);
}, "Set to zero should clear the badge.");
promise_test(async t => {
const result = await navigator.clearAppBadge();
assert_equals(result, undefined);
}, "Should clear the badge.");
promise_test(async t => {
const result = await navigator.setAppBadge(null);
assert_equals(result, undefined);
}, "Setting to null should clear the badge.");
promise_test(async t => {
const result = await navigator.setAppBadge(false);
assert_equals(result, undefined);
}, "Setting to false should clear the badge.");
promise_test(async t => {
const result = await navigator.setAppBadge(true);
assert_equals(result, undefined);
}, "Setting to true should display a value of 1.");
promise_test(async t => {
const result = await navigator.setAppBadge("3");
assert_equals(result, undefined);
}, "Setting to the string '3' should display a value of 3.");
</script>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment