From bfc33f94eab3ac86201134a18c4f75c07bbe0d1b Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Thu, 8 Dec 2016 19:24:28 -0500 Subject: [PATCH] Add tests --- src/service/extension-location.js | 6 +- test/functional/test-extension-location.js | 186 +++++++++++++++++++++ test/functional/test-extensions.js | 91 ---------- test/manual/cache-sw.html | 4 +- 4 files changed, 189 insertions(+), 98 deletions(-) create mode 100644 test/functional/test-extension-location.js diff --git a/src/service/extension-location.js b/src/service/extension-location.js index 35f49377248c8..11a9cec184cdf 100644 --- a/src/service/extension-location.js +++ b/src/service/extension-location.js @@ -65,10 +65,8 @@ export function calculateExtensionScriptUrl(location, extensionId, isLocalDev, export function calculateEntryPointScriptUrl(location, entryPoint, isLocalDev, isTest) { const base = calculateScriptBaseUrl(location, isLocalDev, isTest); - if (isLocalDev) { - return `${base}/${entryPoint}${isMax(location) ? '.max' : ''}.js`; - } - return `${base}/rtv/${getMode().rtvVersion}/${entryPoint}.js`; + const serveMax = isLocalDev && isMax(location); + return `${base}/${entryPoint}${serveMax ? '.max' : ''}.js`; } /** diff --git a/test/functional/test-extension-location.js b/test/functional/test-extension-location.js new file mode 100644 index 0000000000000..cd2811a252d44 --- /dev/null +++ b/test/functional/test-extension-location.js @@ -0,0 +1,186 @@ +/** + * 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 { + calculateExtensionScriptUrl, +} from '../../src/service/extension-location'; +import { + initLogConstructor, + resetLogConstructorForTesting, +} from '../../src/log'; + +describes.sandboxed('Extension Location', {}, () => { + describe('get correct script source', () => { + beforeEach(() => { + // These functions must not rely on log for cases in SW. + resetLogConstructorForTesting(); + }); + + afterEach(() => { + initLogConstructor(); + }); + + it('with local mode for testing with compiled js', () => { + const script = calculateExtensionScriptUrl({ + pathname: 'examples/ads.amp.html', + host: 'localhost:8000', + protocol: 'http:', + }, 'amp-ad', true, true, true); + expect(script).to.equal('http://localhost:8000/dist/v0/amp-ad-0.1.js'); + }); + + it('with local mode for testing without compiled js', () => { + const script = calculateExtensionScriptUrl({ + pathname: 'examples/ads.amp.html', + host: 'localhost:80', + protocol: 'https:', + }, 'amp-ad', true, true, false); + expect(script).to.equal('https://localhost:80/dist/v0/amp-ad-0.1.max.js'); + }); + + it('with local mode normal pathname', () => { + const script = calculateExtensionScriptUrl({ + pathname: 'examples/ads.amp.html', + host: 'localhost:8000', + protocol: 'https:', + }, 'amp-ad', true); + expect(script).to.equal('https://cdn.ampproject.org/v0/amp-ad-0.1.js'); + }); + + it('with local mode min pathname', () => { + const script = calculateExtensionScriptUrl({ + pathname: 'examples/ads.amp.min.html', + host: 'localhost:8000', + protocol: 'http:', + }, 'amp-ad', true); + expect(script).to.equal('http://localhost:8000/dist/v0/amp-ad-0.1.js'); + }); + + it('with local mode max pathname', () => { + const script = calculateExtensionScriptUrl({ + pathname: 'examples/ads.amp.max.html', + host: 'localhost:8000', + protocol: 'http:', + }, 'amp-ad', true); + expect(script).to.equal('http://localhost:8000/dist/v0/amp-ad-0.1.max.js'); + }); + + it('with remote mode', () => { + window.AMP_MODE = {rtvVersion: '123'}; + const script = calculateExtensionScriptUrl({ + pathname: 'examples/ads.amp.min.html', + host: 'localhost:8000', + protocol: 'http:', + }, 'amp-ad', false); + expect(script).to.equal( + 'https://cdn.ampproject.org/rtv/123/v0/amp-ad-0.1.js'); + }); + + it('with document proxy mode: max', () => { + const script = calculateExtensionScriptUrl({ + pathname: '/max/output.jsbin.com/pegizoq/quiet', + host: 'localhost:80', + protocol: 'http:', + }, 'amp-ad', true); + expect(script).to.equal('http://localhost:80/dist/v0/amp-ad-0.1.max.js'); + }); + + it('with document proxy mode: min', () => { + const script = calculateExtensionScriptUrl({ + pathname: '/min/output.jsbin.com/pegizoq/quiet', + host: 'localhost:80', + protocol: 'http:', + }, 'amp-ad', true); + expect(script).to.equal('http://localhost:80/dist/v0/amp-ad-0.1.js'); + }); + }); + + describe('get correct entry point source', () => { + beforeEach(() => { + // These functions must not rely on log for cases in SW. + resetLogConstructorForTesting(); + }); + + afterEach(() => { + initLogConstructor(); + }); + + it('with local mode for testing', () => { + const script = calculateExtensionScriptUrl({ + pathname: 'examples/ads.amp.html', + host: 'localhost:8000', + protocol: 'http:', + }, 'sw.js', true, true); + expect(script).to.equal('http://localhost:8000/dist/sw.js'); + }); + + it('with local mode normal pathname', () => { + const script = calculateExtensionScriptUrl({ + pathname: 'examples/ads.amp.html', + host: 'localhost:8000', + protocol: 'https:', + }, 'sw.js', true); + expect(script).to.equal('https://cdn.ampproject.org/sw.js'); + }); + + it('with local mode min pathname', () => { + const script = calculateExtensionScriptUrl({ + pathname: 'examples/ads.amp.min.html', + host: 'localhost:8000', + protocol: 'http:', + }, 'sw.js', true); + expect(script).to.equal('http://localhost:8000/dist/sw.js'); + }); + + it('with local mode max pathname', () => { + const script = calculateExtensionScriptUrl({ + pathname: 'examples/ads.amp.max.html', + host: 'localhost:8000', + protocol: 'http:', + }, 'sw.js', true); + expect(script).to.equal('http://localhost:8000/dist/sw.max.js'); + }); + + it('with remote mode', () => { + window.AMP_MODE = {rtvVersion: '123'}; + const script = calculateExtensionScriptUrl({ + pathname: 'examples/ads.amp.min.html', + host: 'localhost:8000', + protocol: 'http:', + }, 'sw.js', false); + expect(script).to.equal( + 'https://cdn.ampproject.org/sw.js'); + }); + + it('with document proxy mode: max', () => { + const script = calculateExtensionScriptUrl({ + pathname: '/max/output.jsbin.com/pegizoq/quiet', + host: 'localhost:80', + protocol: 'http:', + }, 'sw.js', true); + expect(script).to.equal('http://localhost:80/dist/sw.js'); + }); + + it('with document proxy mode: min', () => { + const script = calculateExtensionScriptUrl({ + pathname: '/min/output.jsbin.com/pegizoq/quiet', + host: 'localhost:80', + protocol: 'http:', + }, 'sw.js', true); + expect(script).to.equal('http://localhost:80/dist/sw.js'); + }); + }); +}); diff --git a/test/functional/test-extensions.js b/test/functional/test-extensions.js index fc980b0366083..d4cac9a0275bc 100644 --- a/test/functional/test-extensions.js +++ b/test/functional/test-extensions.js @@ -22,15 +22,10 @@ import { addDocFactoryToExtension, addElementToExtension, addShadowRootFactoryToExtension, - calculateExtensionScriptUrl, installExtensionsInShadowDoc, installExtensionsService, registerExtension, } from '../../src/service/extensions-impl'; -import { - initLogConstructor, - resetLogConstructorForTesting, -} from '../../src/log'; import {resetScheduledElementForTesting} from '../../src/custom-element'; import {loadPromise} from '../../src/event-helper'; @@ -537,90 +532,4 @@ describes.sandboxed('Extensions', {}, () => { }); }); }); - - describe('get correct script source', () => { - - beforeEach(() => { - // These functions must not rely on log for cases in SW. - resetLogConstructorForTesting(); - }); - - afterEach(() => { - initLogConstructor(); - }); - - it('with local mode for testing with compiled js', () => { - const script = calculateExtensionScriptUrl({ - pathname: 'examples/ads.amp.html', - host: 'localhost:8000', - protocol: 'http:', - }, 'amp-ad', true, true, true); - expect(script).to.equal('http://localhost:8000/dist/v0/amp-ad-0.1.js'); - }); - - it('with local mode for testing without compiled js', () => { - const script = calculateExtensionScriptUrl({ - pathname: 'examples/ads.amp.html', - host: 'localhost:80', - protocol: 'https:', - }, 'amp-ad', true, true, false); - expect(script).to.equal('https://localhost:80/dist/v0/amp-ad-0.1.max.js'); - }); - - it('with local mode normal pathname', () => { - const script = calculateExtensionScriptUrl({ - pathname: 'examples/ads.amp.html', - host: 'localhost:8000', - protocol: 'https:', - }, 'amp-ad', true); - expect(script).to.equal('https://cdn.ampproject.org/v0/amp-ad-0.1.js'); - }); - - it('with local mode min pathname', () => { - const script = calculateExtensionScriptUrl({ - pathname: 'examples/ads.amp.min.html', - host: 'localhost:8000', - protocol: 'http:', - }, 'amp-ad', true); - expect(script).to.equal('http://localhost:8000/dist/v0/amp-ad-0.1.js'); - }); - - it('with local mode max pathname', () => { - const script = calculateExtensionScriptUrl({ - pathname: 'examples/ads.amp.max.html', - host: 'localhost:8000', - protocol: 'http:', - }, 'amp-ad', true); - expect(script).to.equal('http://localhost:8000/dist/v0/amp-ad-0.1.max.js'); - }); - - it('with remote mode', () => { - window.AMP_MODE = {rtvVersion: '123'}; - const script = calculateExtensionScriptUrl({ - pathname: 'examples/ads.amp.min.html', - host: 'localhost:8000', - protocol: 'http:', - }, 'amp-ad', false); - expect(script).to.equal( - 'https://cdn.ampproject.org/rtv/123/v0/amp-ad-0.1.js'); - }); - - it('with document proxy mode: max', () => { - const script = calculateExtensionScriptUrl({ - pathname: '/max/output.jsbin.com/pegizoq/quiet', - host: 'localhost:80', - protocol: 'http:', - }, 'amp-ad', true); - expect(script).to.equal('http://localhost:80/dist/v0/amp-ad-0.1.max.js'); - }); - - it('with document proxy mode: min', () => { - const script = calculateExtensionScriptUrl({ - pathname: '/min/output.jsbin.com/pegizoq/quiet', - host: 'localhost:80', - protocol: 'http:', - }, 'amp-ad', true); - expect(script).to.equal('http://localhost:80/dist/v0/amp-ad-0.1.js'); - }); - }); }); diff --git a/test/manual/cache-sw.html b/test/manual/cache-sw.html index 77ed9df637777..8eb0b54a65501 100644 --- a/test/manual/cache-sw.html +++ b/test/manual/cache-sw.html @@ -2,7 +2,7 @@ - Ad examples + Cache SW Manual Testing - -