summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/xpcshell/test_DomainCache.js
blob: 9ea53ccdd9e22f772efbfe14d700bb3006aaf2f7 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { Domain } = ChromeUtils.importESModule(
  "chrome://remote/content/cdp/domains/Domain.sys.mjs"
);
const { DomainCache } = ChromeUtils.importESModule(
  "chrome://remote/content/cdp/domains/DomainCache.sys.mjs"
);

class MockSession {
  onEvent() {}
}

const noopSession = new MockSession();

add_task(function test_DomainCache_constructor() {
  new DomainCache(noopSession, {});
});

add_task(function test_DomainCache_domainSupportsMethod() {
  const modules = {
    Foo: class extends Domain {
      bar() {}
    },
  };
  const domains = new DomainCache(noopSession, modules);

  ok(domains.domainSupportsMethod("Foo", "bar"));
  ok(!domains.domainSupportsMethod("Foo", "baz"));
  ok(!domains.domainSupportsMethod("foo", "bar"));
});

add_task(function test_DomainCache_get_invalidModule() {
  Assert.throws(() => {
    const domains = new DomainCache(noopSession, { Foo: undefined });
    domains.get("Foo");
  }, /UnknownMethodError/);
});

add_task(function test_DomainCache_get_missingConstructor() {
  Assert.throws(() => {
    const domains = new DomainCache(noopSession, { Foo: {} });
    domains.get("Foo");
  }, /TypeError/);
});

add_task(function test_DomainCache_get_superClassNotDomain() {
  Assert.throws(() => {
    const domains = new DomainCache(noopSession, { Foo: class {} });
    domains.get("Foo");
  }, /TypeError/);
});

add_task(function test_DomainCache_get_constructs() {
  let eventFired;
  class Session {
    onEvent(event) {
      eventFired = event;
    }
  }

  let constructed = false;
  class Foo extends Domain {
    constructor() {
      super();
      constructed = true;
    }
  }

  const session = new Session();
  const domains = new DomainCache(session, { Foo });

  const foo = domains.get("Foo");
  ok(constructed);
  ok(foo instanceof Foo);

  const event = {};
  foo.emit(event);
  equal(event, eventFired);
});

add_task(function test_DomainCache_size() {
  class Foo extends Domain {}
  const domains = new DomainCache(noopSession, { Foo });

  equal(domains.size, 0);
  domains.get("Foo");
  equal(domains.size, 1);
});

add_task(function test_DomainCache_clear() {
  let dtorCalled = false;
  class Foo extends Domain {
    destructor() {
      dtorCalled = true;
    }
  }

  const domains = new DomainCache(noopSession, { Foo });

  equal(domains.size, 0);
  domains.get("Foo");
  equal(domains.size, 1);

  domains.clear();
  equal(domains.size, 0);
  ok(dtorCalled);
});