aboutsummaryrefslogtreecommitdiff
path: root/catapult/third_party/polymer/components/shadycss/tests/ordering.html
blob: 392897c4c1f5872d218624250a1f6bd884f51bdb (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!doctype html>
<!--
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<script>WCT = {waitFor(cb){addEventListener('DOMContentLoaded', cb)}};</script>
<script src="test-flags.js"></script>
<script src="../node_modules/wct-browser-legacy/browser.js"></script>
<script src="../node_modules/@webcomponents/webcomponents-platform/webcomponents-platform.js"></script>
<script src="../node_modules/es6-promise/dist/es6-promise.auto.min.js"></script>
<script src="../node_modules/@webcomponents/template/template.js"></script>
<script src="../node_modules/@webcomponents/html-imports/html-imports.min.js"></script>
<script src="../node_modules/@webcomponents/shadydom/shadydom.min.js"></script>
<script src="../node_modules/@webcomponents/custom-elements/custom-elements.min.js"></script>
<script src="module/generated/make-element.js"></script>

<custom-style>
  <style>
    html {
      --coloring: {
        color: rgb(0, 0, 255);
        background-color: rgb(255, 0, 0);
      }
    }
  </style>
</custom-style>

<custom-style>
  <style>
    html {
      --border: 2px solid black;
    }
  </style>
</custom-style>

<template id="x-inner">
  <style>
    :host {
      @apply --inner;
      @apply --coloring;
    }
  </style>
<slot></slot>
</template>

<template id="x-element">
  <style>
    :host {
      display: block;
      border: var(--border);
      @apply --coloring;
    }
    x-inner {
      --inner: {
        border: 10px solid black;
      }
    }
  </style>
  <div>What color?</div>
  <x-inner>Inner</x-inner>
</template>

<template id="x-early">
  <style>
    :host {
      display: block;
      background: rgb(123, 123, 123);
      color: rgb(255, 165, 0);
      border: var(--border, 10px dotted blue);
      @apply --coloring;
    }
  </style>
  <div>Early!</div>
</template>

<x-early></x-early>

<script>
  function loadScript(src) {
    console.log(`loading ${src}`);
    let script = document.createElement('script')
    script.src = src;
    let p = new Promise((resolve, reject) => {
      script.onload = () => {console.log(`loaded ${src}`); resolve()};
      script.onerror = () => {console.error(`error ${src}`); reject()};
    });
    document.head.appendChild(script);
    return p;
  }

  function registerEarly() {
    makeElement('x-early');
  }

  function loadScopingShim() {
    return loadScript('../scoping-shim.min.js');
  }

  function loadCustomStyle() {
    return loadScript('../custom-style-interface.min.js').then(() => {
      return loadScript('module/generated/custom-style-element.js')
    });
  }

  function loadApplyShim() {
    return loadScript('../apply-shim.min.js');
  }

  function register() {
    makeElement('x-inner');
    makeElement('x-element');
  }

  function assertComputed(element, property, expected) {
    let value = (getComputedStyle(element).getPropertyValue(property) || '').trim();
    assert.equal(expected, value, `${property} on ${element.localName} incorrect`);
  }

  function chain(arr) {
    let out = Promise.resolve();
    for (let i = 0; i < arr.length; i++) {
      out = out.then(arr[i]);
    }
    return out;
  }

  let orderSteps = {
    scoping: loadScopingShim,
    early: registerEarly,
    apply: loadApplyShim,
    custom: loadCustomStyle,
  };

  /**
   * Support the following permutations of loading via url query params:
   *
   * Apply Shim, CustomStyle
   * Scoping Shim, Apply Shim, Custom Style
   * Apply Shim, Element, CustomStyle
   * Scoping Shim, Element, Apply Shim, Custom Style
   * Scoping Shim, Apply Shim, Element, Custom Style
   */

  suite('Dynamic ordering of components', () => {
    let flags = window.WebComponents.flags;
    let order = decodeURIComponent(flags.order || 'scoping,apply,custom').split(',');
    let steps = chain(order.map(o => orderSteps[o]));
    let otherFlags = `${flags.ce ? 'ce' : ''} ${flags.shadydom ? 'shadydom' : ''} ${flags.shimcssproperties ? 'shimcssproperties' : ''}`;
    let needsScopingShim = window.ShadyDOM && window.ShadyDOM.inUse || flags.shimcssproperties;

    test(`${order.join(', ')} with ${otherFlags}`, function() {
      console.log(order, flags);
      if (order.indexOf('scoping') === -1 && needsScopingShim) {
        this.skip();
      }
      return steps.then(
        register
      ).then(() => {
        let el = document.createElement('x-element');
        document.body.appendChild(el);
        assertComputed(el, 'background-color', 'rgb(255, 0, 0)');
        assertComputed(el, 'border-top-width', '2px');
        assertComputed(el.shadowRoot.querySelector('div'), 'color', 'rgb(0, 0, 255)');
        assertComputed(el.shadowRoot.querySelector('x-inner'), 'border-top-width', '10px');
      });
    })
  })
</script>