/***************************************************************************
 *   Copyright (C) 2005 by Ian Reinhart Geiser                             *
 *   geiseri@sourcextreme.com                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QApplication>
#include <QByteArray>
#include <QSizeF>
#include <QLine>
#include <QPolygon>
#include <QList>

#include <dhwpage.h>

#include <QtTest/QtTest>

#include <qdebug.h>

#include "readbytes.h"

char HeaderBytes[] = {	0x41, 0x43, 0x45, 0x43, 0x41, 0x44, 0x5f, 0x44, 0x49, 0x47, 0x49,
			0x4d, 0x45, 0x4d, 0x4f, 0x5f, 0x48, 0x41, 0x4e, 0x44, 0x57, 0x52,
			0x49, 0x54, 0x49, 0x4e, 0x47, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x01,
			0x38, 0x17, 0x10, 0x21, 0x01, 0x00, 0x00 };
			// Version 1
			// 5944 x 8464 ?
			// A5 paper

char StrokeBytes[] = {	0x81, 0x4e,0x1f, 0x19, 0x19, 0x52, 0x1f, 0x1e, 0x19, 0x80, 0x6b, 0x10, 0x1b, 0x21, 0x88, 0x7f,
			0x81, 0x4e,0x1f, 0x19, 0x19, 0x52, 0x1f, 0x1e, 0x19, 0x80, 0x6b, 0x10, 0x1b, 0x21, 0x88, 0x7f,
			0x81, 0x4e,0x1f, 0x19, 0x19, 0x52, 0x1f, 0x1e, 0x19, 0x80, 0x6b, 0x10, 0x1b, 0x21, 0x90, 0x01 };

char StrokeBytesFunny[] = { 0x80, 0x4e,0x1f, 0x19, 0x19, 0x52, 0x1f, 0x1e, 0x19, 0x80, 0x6b, 0x10, 0x1b, 0x21, 0x88, 0x7f }; // One sample has this, but its not in the spec.


// Pen up
// 0x80, 0x82, 0x84, 0x86
// read 4 bytes
// Pen down
// 0x81, 0x83, 0x85, 0x87
// read in 4 byte chunks until we get a penup, or end layer.
// 0x88 timestamp
// read 1 byte to get timestamp
// 0x90 end layer
// read 1 byte to get the layer

QTEST_MAIN(TestReadBytes)

void TestReadBytes::testReadPageType()
{
	QByteArray bytes(HeaderBytes, sizeof(HeaderBytes) );
	DHWPage page(bytes);
	QCOMPARE(page.readPageType(),DHWPage::A4);
}

void TestReadBytes::testReadVersion()
{
	QByteArray bytes(HeaderBytes, sizeof(HeaderBytes) );
	DHWPage page(bytes);
	QCOMPARE(page.readVersion(),1);
}

void TestReadBytes::testReadBoundingBox()
{
	QSizeF size(5944,8464);
	QByteArray bytes(HeaderBytes, sizeof(HeaderBytes) );
	DHWPage page(bytes);

	QSizeF readSize = page.readPageSize();
	QCOMPARE(readSize ,size );
}

void TestReadBytes::testReadPolyline()
{
	QByteArray headerBytes(HeaderBytes, sizeof(HeaderBytes) );
	QByteArray strokeBytes(StrokeBytes, sizeof(StrokeBytes) );
	DHWPage page(headerBytes + strokeBytes);

	// read from 40, get pen down, get coords until none left.
	QList<DHWLayer> layers = page.readLayerData();
	QCOMPARE(layers.count(),1);
}

void TestReadBytes::testReadCordinate()
{
	QByteArray headerBytes(HeaderBytes, sizeof(HeaderBytes) );
	QByteArray strokeBytes(StrokeBytes, sizeof(StrokeBytes) );
	DHWPage page(headerBytes + strokeBytes);

	QPointF point = page.readPoint( 41 );
	QPointF expected( 4046, 5239 );
	QCOMPARE(point,expected );
}

void TestReadBytes::testIdBytes()
{
	QByteArray strokeBytes(StrokeBytes, sizeof(StrokeBytes) );
	DHWPage page(strokeBytes);

	QCOMPARE( page.idBytes( 0 ), DHWPage::PenDown);
	QCOMPARE( page.idBytes( 2 ), DHWPage::Data);
}


