summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resources/chromium/contacts_manager_mock.js
blob: 049685242bc5a454d4d374224f8dd076de06f576 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import {ContactsManager, ContactsManagerReceiver} from '/gen/third_party/blink/public/mojom/contacts/contacts_manager.mojom.m.js';

self.WebContactsTest = (() => {
  class MockContacts {
    constructor() {
      this.receiver_ = new ContactsManagerReceiver(this);

      this.interceptor_ =
          new MojoInterfaceInterceptor(ContactsManager.$interfaceName);
      this.interceptor_.oninterfacerequest =
          e => this.receiver_.$.bindHandle(e.handle);
      this.interceptor_.start();

      this.selectedContacts_ = [];
    }

    formatAddress_(address) {
      // These are all required fields in the mojo definition.
      return {
        country: address.country || '',
        addressLine: address.addressLine || [],
        region: address.region || '',
        city: address.city || '',
        dependentLocality: address.dependentLocality || '',
        postalCode: address.postCode || '',
        sortingCode: address.sortingCode || '',
        organization: address.organization || '',
        recipient: address.recipient || '',
        phone: address.phone || '',
      };
    }

    async select(multiple, includeNames, includeEmails, includeTel, includeAddresses, includeIcons) {
      if (this.selectedContacts_ === null)
        return {contacts: null};

      const contactInfos = await Promise.all(this.selectedContacts_.map(async contact => {
        const contactInfo = {};
        if (includeNames)
          contactInfo.name = contact.name || [];
        if (includeEmails)
          contactInfo.email = contact.email || [];
        if (includeTel)
          contactInfo.tel = contact.tel || [];
        if (includeAddresses) {
          contactInfo.address = (contact.address || []).map(address => this.formatAddress_(address));
        }
        if (includeIcons) {
          contactInfo.icon = await Promise.all(
            (contact.icon || []).map(async blob => ({
              mimeType: blob.type,
              data: (await blob.text()).split('').map(s => s.charCodeAt(0)),
            })));
        }
        return contactInfo;
      }));

      if (!contactInfos.length) return {contacts: []};
      if (!multiple) return {contacts: [contactInfos[0]]};
      return {contacts: contactInfos};
    }

    setSelectedContacts(contacts) {
      this.selectedContacts_ = contacts;
    }

    reset() {
      this.receiver_.$.close();
      this.interceptor_.stop();
    }
  }

  const mockContacts = new MockContacts();

  class ContactsTestChromium {
    constructor() {
      Object.freeze(this); // Make it immutable.
    }

    setSelectedContacts(contacts) {
      mockContacts.setSelectedContacts(contacts);
    }
  }

  return ContactsTestChromium;
})();