import 'mocha' ; import * as assert from 'assert' ; import BarcodeFormat from './../../../core/BarcodeFormat' ; import EncodeHintType from './../../../core/EncodeHintType' ; import Writer from './../../../core/Writer' ; import BitMatrix from './../../../core/common/BitMatrix' ; import ErrorCorrectionLevel from './../../../core/qrcode/decoder/ErrorCorrectionLevel' ; import SharpImage from './../util/SharpImage' ; import QRCodeWriter from './../../../core/qrcode/QRCodeWriter' ; const path = require ( 'path' ) ; author satorux google author dswitkin google describe ( "QRCodeWriterTestCase" , ( ) => { const BASE_IMAGE_PATH = "src/test/core/resources/golden/qrcode/" ; it ( "testQRCodeWriter" , ( ) => { const bigEnough = 256 ; const writer = new QRCodeWriter ( ) ; let matrix = writer . encode ( "http://www.google.com/" , BarcodeFormat . QR_CODE , bigEnough , bigEnough , null ) ; assert . strictEqual ( matrix !== null , true ) ; assert . strictEqual ( matrix . getWidth ( ) , bigEnough ) ; assert . strictEqual ( matrix . getHeight ( ) , bigEnough ) ; const tooSmall = 20 ; matrix = writer . encode ( "http://www.google.com/" , BarcodeFormat . QR_CODE , tooSmall , tooSmall , null ) ; assert . strictEqual ( matrix !== null , true ) ; assert . strictEqual ( tooSmall < matrix . getWidth ( ) , true ) ; assert . strictEqual ( tooSmall < matrix . getHeight ( ) , true ) ; const strangeWidth = 500 ; const strangeHeight = 100 ; matrix = writer . encode ( "http://www.google.com/" , BarcodeFormat . QR_CODE , strangeWidth , strangeHeight , null ) ; assert . strictEqual ( matrix !== null , true ) ; assert . strictEqual ( matrix . getWidth ( ) , strangeWidth ) ; assert . strictEqual ( matrix . getHeight ( ) , strangeHeight ) ; } ) ; function compareToGoldenFile ( contents , ecLevel , resolution , fileName ) { const filePath = path . resolve ( BASE_IMAGE_PATH , fileName ) ; SharpImage . loadAsBitMatrix ( filePath , ( err , goldenResult ) => { if ( err ) { assert . ok ( false , err ) ; } else { const hints = new Map < EncodeHintType , ErrorCorrectionLevel > ( ) ; hints . set ( EncodeHintType . ERROR_CORRECTION , ecLevel ) ; const writer = new QRCodeWriter ( ) ; const generatedResult = writer . encode ( contents , BarcodeFormat . QR_CODE , resolution , resolution , hints ) ; assert . strictEqual ( generatedResult . getWidth ( ) , resolution ) ; assert . strictEqual ( generatedResult . getHeight ( ) , resolution ) ; assert . strictEqual ( generatedResult . equals ( goldenResult ) , true ) ; } } ) ; } it ( "testRegressionTest" , ( ) => { compareToGoldenFile ( "http://www.google.com/" , ErrorCorrectionLevel . M , 99 , "renderer-test-01.png" ) ; } ) ; } ) ;