activityfw/tsutils/tsrc/t_tsgraphicfilescalinghandler/src/t_tsgraphicfilescalinghandler.cpp
author Jaakko Haukipuro (Nokia-MS/Oulu) <Jaakko.Haukipuro@nokia.com>
Thu, 16 Sep 2010 12:11:40 +0100
changeset 117 c63ee96dbe5f
permissions -rw-r--r--
Missing activityfw and taskswitcher components - fix for Bug 3670

//Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
//All rights reserved.
//This component and the accompanying materials are made available
//under the terms of "Eclipse Public License v1.0"
//which accompanies this distribution, and is available
//at the URL "http://www.eclipse.org/legal/epl-v10.html".

//Initial Contributors:
//Nokia Corporation - initial contribution.

//Contributors:

//Description:

/////////////////////////////////////////////////////////////////////

// INCLUDED FILES
#include <QtTest/QtTest>
#include "t_tsgraphicfilescalinghandler.h"

_LIT(KFileName, "c:\\data\\Images\\Pictures\\image_file.png");
_LIT8(KMimeType, "image/png");
QString KFileName2("c:\\data\\Images\\Pictures\\image_file.png");

const TSize KOriginalSize(480, 236);
const TSize KWanted_TheSameRatio(KOriginalSize.iWidth/2, KOriginalSize.iHeight/2);
const TSize KWanted_LessWidth(KOriginalSize.iWidth/2, KOriginalSize.iHeight);
const TSize KWanted_LessHeight(KOriginalSize.iWidth, KOriginalSize.iHeight/2);

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
QTEST_MAIN(T_CTsGraphicFileScalingHandler)

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
T_CTsGraphicFileScalingHandler::T_CTsGraphicFileScalingHandler(QObject *parent) :
    QObject(parent),
    mErrorCallBack(KErrNone)
{
    User::LeaveIfError(mFs.Connect());
    User::LeaveIfError(RFbsSession::Connect(mFs));
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
T_CTsGraphicFileScalingHandler::~T_CTsGraphicFileScalingHandler()
{
    mFs.Close();
    RFbsSession::Disconnect();
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::WaitActive(TInt aTime)
{
    CActiveSchedulerWait *wait = new (ELeave)CActiveSchedulerWait();
    CPeriodic *periodic = CPeriodic::NewL(CActive::EPriorityIdle);
    periodic->Start(aTime, aTime, TCallBack(CallStop, wait));
    wait->Start();
    delete periodic;
    delete wait;
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
TInt T_CTsGraphicFileScalingHandler::CallStop(TAny *aWait)
{
    (static_cast<CActiveSchedulerWait*>(aWait))->AsyncStop();
    return 1;
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testGraphicFileScalingIgnoreAspectRatio_LessWidth()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;

    TRAPD(errNo, screenShotHadnler =
        CTsGraphicFileScalingHandler::NewL(*this,
                                    mFs,
                                    KFileName,
                                    KMimeType,
                                    KWanted_LessWidth
                                    /*CTsGraphicFileScalingHandler::EIgnoreAspectRatio*/));

    QCOMPARE(errNo, KErrNone);
    WaitActive(5000000);

    QVERIFY(!mErrorCallBack);
    QVERIFY(!mPixMapCallBack.isNull());
    QCOMPARE(mPixMapCallBack.width(), KWanted_LessWidth.iWidth);
    QCOMPARE(mPixMapCallBack.height(), KWanted_LessWidth.iHeight);
    delete screenShotHadnler;
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testGraphicFileScalingIgnoreAspectRatio_LessHeight()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;

    TRAPD(errNo, screenShotHadnler =
        CTsGraphicFileScalingHandler::NewL(*this,
                                    mFs,
                                    KFileName,
                                    KMimeType,
                                    KWanted_LessHeight
                                    /*CTsGraphicFileScalingHandler::EIgnoreAspectRatio*/));

    QCOMPARE(errNo, KErrNone);
    WaitActive(5000000);

    QVERIFY(!mErrorCallBack);
    QVERIFY(!mPixMapCallBack.isNull());
    QCOMPARE(mPixMapCallBack.width(), KWanted_LessHeight.iWidth);
    QCOMPARE(mPixMapCallBack.height(), KWanted_LessHeight.iHeight);
    delete screenShotHadnler;
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testGraphicFileScalingIgnoreAspectRatio_TheSameRatio()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;

    TRAPD(errNo, screenShotHadnler =
        CTsGraphicFileScalingHandler::NewL(*this,
                                    mFs,
                                    KFileName,
                                    KMimeType,
                                    KWanted_TheSameRatio
                                    /*CTsGraphicFileScalingHandler::EIgnoreAspectRatio*/));

    QCOMPARE(errNo, KErrNone);
    WaitActive(5000000);

    QVERIFY(!mErrorCallBack);
    QVERIFY(!mPixMapCallBack.isNull());
    QCOMPARE(mPixMapCallBack.width(), KWanted_TheSameRatio.iWidth);
    QCOMPARE(mPixMapCallBack.height(), KWanted_TheSameRatio.iHeight);
    delete screenShotHadnler;
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testGraphicFileScalingKeepAspectRatio_LessWidth()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;

    TRAPD(errNo, screenShotHadnler =
        CTsGraphicFileScalingHandler::NewL(*this,
                                    mFs,
                                    KFileName,
                                    KMimeType,
                                    KWanted_LessWidth,
                                    CTsGraphicFileScalingHandler::EKeepAspectRatio));

    QCOMPARE(errNo, KErrNone);
    WaitActive(5000000);

    QVERIFY(!mErrorCallBack);
    QVERIFY(!mPixMapCallBack.isNull());
    QVERIFY(mPixMapCallBack.width() <= KWanted_LessWidth.iWidth);
    QVERIFY(mPixMapCallBack.height() <= KWanted_LessWidth.iHeight);
    delete screenShotHadnler;
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testGraphicFileScalingKeepAspectRatio_LessHeight()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;

    TRAPD(errNo, screenShotHadnler =
        CTsGraphicFileScalingHandler::NewL(*this,
                                    mFs,
                                    KFileName,
                                    KMimeType,
                                    KWanted_LessHeight,
                                    CTsGraphicFileScalingHandler::EKeepAspectRatio));

    QCOMPARE(errNo, KErrNone);
    WaitActive(5000000);

    QVERIFY(!mErrorCallBack);
    QVERIFY(!mPixMapCallBack.isNull());
    QVERIFY(mPixMapCallBack.width() <= KWanted_LessHeight.iWidth);
    QVERIFY(mPixMapCallBack.height() <= KWanted_LessHeight.iHeight);
    delete screenShotHadnler;
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testGraphicFileScalingKeepAspectRatio_TheSameRatio()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;

    TRAPD(errNo, screenShotHadnler =
        CTsGraphicFileScalingHandler::NewL(*this,
                                    mFs,
                                    KFileName,
                                    KMimeType,
                                    KWanted_TheSameRatio,
                                    CTsGraphicFileScalingHandler::EKeepAspectRatio));

    QCOMPARE(errNo, KErrNone);
    WaitActive(5000000);

    QVERIFY(!mErrorCallBack);
    QVERIFY(!mPixMapCallBack.isNull());
    QVERIFY(mPixMapCallBack.width() <= KWanted_TheSameRatio.iWidth);
    QVERIFY(mPixMapCallBack.height() <= KWanted_TheSameRatio.iHeight);
    delete screenShotHadnler;
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testGraphicFileScalingKeepAspectRatioByExpanding_LessWidth()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;

    TRAPD(errNo, screenShotHadnler =
        CTsGraphicFileScalingHandler::NewL(*this,
                                    mFs,
                                    KFileName,
                                    KMimeType,
                                    KWanted_LessWidth,
                                    CTsGraphicFileScalingHandler::EKeepAspectRatioByExpanding));

    QCOMPARE(errNo, KErrNone);
    WaitActive(5000000);

    QVERIFY(!mErrorCallBack);
    QVERIFY(!mPixMapCallBack.isNull());
    QCOMPARE(mPixMapCallBack.width(), KWanted_LessWidth.iWidth);
    QCOMPARE(mPixMapCallBack.height(), KWanted_LessWidth.iHeight);
    delete screenShotHadnler;
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testGraphicFileScalingKeepAspectRatioByExpanding_LessHeight()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;

    TRAPD(errNo, screenShotHadnler =
        CTsGraphicFileScalingHandler::NewL(*this,
                                    mFs,
                                    KFileName,
                                    KMimeType,
                                    KWanted_LessHeight,
                                    CTsGraphicFileScalingHandler::EKeepAspectRatioByExpanding));

    QCOMPARE(errNo, KErrNone);
    WaitActive(5000000);

    QVERIFY(!mErrorCallBack);
    QVERIFY(!mPixMapCallBack.isNull());
    QCOMPARE(mPixMapCallBack.width(), KWanted_LessHeight.iWidth);
    QCOMPARE(mPixMapCallBack.height(), KWanted_LessHeight.iHeight);
    delete screenShotHadnler;
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testGraphicFileScalingKeepAspectRatioByExpanding_TheSameRatio()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;

    TRAPD(errNo, screenShotHadnler =
        CTsGraphicFileScalingHandler::NewL(*this,
                                    mFs,
                                    KFileName,
                                    KMimeType,
                                    KWanted_TheSameRatio,
                                    CTsGraphicFileScalingHandler::EKeepAspectRatioByExpanding));

    QCOMPARE(errNo, KErrNone);
    WaitActive(5000000);

    QVERIFY(!mErrorCallBack);
    QVERIFY(!mPixMapCallBack.isNull());
    QCOMPARE(mPixMapCallBack.width(), KWanted_TheSameRatio.iWidth);
    QCOMPARE(mPixMapCallBack.height(), KWanted_TheSameRatio.iHeight);
    delete screenShotHadnler;
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testGraphicFileScalingInvalidImagePath()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;
    TSize newSize(240, 118);
    _LIT(wrongImagePath, "c:\\data\\Images\\Pictures\\<image_file.png");

    TRAPD(errNo, screenShotHadnler =
            CTsGraphicFileScalingHandler::NewL(*this,
                                        mFs,
                                        wrongImagePath,
                                        KMimeType,
                                        newSize);
    );

    QCOMPARE(errNo, KErrPathNotFound);
    WaitActive(5000000);
    delete screenShotHadnler;
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testGraphicFileScalingEmptyImagePath()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;
    TSize newSize(240, 118);
    _LIT(wrongImagePath, "");

    TRAPD(errNo, screenShotHadnler =
            CTsGraphicFileScalingHandler::NewL(*this,
                                        mFs,
                                        wrongImagePath,
                                        KMimeType,
                                        newSize);
    );

    QCOMPARE(errNo, KErrPathNotFound);
    WaitActive(5000000);
    delete screenShotHadnler;
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testGraphicFileScalingEmptyMimeType()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;
    TSize newSize(240, 118);
    _LIT8(wrongMimeType, "");

    TRAPD(errNo,
        screenShotHadnler = CTsGraphicFileScalingHandler::NewL(*this,
                                        mFs,
                                        KFileName,
                                        wrongMimeType,
                                        newSize);
    );

    QCOMPARE(errNo, KErrBadName);
    WaitActive(5000000);
    delete screenShotHadnler;
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testGraphicFileScalingScalingToIncorrectWidthSize()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;
    TSize newSize(-20, 118);

    TRAPD(errNo, screenShotHadnler =
            CTsGraphicFileScalingHandler::NewL(*this,
                                        mFs,
                                        KFileName,
                                        KMimeType,
                                        newSize);
    );

    QCOMPARE(errNo, KErrCorrupt);
    WaitActive(5000000);
    delete screenShotHadnler;
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testGraphicFileScalingScalingToIncorrectHeightSize()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;
    TSize newSize(240, -20);

    TRAPD(errNo, screenShotHadnler =
            CTsGraphicFileScalingHandler::NewL(*this,
                                        mFs,
                                        KFileName,
                                        KMimeType,
                                        newSize);
    );

    QCOMPARE(errNo, KErrCorrupt);
    WaitActive(5000000);
    delete screenShotHadnler;
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testGraphicFileScalingScalingToZeroSize()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;
    TSize newSize(0, 0);

    TRAPD(errNo, screenShotHadnler =
            CTsGraphicFileScalingHandler::NewL(*this,
                                        mFs,
                                        KFileName,
                                        KMimeType,
                                        newSize);
    );

    QCOMPARE(errNo, KErrCorrupt);
    WaitActive(5000000);
    delete screenShotHadnler;
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testDoCancel()
{
    CTsGraphicFileScalingHandler *screenShotHadnler = 0;

    TRAPD(errNo, screenShotHadnler =
        CTsGraphicFileScalingHandler::NewL(*this,
                                    mFs,
                                    KFileName,
                                    KMimeType,
                                    KWanted_LessWidth));

    QCOMPARE(errNo, KErrNone);

    screenShotHadnler->Cancel();

    WaitActive(5000000);
    QCOMPARE(mErrorCallBack, KErrCancel);
    QVERIFY(mPixMapCallBack.isNull());
    delete screenShotHadnler;
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testCFbsBitmapScalingToCorrectSize()
{
    QPixmap imputPixMap(KFileName2);

    CFbsBitmap *cFbsBitmap = new(ELeave)CFbsBitmap();
    QVERIFY(cFbsBitmap = imputPixMap.toSymbianCFbsBitmap());

    CTsGraphicFileScalingHandler *screenShotHadnler = 0;

    TRAPD(errNoo, screenShotHadnler =
        CTsGraphicFileScalingHandler::NewL(*this,
                                    *cFbsBitmap,
                                    KWanted_LessHeight));

    QCOMPARE(errNoo, KErrNone);
    WaitActive(5000000);

    QVERIFY(!mErrorCallBack);
    QVERIFY(!mPixMapCallBack.isNull());
    QCOMPARE(mPixMapCallBack.width(), KWanted_LessHeight.iWidth);
    QCOMPARE(mPixMapCallBack.height(), KWanted_LessHeight.iHeight);

    delete screenShotHadnler;
    delete cFbsBitmap;
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testCFbsBitmapScalinToIncorrectWidthSize()
{
    QPixmap imputPixMap(KFileName2);

    CFbsBitmap *cFbsBitmap = new(ELeave)CFbsBitmap();
    QVERIFY(cFbsBitmap = imputPixMap.toSymbianCFbsBitmap());

    CTsGraphicFileScalingHandler *screenShotHadnler = 0;
    TSize newSize(-20, 118);

    TRAPD(errNoo, screenShotHadnler =
        CTsGraphicFileScalingHandler::NewL(*this,
                                    *cFbsBitmap,
                                    newSize));

    QCOMPARE(errNoo, KErrCorrupt);
    WaitActive(5000000);

    delete screenShotHadnler;
    delete cFbsBitmap;
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::testCFbsBitmapScalinToIncorrectHeightSize()
{
    QPixmap imputPixMap(KFileName2);

    CFbsBitmap *cFbsBitmap = new(ELeave)CFbsBitmap();
    QVERIFY(cFbsBitmap = imputPixMap.toSymbianCFbsBitmap());

    CTsGraphicFileScalingHandler *screenShotHadnler = 0;
    TSize newSize(240, -20);

    TRAPD(errNoo, screenShotHadnler =
        CTsGraphicFileScalingHandler::NewL(*this,
                                    *cFbsBitmap,
                                    newSize));

    QCOMPARE(errNoo, KErrCorrupt);
    WaitActive(5000000);

    delete screenShotHadnler;
    delete cFbsBitmap;
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
void T_CTsGraphicFileScalingHandler::ImageReadyCallBack(TInt error,
                                                   const CFbsBitmap *bitmap)
{
    CFbsBitmap *retBitmap = const_cast<CFbsBitmap *>(bitmap);

    mErrorCallBack = error;
    mPixMapCallBack = QPixmap();

    if (retBitmap && !error) {
        mPixMapCallBack = QPixmap::fromSymbianCFbsBitmap(retBitmap);
    }
}

//  End of KFileName