taskswitcher/screenshotplugin/tsrc/t_screenshotplugin/t_screenshotplugin.cpp
changeset 117 c63ee96dbe5f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/taskswitcher/screenshotplugin/tsrc/t_screenshotplugin/t_screenshotplugin.cpp	Thu Sep 16 12:11:40 2010 +0100
@@ -0,0 +1,158 @@
+/*
+* Copyright (c) 2009 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:
+*
+*/
+#include <QtTest/QtTest>
+#include <w32std.h>
+#include <s32mem.h>
+#include <apgtask.h>
+#include <qdesktopwidget.h>
+#include <qsignalspy.h>
+#include <coecntrl.h>
+
+#include "t_screenshotplugin.h"
+#include "tstaskmonitorglobals.h"
+#include "tsscreenshotmsg.h"
+
+
+const int testTimeout(10000);
+const int homescreenUID(0x20022F35);
+// -----------------------------------------------------------------------------
+//
+// -----------------------------------------------------------------------------
+//
+class CScreenshotPluginClient: public CWsGraphic
+{
+public:
+    static CScreenshotPluginClient* NewLC(MScreenshotObserver& observer)
+    {
+        CScreenshotPluginClient* self = new (ELeave)CScreenshotPluginClient(observer);
+        CleanupStack::PushL(self);
+        self->BaseConstructL(TUid::Uid(0x200267AE), KNullDesC8);
+        return self;
+    }
+
+private:
+    CScreenshotPluginClient(MScreenshotObserver& observer)
+    :mObserver(observer)
+    {}
+    
+    void HandleMessage(const TDesC8& data)
+    {TRAP_IGNORE(HandleMessageL(data));}
+    
+    void HandleMessageL(const TDesC8& data)
+    {
+        RDesReadStream msgStream(data);
+        CleanupClosePushL(msgStream);
+        if(RegisterScreenshotMessage != msgStream.ReadInt32L()) {
+            User::Leave(KErrNotSupported);
+        }
+        
+        //parse msg to ACK provider ASAP and be sure that bitmap still exists
+        CTsScreenshotMsg* screenshotMsg = CTsScreenshotMsg::NewLC(msgStream); 
+        SendMessage(data);
+        
+        //reset stream
+        msgStream.Close();
+        msgStream.Open(data);
+        
+        //forward stream to storage
+        mObserver.ScreenshotTaken(screenshotMsg->windowGroupId(), 
+                                  screenshotMsg->screenshot().Handle(), 
+                                  0, 
+                                  screenshotMsg->priority());
+        
+        CleanupStack::PopAndDestroy(screenshotMsg);
+        CleanupStack::PopAndDestroy(&msgStream);
+    }
+    
+    void OnReplace()
+    {}
+
+private:
+    MScreenshotObserver &mObserver;
+};
+
+// -----------------------------------------------------------------------------
+//
+// -----------------------------------------------------------------------------
+//
+
+T_Screenshotplugin::T_Screenshotplugin() : testUi("test")
+{
+    testUi.show();
+}
+
+void T_Screenshotplugin::testPlugin()
+{
+    const bool appMovingOperations [] = {false, true, false};
+    QEventLoop loop;
+    connect(this,
+            SIGNAL(screenshotTaken(int, int, int, int)),
+            &loop,
+            SLOT(quit()));
+    
+    CScreenshotPluginClient::NewLC(*this);//create plugin client. test doesnt need pointer
+    
+    //just move tested app to foreground
+    QTimer::singleShot(testTimeout, &loop, SLOT(quit()));
+    moveApp(homescreenUID, true);
+    loop.exec();
+    
+    //!!!!Tested application should be in foreground!!!!!!
+    //Move tested app foreground/background and wait for screenshot/timeout
+    for (int iter(0), count(sizeof(appMovingOperations)/sizeof(bool)); iter < count; ++iter) {
+        QSignalSpy spy(this, SIGNAL(screenshotTaken(int, int, int, int)));
+        QTimer::singleShot(testTimeout, &loop, SLOT(quit()));
+        moveApp(homescreenUID, appMovingOperations[iter]);
+        loop.exec();
+        QTest::qWait(4000);
+        QCOMPARE(spy.count(), 1);
+    
+    }
+    //!!!!Tested application should be in background!!!!!!
+    QSignalSpy spy(this, SIGNAL(screenshotTaken(int, int, int, int)));
+    QTimer::singleShot(testTimeout, &loop, SLOT(quit()));
+    moveApp(homescreenUID, false);
+    loop.exec();
+    QCOMPARE(spy.count(), 0);
+    
+    CleanupStack::PopAndDestroy();//destroy client
+}
+
+// -----------------------------------------------------------------------------
+//
+// -----------------------------------------------------------------------------
+//
+void T_Screenshotplugin::ScreenshotTaken(int a, int b, int c, int d)
+{
+    emit screenshotTaken(a, b, c, d);
+}
+
+// -----------------------------------------------------------------------------
+//
+// -----------------------------------------------------------------------------
+//
+void T_Screenshotplugin::moveApp(int uid, bool foreground)
+{
+    TApaTaskList taskList(QApplication::desktop()->winId()->ControlEnv()->WsSession());
+    TApaTask task(taskList.FindApp(TUid::Uid(uid)));
+    if ( task.Exists() ) 
+        {
+        foreground ? task.BringToForeground() : task.SendToBackground();
+        }
+}
+
+QTEST_MAIN(T_Screenshotplugin)