summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/browsers/the-window-object/proxy-getOwnPropertyDescriptor.html
blob: 4ea95048705c4595258aa662fcfe8fa64136e5cb (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>getOwnPropertyDescriptor() is correct for Proxy with host object target</title>
  <link rel="help" href="https://html.spec.whatwg.org/multipage/#window">
  <link rel="help" href="https://webidl.spec.whatwg.org/#Unforgeable">
  <script src="/resources/testharness.js"></script>
  <script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
'use strict';

const assert_accessor_descriptor_equals = (actual, expected) => {
  assert_equals(actual.get, expected.get, 'get');
  assert_equals(actual.set, expected.set, 'set');
  assert_equals(actual.enumerable, expected.enumerable, 'enumerable');
  assert_equals(actual.configurable, expected.configurable, 'configurable');
};

const assert_data_descriptor_equals = (actual, expected) => {
  assert_equals(actual.value, expected.value, 'value');
  assert_equals(actual.writable, expected.writable, 'writable');
  assert_equals(actual.enumerable, expected.enumerable, 'enumerable');
  assert_equals(actual.configurable, expected.configurable, 'configurable');
};

test(() => {
  const windowProxy = new Proxy(window, {});
  name = 'old_name';
  const descriptor = Object.getOwnPropertyDescriptor(windowProxy, 'name');

  assert_equals(descriptor.get.call(window), 'old_name');
  descriptor.set.call(window, 'new_name');
  assert_equals(name, 'new_name');
  assert_true(descriptor.enumerable);
  assert_true(descriptor.configurable);
}, 'Window target, no trap, "name" attribute');

test(() => {
  let trapCalls = 0;
  const windowProxy = new Proxy(window, {
    getOwnPropertyDescriptor(...args) {
      trapCalls++;
      return Reflect.getOwnPropertyDescriptor(...args);
    },
  });

  assert_accessor_descriptor_equals(
    Object.getOwnPropertyDescriptor(windowProxy, 'document'),
    Object.getOwnPropertyDescriptor(window, 'document')
  );
  assert_equals(trapCalls, 1);
}, 'Window target, forwarding trap, [LegacyUnforgeable] "document" attribute');

test(() => {
  const trapResult = {get() {}, set(_val) {}, enumerable: false, configurable: true};
  const windowProxy = new Proxy(new Proxy(window, {}), {
    getOwnPropertyDescriptor: () => trapResult,
  });

  assert_accessor_descriptor_equals(
    Object.getOwnPropertyDescriptor(windowProxy, 'onclick'),
    trapResult
  );
}, 'Window proxy target, custom trap, "onclick" event handler attribute');

test(() => {
  let trapCalls = 0;
  const documentProxy = new Proxy(document, {
    getOwnPropertyDescriptor(...args) {
      trapCalls++;
      return Reflect.getOwnPropertyDescriptor(...args);
    },
  });

  assert_accessor_descriptor_equals(
    Object.getOwnPropertyDescriptor(documentProxy, 'location'),
    Object.getOwnPropertyDescriptor(document, 'location')
  );
  assert_equals(trapCalls, 1);
}, 'Document target, forwarding trap, [LegacyUnforgeable] "location" attribute');

test(() => {
  const trapResult = {value: 4, writable: false, enumerable: true, configurable: true};
  const documentProxy = new Proxy(new Proxy(document, {}), {
    getOwnPropertyDescriptor: () => trapResult,
  });

  assert_data_descriptor_equals(
    Object.getOwnPropertyDescriptor(documentProxy, 'foo'),
    trapResult
  );
}, 'Document proxy target, custom trap, non-existent value attribute');

test(() => {
  const locationProxy = new Proxy(location, {});
  location.hash = '#old';
  const descriptor = Object.getOwnPropertyDescriptor(locationProxy, 'hash');

  assert_equals(descriptor.get.call(location), '#old');
  descriptor.set.call(location, '#new');
  assert_equals(location.hash, '#new');
  assert_true(descriptor.enumerable);
  assert_false(descriptor.configurable);
}, 'Location target, no trap, [LegacyUnforgeable] "hash" attribute');

test(() => {
  let trapCalls = 0;
  const locationProxy = new Proxy(new Proxy(location, {}), {
    getOwnPropertyDescriptor(...args) {
      trapCalls++;
      return Reflect.getOwnPropertyDescriptor(...args);
    },
  });

  assert_data_descriptor_equals(
    Object.getOwnPropertyDescriptor(locationProxy, 'reload'),
    Object.getOwnPropertyDescriptor(location, 'reload')
  );
  assert_equals(trapCalls, 1);
}, 'Location proxy target, forwarding trap, [LegacyUnforgeable] "reload" method');
</script>
</body>
</html>