'use strict' var t = require('tape') var fs = require('fs') var path = require('path') var getPixels = require('../') var match = require('pixelmatch') var toab = require('to-array-buffer') var fixture = require('./fixture') var ab2s = require('arraybuffer-to-string') var x = require('object-assign') var getNdPixels = require('get-pixels') var isOnline = require('is-online') var isBrowser = require('is-browser') var save = require('save-file') if (!isBrowser) { var path = require('path') var del = require('del') } if (!isBrowser) { // var { JSDOM } = require('jsdom') // var { window } = new JSDOM(``) } var clipFix = { data: [ 0,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255 ], width: 2, height: 2 } var pngFixData = toab(fixture.pngDataURL) var pngFixURL = fixture.pngURL const ASSERT_N = 19 const REQUEST_TIMEOUT = 3000 async function testSource(t, arg, o, fix=fixture) { // direct let to = setTimeout(function () {t.fail('Direct timeout')}, REQUEST_TIMEOUT) let data = await getPixels(arg, o) clearTimeout(to) t.equal(data.width, fix.width) t.equal(data.height, fix.height) fix.data ? t.equal(match(data.data, fix.data, null, fix.width, fix.height, {threshold: .006}), 0, 'Ok async pixels') : t.ok(data.data[0], 'Ok async pixels') let redata = await getPixels(data) t.equal(redata, data, 'Repeat data') // second time (cache) to = setTimeout(function () {t.fail('Direct second timeout')}, REQUEST_TIMEOUT) let data2 = await getPixels(arg, o) clearTimeout(to) t.deepEqual(data.data, data2.data) t.equal(data2.width, fix.width) t.equal(data2.height, fix.height) fix.data ? t.equal(match(data2.data, fix.data, null, fix.width, fix.height, {threshold: .006}), 0, 'Ok async pixels twice') : t.ok(data2.data[0], 'Ok async pixels twice') let redata2 = await getPixels(data2) t.equal(redata2, data2, 'Repeat data secondary') // clip to = setTimeout(function () {t.fail('Clip timeout')}, REQUEST_TIMEOUT) let clip = await getPixels(arg, x({clip: [1,1,3,3]}, o)) clearTimeout(to) t.equal(clip.width, 2) t.equal(clip.height, 2) fix.data ? t.equal(match(clip.data, clipFix.data, null, 2, 2, {threshold: .006}), 0, 'Ok clip pixels') : t.ok(clip.data[0], 'Ok clip pixels') // alltogether to = setTimeout(function () {t.fail('All timeout')}, REQUEST_TIMEOUT) var list = await getPixels.all([ o, x({clip: [1,1,3,3]}, o) ], {source: arg}) clearTimeout(to) t.deepEqual(data.data, list[0].data, 'Ok all pixels data') t.equal(list[0].width, fix.width) t.equal(list[0].height, fix.height) fix.data ? t.equal(match(list[0].data, fix.data, null, fix.width, fix.height, {threshold: .006}), 0, 'Ok all pixels') : t.ok(list[0].data[0], 'Ok all pixels') t.equal(list[1].width, 2) t.equal(list[1].height, 2) fix.data ? t.equal(match(list[1].data, clipFix.data, null, 2, 2, {threshold: .006}), 0, 'Ok clip pixels') : t.ok(list[1].data[0], 'Ok all clip pixels') } async function online () { if (isOnline.call) { isOnline = await isOnline() } return isOnline } // strings t('absolute path', async t => { t.plan(ASSERT_N) await testSource(t, path.resolve('./test/test_pattern.png')) t.end() }) t('relative path', async t => { t.plan(ASSERT_N) await testSource(t, './test/test_pattern.png') t.end() }) t('some path', async t => { t.plan(ASSERT_N) await testSource(t, 'test/test_pattern.png') t.end() }) t('https', async t => { if (await online()) { t.plan(ASSERT_N) await testSource(t, pngFixURL) } t.end() }) t('http', async t => { if (await online()) { t.plan(ASSERT_N) await testSource(t, pngFixURL.replace('https', 'http')) } t.end() }) t('protocol-relative URL', async t => { if (await online()) { t.plan(ASSERT_N) await testSource(t, pngFixURL.replace('https:', '')) } t.end() }) t('data URL', async t => { t.plan(2 * ASSERT_N) await testSource(t, fixture.pngDataURL) await testSource(t, fixture.jpgDataURL) t.end() }) t('base64', async t => { t.plan(ASSERT_N) await testSource(t, fixture.pngDataURL.replace(/^data:image\/(png|jpg);base64,/, '')) t.end() }) t('raw pixels base64', async t => { t.plan(ASSERT_N) await testSource(t, ab2s(fixture.data, 'base64'), {w:16, h:8}) t.end() }) // DOMs t(``, async t => { //TODO: add node test if (!isBrowser) return t.end() t.plan(ASSERT_N) let img = document.createElement('img') img.src = './test/test_pattern.png' await testSource(t, img) t.end() }) t(``, async t => { if (!isBrowser) return t.end() t.plan(ASSERT_N) let el = document.createElement('div') el.innerHTML = ` ` let img = el.firstChild.firstChild await testSource(t, img) t.end() }) t(`