diff --git a/package.json b/package.json index f142318e..467965f2 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ "nodemon": "^1.10.2", "open": "0.0.5", "plist": "^2.0.1", + "pngjs": "^3.0.0", "rimraf": "^2.5.2", "run-series": "^1.1.4", "spectron": "^3.3.0", diff --git a/test/index.js b/test/index.js index b3fed4b3..75fcce36 100644 --- a/test/index.js +++ b/test/index.js @@ -15,9 +15,10 @@ test('app runs', function (t) { }) require('./test-torrent-list') -// require('./test-add-torrent') -// require('./test-video') +require('./test-add-torrent') +require('./test-video') +// TODO: // require('./test-audio') // require('./test-cast') // require('./test-prefs') diff --git a/test/screenshots/darwin/play-torrent-bbb.png b/test/screenshots/darwin/play-torrent-bbb.png index 07908278..5937ccc6 100644 Binary files a/test/screenshots/darwin/play-torrent-bbb.png and b/test/screenshots/darwin/play-torrent-bbb.png differ diff --git a/test/screenshots/darwin/play-torrent-return.png b/test/screenshots/darwin/play-torrent-return.png new file mode 100644 index 00000000..b28dad8c Binary files /dev/null and b/test/screenshots/darwin/play-torrent-return.png differ diff --git a/test/setup.js b/test/setup.js index c9d31758..af4b962c 100644 --- a/test/setup.js +++ b/test/setup.js @@ -2,6 +2,7 @@ const path = require('path') const Application = require('spectron').Application const fs = require('fs-extra') const parseTorrent = require('parse-torrent') +const PNG = require('pngjs').PNG const config = require('./config') module.exports = { @@ -78,7 +79,7 @@ function screenshotCreateOrCompare (app, t, name) { console.log('Saving screenshot ' + ssPath) fs.writeFileSync(ssPath, buffer) } else { - const match = Buffer.compare(buffer, ssBuf) === 0 + const match = compareIgnoringTransparency(buffer, ssBuf) t.ok(match, 'screenshot comparison ' + name) if (!match) { const ssFailedPath = path.join(ssDir, name + '-failed.png') @@ -89,6 +90,30 @@ function screenshotCreateOrCompare (app, t, name) { }) } +// Compares two PNGs, ignoring any transparent regions in bufExpected. +// Returns true if they match. +function compareIgnoringTransparency (bufActual, bufExpected) { + // Common case: exact byte-for-byte match + if (Buffer.compare(bufActual, bufExpected) === 0) return true + + // Otherwise, compare pixel by pixel + const pngA = PNG.sync.read(bufActual) + const pngE = PNG.sync.read(bufExpected) + if (pngA.width !== pngE.width || pngA.height !== pngE.height) return false + const w = pngA.width + const h = pngE.height + const da = pngA.data + const de = pngE.data + for (let y = 0; y < h; y++) { + for (let x = 0; x < w; x++) { + const i = (y * w + x) * 4 + if (de[i + 3] === 0) continue // Skip transparent pixels + if (da[i] !== de[i] || da[i + 1] !== de[i + 1] || da[i + 2] !== de[i + 2]) return false + } + } + return true +} + // Resets the test directory, containing config.json, torrents, downloads, etc function resetTestDataDir () { fs.removeSync(config.TEST_DIR) diff --git a/test/test-video.js b/test/test-video.js index 1954d5e8..525eb5e6 100644 --- a/test/test-video.js +++ b/test/test-video.js @@ -9,7 +9,7 @@ test('basic-streaming', function (t) { setup.waitForLoad(app, t, {online: true}) .then(() => app.client.waitUntilTextExists('.torrent-list', 'Big Buck Bunny')) // Play Big Buck Bunny. Wait for it to start streaming. - .then(() => app.client.moveToObject('.torrent.bbb')) + .then(() => app.client.moveToObject('.torrent')) .then(() => setup.wait()) .then(() => app.client.click('.icon.play')) .then(() => setup.wait(10e3)) @@ -23,6 +23,8 @@ test('basic-streaming', function (t) { .then(() => app.webContents.executeJavaScript('dispatch("escapeBack")')) .then(() => setup.wait()) // Delete Big Buck Bunny + .then(() => app.client.moveToObject('.torrent')) + .then(() => setup.wait()) .then(() => app.client.click('.icon.delete')) .then(() => setup.wait()) .then(() => app.client.click('.control.ok'))