Issue 886 Actionscript test

git-svn-id: https://zxing.googlecode.com/svn/trunk@1842 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2011-06-29 06:16:23 +00:00
parent 776e9b6a06
commit f6525fcd73
5 changed files with 362 additions and 10 deletions

View file

@ -14,17 +14,21 @@
* 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." />
@ -32,6 +36,8 @@
<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>
@ -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>

View file

@ -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;
}
}
}

View file

@ -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() {
}
}
}

View file

@ -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>

View file

@ -7,4 +7,81 @@ Many improvements can be made and not all patches and updates have been applied
If you would like the help, feel free to join the zxing project and submit your improvements and bugfixes.
If you have questions, please send them to the zxing newsgroup.
Bas Vijfwinkel
Bas Vijfwinkel
Build Instructions
------------------
Building ZXing AS3 code requires:
* Flex4 SDK
http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4
* FlexUnit
http://flexunit.org/releases/flexunit-4.1.0-8-4.1.0.16076.zip
* AS3Corelib
https://github.com/downloads/mikechambers/as3corelib/as3corelib-.93.zip
* Apache Ant
http://ant.apache.org/
Setup
-----
1. Please download and unzip Flex4 SDK.
2. Define FLEX_HOME environment variable with a full path to Flex4 SDK folder.
On Mac or Linux:
$ export FLEX_HOME=/specify-full-path-to/flex4_sdk
on Win:
$ set FLEX_HOME=/specify-full-path-to/flex4_sdk
3. Download and unzip FlexUnit and AS3Corelib to core/libs folder. Make sure
that all swc and jar files are located directly under
./zxing-read-only/actionscript/core/libs:
$ ls -l libs
-rw-r----- 1 john smith 193727 Jun 28 14:18 as3corelib.swc
-rw-r--r-- 1 john smith 597386 Apr 13 16:57 flexUnitTasks-4.1.0-8.jar
-rw-r--r-- 1 john smith 192629 Apr 13 16:57 flexunit-4.1.0-8-as3_4.1.0.16076.swc
-rw-r--r-- 1 john smith 203196 Apr 13 16:57 flexunit-4.1.0-8-flex_4.1.0.16076.swc
-rw-r--r-- 1 john smith 2379 Apr 13 17:01 flexunit-aircilistener-4.1.0-8-4.1.0.16076.swc
-rw-r--r-- 1 john smith 11993 Apr 13 16:58 flexunit-cilistener-4.1.0-8-4.1.0.16076.swc
-rw-r--r-- 1 john smith 9686 Apr 13 16:58 flexunit-flexcoverlistener-4.1.0-8-4.1.0.16076.swc
-rw-r--r-- 1 john smith 227292 Apr 13 17:01 flexunit-uilistener-4.1.0-8-4.1.0.16076.swc
-rw-r--r-- 1 john smith 6332 Apr 13 16:58 fluint-extensions-4.1.0-8-4.1.0.16076.swc
4. Download and unzip Apache Ant. Add ant tool to the PATH.
Building ZXing SWC
------------------
To build zxing.swc, run ant:
$ cd zxing-read-only/actionscript/core
$ ant
The build should create zxing-1.6.swc in actionscript/core/bin folder.
Building and Running ZXing Tests
--------------------------------
To build tests, execute:
$ ant compile.tests
To run the tests, open bin/output/ZXingTestsRunner.swf in a Flash Player or a
web browser. Here is an example of running the tests with the standalone
Flash Player debugger:
$ flashplayerdebugger bin/output/ZXingTestsRunner.swf