<commit_before>/**
 * Copyright 2015 The AMP HTML Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {
  createFixtureIframe,
  expectBodyToBecomeVisible,
} from '../../testing/iframe.js';
import {BaseElement} from '../../src/base-element';
import {createAmpElementProto} from '../../src/custom-element';
import {viewerFor} from '../../src/viewer';
import {resourcesFor} from '../../src/resources';
import {VisibilityState} from '../../src/service/viewer-impl';

describe('Viewer Visibility State', () => {

  let sandbox;

  function noop() {}

  describe('Element Transitions', () => {
    let fixture;
    let resources;
    let viewer;
    let protoElement;
    let layoutCallback;
    let unlayoutCallback;
    let pauseCallback;
    let resumeCallback;
    let docHidden;

    function changeVisibility(vis) {
      docHidden.returns(vis === 'hidden');
      viewer.docState_.onVisibilityChanged_();
    }

    let shouldPass = false;
    let doPass_;
    let notifyPass = noop;
    function doPass() {
      if (shouldPass) {
        doPass_.call(this);
        shouldPass = false;
        notifyPass();
      }
    }

    function waitForNextPass() {
      return new Promise(resolve => {
        shouldPass = true;
        notifyPass = resolve;
        resources.schedulePass();
      });
    }

    class TestElement extends BaseElement {
      // Basic setup
      isLayoutSupported(unusedLayout) {
        return true;
      }
      isRelayoutNeeded() {
        return true;
      }
      prerenderAllowed() {
        return true;
      }
      // Actual state transitions
      layoutCallback() {
        return Promise.resolve();
      }
      unlayoutCallback() {
        return true;
      }
      pauseCallback() {}
      resumeCallback() {}
    }

    function setupSpys() {
      layoutCallback = sinon.spy(protoElement, 'layoutCallback');
      unlayoutCallback = sinon.spy(protoElement, 'unlayoutCallback');
      pauseCallback = sinon.spy(protoElement, 'pauseCallback');
      resumeCallback = sinon.spy(protoElement, 'resumeCallback');
    }

    beforeEach(() => {
      sandbox = sinon.sandbox.create();
      notifyPass = noop;
      shouldPass = false;

      return createFixtureIframe('test/fixtures/visibility-state.html', 10000)
      .then(f => {
        fixture = f;
        fixture.win.name = '__AMP__visibilityState=prerender';
        return expectBodyToBecomeVisible(fixture.win);
      }).then(() => {
        viewer = viewerFor(fixture.win);
        docHidden = sandbox.stub(viewer.docState_, 'isHidden').returns(false);

        protoElement = createAmpElementProto(
          fixture.win,
          'amp-test',
          TestElement
        );

        fixture.doc.registerElement('amp-test', {
          prototype: protoElement,
        });
        resources = resourcesFor(fixture.win);<commit_msg>Use sandbox<commit_after>/**
 * Copyright 2015 The AMP HTML Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {
  createFixtureIframe,
  expectBodyToBecomeVisible,
} from '../../testing/iframe.js';
import {BaseElement} from '../../src/base-element';
import {createAmpElementProto} from '../../src/custom-element';
import {viewerFor} from '../../src/viewer';
import {resourcesFor} from '../../src/resources';
import {VisibilityState} from '../../src/service/viewer-impl';

describe('Viewer Visibility State', () => {

  let sandbox;

  function noop() {}

  describe('Element Transitions', () => {
    let fixture;
    let resources;
    let viewer;
    let protoElement;
    let layoutCallback;
    let unlayoutCallback;
    let pauseCallback;
    let resumeCallback;
    let docHidden;

    function changeVisibility(vis) {
      docHidden.returns(vis === 'hidden');
      viewer.docState_.onVisibilityChanged_();
    }

    let shouldPass = false;
    let doPass_;
    let notifyPass = noop;
    function doPass() {
      if (shouldPass) {
        doPass_.call(this);
        shouldPass = false;
        notifyPass();
      }
    }

    function waitForNextPass() {
      return new Promise(resolve => {
        shouldPass = true;
        notifyPass = resolve;
        resources.schedulePass();
      });
    }

    class TestElement extends BaseElement {
      // Basic setup
      isLayoutSupported(unusedLayout) {
        return true;
      }
      isRelayoutNeeded() {
        return true;
      }
      prerenderAllowed() {
        return true;
      }
      // Actual state transitions
      layoutCallback() {
        return Promise.resolve();
      }
      unlayoutCallback() {
        return true;
      }
      pauseCallback() {}
      resumeCallback() {}
    }

    function setupSpys() {
      layoutCallback = sandbox.spy(protoElement, 'layoutCallback');
      unlayoutCallback = sandbox.spy(protoElement, 'unlayoutCallback');
      pauseCallback = sandbox.spy(protoElement, 'pauseCallback');
      resumeCallback = sandbox.spy(protoElement, 'resumeCallback');
    }

    beforeEach(() => {
      sandbox = sinon.sandbox.create();
      notifyPass = noop;
      shouldPass = false;

      return createFixtureIframe('test/fixtures/visibility-state.html', 10000)
      .then(f => {
        fixture = f;
        fixture.win.name = '__AMP__visibilityState=prerender';
        return expectBodyToBecomeVisible(fixture.win);
      }).then(() => {
        viewer = viewerFor(fixture.win);
        docHidden = sandbox.stub(viewer.docState_, 'isHidden').returns(false);

        protoElement = createAmpElementProto(
          fixture.win,
          'amp-test',
          TestElement
        );

        fixture.doc.registerElement('amp-test', {
          prototype: protoElement,
        });
        resources = resourcesFor(fixture.win);
{'fraction_non_alphanumeric': 0.11579257513258692, 'fraction_numerical': 0.0038302887448438423, 'mean_word_length': 2.592063492063492, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2262101', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1866, 'n_words': 486}