Skip to content

Commit

Permalink
Issue 886 Actionscript test
Browse files Browse the repository at this point in the history
git-svn-id: https://zxing.googlecode.com/svn/trunk@1842 59b500cc-1b3d-0410-9834-0bbf25fbcc57
  • Loading branch information
srowen committed Jun 29, 2011
1 parent 776e9b6 commit f6525fc
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 10 deletions.
51 changes: 42 additions & 9 deletions actionscript/core/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,30 @@
* See the License for the specific language governing permissions and
* limitations under the License.
-->
<project name="youtube-as3-player-helper" default="build" basedir="../">
<project name="ZXing" default="build" basedir="../">
<property environment="env" />
<property file="${basedir}/build.properties.local" />
<property file="${basedir}/../../build.properties"/>
<property file="${basedir}/../build.properties"/>
<property name="FLEX_HOME" location="${env.FLEX_HOME}" />
<property name="dist" location="${basedir}/bin" />
<property name="src" location="${basedir}/src" />
<property name="dist" location="${basedir}/core/bin" />
<property name="src" location="${basedir}/core/src" />
<property name="test" location="${basedir}/core/test" />
<property name="assets" location="${basedir}/../core/test/data" />
<property name="libs" location="${basedir}/core/libs" />
<property name="output" location="${basedir}/core/bin/output" />
<property name="report" location="${basedir}/core/bin/report" />

<fail unless="FLEX_HOME"
message="Error: define FLEX_HOME env. var. with a path to Flex4 SDK" />

<fail unless="version"
message="Error: version is undefined." />

<property name="libraryName" value="zxing-${version}.swc" />

<taskdef resource="flexTasks.tasks"
classpath="${FLEX_HOME}/ant/lib/flexTasks.jar" />
<taskdef resource="flexUnitTasks.tasks"
classpath="${libs}/flexUnitTasks-4.1.0-8.jar" />

<target name="clean">
<delete>
Expand All @@ -44,19 +50,46 @@
</target>

<target name="build" depends="init">
<fileset id="srcFiles" dir="src">
<fileset id="srcFiles" dir="${src}">
<include name="**/*.as"/>
</fileset>

<pathconvert property="classes" pathsep="," refid="srcFiles">
<chainedmapper>
<globmapper from="${basedir}/src/*" to="*"></globmapper>
<globmapper from="${src}/*" to="*"></globmapper>
<mapper type="package" from="*.as" to="*"/>
</chainedmapper>
</pathconvert>

<compc output="${dist}/${libraryName}" include-classes="${classes}">
<source-path path-element="${src}"/>
</compc>
</target>

<target name="compile.tests" depends="build">
<mxmlc file="${test}/com/google/zxing/testrunner/ZXingTestsRunner.mxml"
output="${output}/ZXingTestsRunner.swf"
debug="true">
<library-path dir="${dist}" append="true">
<include name="*.swc"/>
</library-path>
<library-path dir="${libs}" append="true">
<include name="*.swc"/>
</library-path>
<library-path dir="${FLEX_HOME}/frameworks/libs/air" append="true">
<include name="*.swc"/>
</library-path>
<library-path dir="${FLEX_HOME}/frameworks/libs" append="true">
<include name="framework.swc"/>
</library-path>
<source-path path-element="${test}" />
<source-path path-element="${assets}" />
<compiler.verbose-stacktraces>true</compiler.verbose-stacktraces>
<compiler.headless-server>true</compiler.headless-server>
</mxmlc>
<copy todir="${output}">
<fileset dir="${test}/com/google/zxing/testrunner">
<include name="zxing*.xml" />
</fileset>
</copy>
</target>

</project>
180 changes: 180 additions & 0 deletions actionscript/core/test/com/google/zxing/EncodeDecodeEAN8Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* Copyright 2011 ZXing authors
*
* 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.
*/
package com.google.zxing {

import com.adobe.images.PNGEncoder;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.ResultParser;
import com.google.zxing.common.ByteMatrix;
import com.google.zxing.common.GlobalHistogramBinarizer;
import com.google.zxing.common.flexdatatypes.HashTable;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.net.FileReference;
import flash.utils.ByteArray;

import mx.core.BitmapAsset;

import org.flexunit.asserts.assertEquals;
import org.flexunit.asserts.assertTrue;
import org.flexunit.asserts.fail;

public class EncodeDecodeEAN8Test {

[Embed(source="/blackbox/ean8-1/1.gif")]
private var Ean8_48512343:Class;

[Embed(source="/data/ean8/ean8_48512343.png")]
private var ZXingGeneratedEan8_48512343:Class;

private var imageWidth:int = 338;
private var imageHeight:int = 323;

[Test(description="Read EAN8 and validate decoded number.")]
public function testDecodedEAN8ShouldBeEqualToEncodedNumber():void {
var ean8image:BitmapAsset = new Ean8_48512343() as BitmapAsset;
var decodedNumber:Number = decodeEAN8BarcodeImage(ean8image.bitmapData);
assertEquals(48512343, decodedNumber);
}

[Test(async,
description="Ensure an encoded EAN8 image equals to expected.")]
public function testEncodedEAN8ImageShouldBeEqualToTarget():void {
var testCode:Number = 48512343;
var expectedEan8image:BitmapAsset =
new ZXingGeneratedEan8_48512343() as BitmapAsset;
var bitmap:Bitmap = generateEANBarcodeImage(testCode,
expectedEan8image.width,
expectedEan8image.height);
var diffObject:Object =
expectedEan8image.bitmapData.compare(bitmap.bitmapData);
assertTrue("diffObject should be int",
diffObject is int);
assertEquals(0, diffObject as int);
}

[Test(description="Encode a number, decode it, and compare.")]
public function encodedEAN8shouldBeDecodedSuccessfully():void {
var testCode:Number = 48512343;
var decodedNumber:Number = encodeAndDecode(testCode);
assertEquals(testCode, decodedNumber);
}

[Test(description="Bug: Endoding and decoding 1 fails")]
public function shouldEncodeAndDecodeAnyValidEAN8number():void {
var testCode:Number = 12345678;
var decodedNumber:Number = encodeAndDecode(testCode);
assertEquals(testCode, decodedNumber);
}

private function encodeAndDecode(testCode:Number):Number {
var bitmap:Bitmap = generateEANBarcodeImage(testCode, imageWidth,
imageHeight);
var decodedNumber:Number = decodeEAN8BarcodeImage(bitmap.bitmapData);
return decodedNumber;
}

public function saveBitmapToImage(bitmapData:BitmapData, fileName:String,
onFileSaveComplete:Function):void {
var imageBytes:ByteArray = PNGEncoder.encode(bitmapData);
var file:FileReference = new FileReference();
file.addEventListener(Event.COMPLETE, onFileSaveComplete);
file.save(imageBytes, fileName);
}

public function generateEANBarcodeImage(number:Number, imageWidth:Number,
imageHeight:Number):Bitmap {
var contents:String = formatEAN8String(number);
var resultBits:Array = encodeEAN8(contents);
var bitmap:Bitmap = generateImage(resultBits, imageWidth, imageHeight);
return bitmap;
}

public function decodeEAN8BarcodeImage(imageBitmapData:BitmapData):Number {
var resultNumber:Number;
var source:BufferedImageLuminanceSource =
new BufferedImageLuminanceSource(imageBitmapData);
var luminance:BinaryBitmap =
new BinaryBitmap(new GlobalHistogramBinarizer(source));
var hints:HashTable = new HashTable();
hints.Add(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.EAN_8);
var reader:MultiFormatReader = new MultiFormatReader();
var result:Result = reader.decode(luminance, hints);
if (result != null) {
var parsedResult:ParsedResult = ResultParser.parseResult(result);
var resultText:String = parsedResult.getDisplayResult();
resultNumber = Number(resultText);
}
return resultNumber;
}

private function generateImage(resultBits:Array, imageWidth:Number,
imageHeight:Number):Bitmap {
// generate an image
var bitmapData:BitmapData = new BitmapData(imageWidth, imageHeight, false,
0xFFFFFF);
var bitmap:Bitmap = new Bitmap(bitmapData);
for (var h:int = 0; h < bitmapData.height; h++) {
var bmpdwidth:int = bitmapData.width;
var padding:int = bmpdwidth * 0.12;
var barsWidth:int = bmpdwidth - padding;
for (var w:int = 0; w < barsWidth; w++) {
if (resultBits[Math.round(w * (resultBits.length / barsWidth))]
== 0) {
bitmapData.setPixel(w + padding / 2, h, 0);
}
else {
bitmapData.setPixel(w + padding / 2, h, 0xFFFFFF);
}
}
}
return bitmap;
}

private function encodeEAN8(contents:String):Array {
var barcodeType:BarcodeFormat = BarcodeFormat.EAN_8;
var myWriter:MultiFormatWriter = new MultiFormatWriter();
try {
var result:ByteMatrix = myWriter.encode(contents, barcodeType)
as ByteMatrix;
} catch (e:Error) {
fail("An error occured when making the barcode: " + e);
}
var resultBits:Array = new Array(result.height());
for (var i:int = 0; i < result.height(); i++) {
resultBits[i] = result._get(i, 0);
}
return resultBits;
}

private function formatEAN8String(number:Number):String {
var inputlength:Number = 8;
var barCodeFormat:String = "EAN8";

var contents:String = String(number);
if (contents.length > inputlength) {
fail(barCodeFormat + ' can only contain ' + inputlength + ' digits');
return null;
}
while (contents.length < inputlength) {
contents = "0" + contents;
}
return contents;
}
}
}
26 changes: 26 additions & 0 deletions actionscript/core/test/com/google/zxing/IntegrationTestsSuite.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2011 ZXing authors
*
* 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.
*/
package com.google.zxing {

[Suite]
[RunWith("org.flexunit.runners.Suite")]
public class IntegrationTestsSuite {
public var encodeDecodeTest:EncodeDecodeEAN8Test;

public function IntegrationTestsSuite() {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0"?>
<!--
* Copyright 2011 ZXing authors
*
* 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.
-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:flexUnitUIRunner="http://www.adobe.com/2009/flexUnitUIRunner"
creationComplete="runTests()"
width="1024" height="768">

<mx:Script>
<![CDATA[
import com.google.zxing.IntegrationTestsSuite;
import org.flexunit.runner.FlexUnitCore;
public function runTests():void {
var core:FlexUnitCore = new FlexUnitCore();
core.addListener(uiListener);
core.run(IntegrationTestsSuite);
}
]]>
</mx:Script>
<flexUnitUIRunner:TestRunnerBase id="uiListener"
width="100%" height="100%" />
</mx:Application>
Loading

0 comments on commit f6525fc

Please sign in to comment.