/**
 * Copyright 2016 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 {createIframePromise} from '../../testing/iframe';
import {listenParent} from '../../3p/messaging';
import {postMessage} from '../../src/iframe-helper';
import {timer} from '../../src/timer';

describe('3p messaging', () => {

  let testWin;
  let iframe;

  beforeEach(() => {
    return createIframePromise(true).then(i => {
      testWin = i.win;
      testWin.context = {
        location: window.location,
      };
      iframe = {
        contentWindow: testWin,
      };
    });
  });

  it('should receive messages', () => {
    let progress = '';
    listenParent(testWin, 'test', function(d) {
      progress += d.s;
    });
    postMessage(iframe, 'test', {s: 'a'}, '*', true);
    postMessage(iframe, 'test', {s: 'b'}, '*', false);
    postMessage(iframe, 'other', {s: 'c'}, '*', true);
    postMessage(iframe, 'test', {s: 'd'}, '*', true);
    return timer.promise(10).then(() => {
      expect(progress).to.equal('ad');
    });
  });

  it('should receive more messages', () => {
    let progress = '';
    listenParent(testWin, 'test', function(d) {
      progress += d.s;
    });
    listenParent(testWin, 'test', function(d) {
      progress += d.s;
    });
    listenParent(testWin, 'test2', function(d) {
      progress += d.s;
    });
    postMessage(iframe, 'test', {s: 'a'}, '*', true);
    postMessage(iframe, 'test2', {s: 'a'}, '*', true);
    postMessage(iframe, 'test2', {s: 'a'}, '*', true);
    postMessage(iframe, 'test', {s: 'b'}, '*', false);
    postMessage(iframe, 'other', {s: 'c'}, '*', true);
    postMessage(iframe, 'test2', {s: 'a'}, '*', true);
    postMessage(iframe, 'test', {s: 'd'}, '*', true);
    return timer.promise(10).then(() => {
      expect(progress).to.equal('aaaaadd');
    });
  });

  it('should support unlisten', () => {
    let progress = '';
    const unlisten0 = listenParent(testWin, 'test', function(d) {
      progress += d.s;
    });
    const unlisten1 = listenParent(testWin, 'test', function(d) {
      progress += d.s;
    });
    const unlisten2 = listenParent(testWin, 'test2', function(d) {
      progress += d.s;
    });
    postMessage(iframe, 'test', {s: 'a'}, '*', true);
    return timer.promise(10).then(() => {
      expect(progress).to.equal('aa');
      unlisten0();
      postMessage(iframe, 'test2', {s: 'a'}, '*', true);
      postMessage(iframe, 'test2', {s: 'a'}, '*', true);
      postMessage(iframe, 'test', {s: 'b'}, '*', true);
      return timer.promise(10).then(() => {
        unlisten2();
        unlisten1();
        postMessage(iframe, 'test2', {s: 'a'}, '*', true);
        postMessage(iframe, 'test', {s: 'd'}, '*', true);
        return timer.promise(10).then(() => {
          expect(progress).to.equal('aaaab');
        });
      });
    });
  });

  it('should not stop on errors', () => {
    let progress = '';
    listenParent(testWin, 'test', function() {
      throw new Error('expected');
    });
    listenParent(testWin, 'test', function(d) {
      progress += d.s;
    });
    postMessage(iframe, 'test', {s: 'a'}, '*', true);
    postMessage(iframe, 'test', {s: 'd'}, '*', true);
    return timer.promise(10).then(() => {
      expect(progress).to.equal('ad');
    });
  });
});

@@ -3436,32 +3436,288 @@
  progress = '';%0A
+    const origOnError = window.onError;%0A    const expected = new Error('expected');%0A    window.onerror = (message, source, lineno, colno, error) =%3E %7B%0A      if (error === expected) %7B%0A        return;%0A      %7D%0A      origOnError.apply(this, arguments);%0A    %7D;%0A%0A
     listenParent
@@ -3759,35 +3759,24 @@
   throw 
-new Error('
 expected
 ');%0A    
@@ -3763,26 +3763,24 @@
 row expected
-')
 ;%0A    %7D);%0A  
@@ -3989,36 +3989,88 @@
 promise(10).
-then(() =%3E %7B
+catch(() =%3E %7B%7D).then(() =%3E %7B%0A      window.onError = origOnError;
 %0A      expec

Fix issue with uncaught error