Commit 2ebc5d47 authored by Peter Beverloo's avatar Peter Beverloo Committed by Commit Bot

Add more extensive testing to the Contact API

Create a mock Mojo interface through which individual test cases can
modify behaviour of the call. This enables us to extensively test the
Web Exposed behaviour.

Bug: 860467
Change-Id: I90da5fb37e600466e488c49ef52b3d14b98d262f
Reviewed-on: https://chromium-review.googlesource.com/c/1431512
Commit-Queue: Peter Beverloo <peter@chromium.org>
Reviewed-by: default avatarFinnur Thorarinsson <finnur@chromium.org>
Cr-Commit-Position: refs/heads/master@{#625673}
parent 59cc57db
'use strict';
// Mock implementation of the blink.mojom.ContactsManager interface.
class MockContactsManager {
constructor() {
this.selectCallback_ = null;
this.binding_ = new mojo.Binding(blink.mojom.ContactsManager, this);
this.interceptor_ = new MojoInterfaceInterceptor(
blink.mojom.ContactsManager.name);
this.interceptor_.oninterfacerequest = e => this.binding_.bind(e.handle);
this.interceptor_.start();
}
// Sets |callback| to be invoked when the ContactsManager.Select() method is
// being called over the Mojo connection.
setSelectCallback(callback) {
this.selectCallback_ = callback;
}
async select(multiple, includeNames, includeEmails, includeTel) {
if (this.selectCallback_) {
return this.selectCallback_(
{ multiple, includeNames, includeEmails, includeTel });
}
return { contacts: null };
}
}
const mockContactsManager = new MockContactsManager();
<!doctype html>
<meta charset="utf-8">
<title>Contact API: Behaviour of the select() function</title>
<script src="/gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="/gen/third_party/blink/public/mojom/contacts/contacts_manager.mojom.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/mock_contacts_manager.js"></script>
<script>
'use strict';
......@@ -64,11 +67,117 @@ promise_test(async () => {
promise_test(async () => {
triggerUserGesture();
// TODO(peter): Fake the Mojo interface so that we can extend this test with
// valid behaviour, and actually verify the API's functionality.
// Returns a NULL result, indicating that no results are available.
mockContactsManager.setSelectCallback(async (options) => {
return { contacts: null };
});
await expectTypeError(() =>
navigator.contacts.select({ properties: ['name'] }));
}, 'The Contact API can fail when the selector cannot be opened');
promise_test(async () => {
triggerUserGesture();
let storedOptions = null;
// Stores the |options| in |storedOptions| for inspection after the call
// completes. Deliberately fails the call with no data.
mockContactsManager.setSelectCallback(async (options) => {
storedOptions = options;
return { contacts: null };
});
await expectTypeError(() => {
return navigator.contacts.select({
properties: ['name', 'email'],
multiple: true
});
});
assert_not_equals(storedOptions, null);
assert_true(storedOptions.multiple);
assert_true(storedOptions.includeNames);
assert_true(storedOptions.includeEmails);
assert_false(storedOptions.includeTel);
}, 'The Contact API correctly interprets the JavaScript arguments');
promise_test(async () => {
triggerUserGesture();
// Returns two contacts with all information available.
mockContactsManager.setSelectCallback(async (options) => {
return {
contacts: [
{ name: ['Finnur'], email: ['finnur@chromium.org'], tel: ['000-0000'] },
{ name: ['Peter', 'Test'], email: ['peter@chromium.org'], tel: [] },
]
};
});
const results = await navigator.contacts.select({
properties: ['name', 'email', 'tel'],
multiple: true
});
assert_equals(results.length, 2);
{
const finnur = results[0];
assert_own_property(finnur, 'name');
assert_own_property(finnur, 'email');
assert_own_property(finnur, 'tel');
assert_array_equals(finnur.name, ['Finnur']);
assert_array_equals(finnur.email, ['finnur@chromium.org']);
assert_array_equals(finnur.tel, ['000-0000']);
}
{
const peter = results[1];
assert_own_property(peter, 'name');
assert_own_property(peter, 'email');
assert_own_property(peter, 'tel');
assert_array_equals(peter.name, ['Peter', 'Test']);
assert_array_equals(peter.email, ['peter@chromium.org']);
assert_array_equals(peter.tel, []);
}
}, 'The Contact API correctly returns ContactInfo entries');
promise_test(async () => {
triggerUserGesture();
// Returns partial information since no e-mail addresses are requested.
mockContactsManager.setSelectCallback(async (options) => {
return {
contacts: [{ name: ['Tim'], email: null, tel: null }]
};
});
const results = await navigator.contacts.select({
properties: ['name']
});
assert_equals(results.length, 1);
{
const tim = results[0];
assert_own_property(tim, 'name');
assert_false('email' in tim);
assert_false('tel' in tim);
assert_array_equals(tim.name, ['Tim']);
assert_equals(tim.email, undefined);
assert_equals(tim.tel, undefined);
}
}, 'The Contact API does not include fields that were not requested');
</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