MCPcopy
hub / github.com/ampproject/amphtml / createFixtureIframe

Function createFixtureIframe

testing/iframe.js:52–193  ·  view source on GitHub ↗
(
  fixture,
  initialIframeHeight,
  opt_beforeLoad
)

Source from the content-addressed store, hash-verified

50 * }>}
51 */
52export function createFixtureIframe(
53 fixture,
54 initialIframeHeight,
55 opt_beforeLoad
56) {
57 dev().assertNumber(
58 initialIframeHeight,
59 'Attempted to create fixture iframe with non-numeric height'
60 );
61 return new Promise((resolve, reject) => {
62 // Counts the supported custom events.
63 const events = {
64 [AmpEvents_Enum.ATTACHED]: 0,
65 [AmpEvents_Enum.DOM_UPDATE]: 0,
66 [AmpEvents_Enum.ERROR]: 0,
67 [AmpEvents_Enum.LOAD_END]: 0,
68 [AmpEvents_Enum.LOAD_START]: 0,
69 [AmpEvents_Enum.STUBBED]: 0,
70 [AmpEvents_Enum.UNLOAD]: 0,
71 [BindEvents.INITIALIZE]: 0,
72 [BindEvents.SET_STATE]: 0,
73 [BindEvents.RESCAN_TEMPLATE]: 0,
74 [FormEvents.SERVICE_INIT]: 0,
75 };
76 let html = __html__[fixture] // eslint-disable-line no-undef
77 .replace(
78 /__TEST_SERVER_PORT__/g,
79 window.ampTestRuntimeConfig.testServerPort
80 );
81 if (!html) {
82 throw new Error('Cannot find fixture: ' + fixture);
83 }
84 html = maybeSwitchToMinifiedJs(html);
85 window.ENABLE_LOG = true;
86 // This global function will be called by the iframe immediately when it
87 // starts loading. This appears to be the only way to get the correct
88 // window object early enough to not miss any events that may get fired
89 // on that window.
90 window.beforeLoad = function (win) {
91 // Flag as being a test window.
92 win.__AMP_TEST_IFRAME = true;
93 win.__AMP_TEST = true;
94 // Set the testLocation on iframe to parent's location since location of
95 // the test iframe is about:srcdoc.
96 // Unfortunately location object is not configurable, so we have to define
97 // a new property.
98 win.testLocation = new FakeLocation(window.location.href, win);
99 win.ampTestRuntimeConfig = window.ampTestRuntimeConfig;
100 if (opt_beforeLoad) {
101 opt_beforeLoad(win);
102 }
103 const messages = new MessageReceiver(win);
104 // Function that returns a promise for when the given event fired at
105 // least count times.
106 const awaitEvent = (eventName, count) => {
107 if (!(eventName in events)) {
108 throw new Error('Unknown custom event ' + eventName);
109 }

Callers 14

test-errors.jsFile · 0.90
runTestFunction · 0.90
getVideoPlayerFunction · 0.90
test-css.jsFile · 0.90
createFixtureFunction · 0.90
testLoadOrderFixtureFunction · 0.90
test-amp-img.jsFile · 0.90

Calls 13

devFunction · 0.90
maybeSwitchToMinifiedJsFunction · 0.85
assertNumberMethod · 0.80
applyMethod · 0.80
resolveFunction · 0.50
replaceMethod · 0.45
addEventListenerMethod · 0.45
pushMethod · 0.45
testMethod · 0.45
setAttributeMethod · 0.45
openMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected