xml/legacyminidomparser/XMLParser/test/t_GmxmlParser.CPP
changeset 34 c7e9f1c97567
parent 25 417699dc19c9
child 36 172b09aa4eb6
equal deleted inserted replaced
25:417699dc19c9 34:c7e9f1c97567
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32test.h>
       
    17 #include <e32std.h>
       
    18 #include <f32file.h>
       
    19 #include <gmxmlparser.h>
       
    20 
       
    21 #include "GmxmlTestsetup.h"
       
    22 
       
    23 
       
    24 _LIT(KGMXMLParserTest,"T_GMXMLParser");
       
    25 class CTestGMXMLParser;
       
    26 
       
    27 RTest test(KGMXMLParserTest);
       
    28 LOCAL_D CTestGMXMLParser* testParser;
       
    29 LOCAL_D CActiveScheduler* scheduler;
       
    30 
       
    31 //------------------------------------------------------------------------------
       
    32 
       
    33 class CStreamErrorDataSupplier : public CBase, public MMDXMLParserDataProvider
       
    34 	{
       
    35 public:
       
    36 	static CStreamErrorDataSupplier * NewL();
       
    37 	~CStreamErrorDataSupplier () {}
       
    38 
       
    39 	// From MMDXMLParserDataProvided
       
    40 	void GetData(TPtrC8& aPtr, TRequestStatus &aStatus);
       
    41 	void Disconnect() {}
       
    42 	};
       
    43 
       
    44 //------------------------------------------------------------------------------
       
    45 
       
    46 CStreamErrorDataSupplier* CStreamErrorDataSupplier::NewL()
       
    47 	{
       
    48 	CStreamErrorDataSupplier* self = new (ELeave) CStreamErrorDataSupplier();
       
    49 	return self;
       
    50 	}
       
    51 
       
    52 //------------------------------------------------------------------------------
       
    53 // From MMDXMLParserDataProvided
       
    54 void CStreamErrorDataSupplier::GetData(TPtrC8& /*aPtr*/, TRequestStatus &aStatus)
       
    55 	{
       
    56 	TRequestStatus *s = &aStatus;
       
    57 	User::RequestComplete(s, KDataStreamError);
       
    58 	return;
       
    59 	}
       
    60 
       
    61 //------------------------------------------------------------------------------
       
    62 
       
    63 class CTestGMXMLParser : public CActive, public MMDXMLParserObserver
       
    64 	{
       
    65 public:
       
    66 	static CTestGMXMLParser* NewLC();
       
    67 	~CTestGMXMLParser();
       
    68 	void ConstructL();
       
    69 	void RunTestL();
       
    70 
       
    71 public: // from CActive
       
    72 	void DoCancel();
       
    73 	void RunL();
       
    74 
       
    75 public: // from MMDXMLParserObserver
       
    76 	void ParseFileCompleteL();
       
    77 
       
    78 private:
       
    79 	CTestGMXMLParser();
       
    80 
       
    81 public:
       
    82 	TBool iParseFileCompleted; // Whether the ParseFileCompleted was called.
       
    83 	TBool iParserCalled;       // Whether the GMXMLParser::ParseSource was called
       
    84 
       
    85 private:
       
    86 	CMDXMLParser* iParser;
       
    87 	CStreamErrorDataSupplier* iDataSupplier;
       
    88 	CTestTimer* iTimer;
       
    89 
       
    90 	static const TTimeIntervalMicroSeconds32 TestParserTimeout;
       
    91 	};
       
    92 
       
    93 //------------------------------------------------------------------------------
       
    94 
       
    95 const TTimeIntervalMicroSeconds32 CTestGMXMLParser::TestParserTimeout = 10000000; // 10s
       
    96 
       
    97 //------------------------------------------------------------------------------
       
    98 
       
    99 CTestGMXMLParser* CTestGMXMLParser::NewLC()
       
   100 	{
       
   101 	CTestGMXMLParser* self = new (ELeave) CTestGMXMLParser();
       
   102 	CleanupStack::PushL(self);
       
   103 	self->ConstructL();
       
   104 	return self;
       
   105 	}
       
   106 
       
   107 //------------------------------------------------------------------------------
       
   108 
       
   109 CTestGMXMLParser::~CTestGMXMLParser()
       
   110 	{
       
   111 	delete iParser;
       
   112 	delete iDataSupplier;
       
   113 	delete iTimer;
       
   114 	}
       
   115 
       
   116 //------------------------------------------------------------------------------
       
   117 
       
   118 CTestGMXMLParser::CTestGMXMLParser() : CActive(EPriorityStandard), iParseFileCompleted(EFalse), iParserCalled(EFalse)
       
   119 	{
       
   120 	}
       
   121 
       
   122 //------------------------------------------------------------------------------
       
   123 
       
   124 void CTestGMXMLParser::ConstructL()
       
   125 	{
       
   126 	iParser = CMDXMLParser::NewL(this);
       
   127 	iTimer = CTestTimer::NewL();
       
   128 
       
   129 	CActiveScheduler::Add(this);
       
   130 
       
   131 	TRequestStatus *s = &iStatus;
       
   132 	User::RequestComplete(s, KErrNone);
       
   133 	SetActive();
       
   134 	}
       
   135 
       
   136 //------------------------------------------------------------------------------
       
   137 
       
   138 void CTestGMXMLParser::RunL()
       
   139 	{
       
   140 	RunTestL();
       
   141 	}
       
   142 
       
   143 //------------------------------------------------------------------------------
       
   144 
       
   145 void CTestGMXMLParser::DoCancel()
       
   146 	{
       
   147 	}
       
   148 
       
   149 //------------------------------------------------------------------------------
       
   150 
       
   151 void CTestGMXMLParser::RunTestL()
       
   152 	{
       
   153 	if (!iParserCalled)
       
   154 		{
       
   155 		delete iDataSupplier;
       
   156 		iDataSupplier = NULL;
       
   157 		iDataSupplier = CStreamErrorDataSupplier::NewL();
       
   158 		iParser->ParseSource(iDataSupplier);
       
   159 
       
   160 		iStatus = KRequestPending;
       
   161 		SetActive();
       
   162 		iParserCalled = ETrue;
       
   163 		// Create a timer to stop the active scheduler if ParseFileComplete not called.
       
   164 		iTimer->AfterReq(CTestGMXMLParser::TestParserTimeout, iStatus);
       
   165 		}
       
   166 	else
       
   167 		CActiveScheduler::Stop();
       
   168 	}
       
   169 
       
   170 //------------------------------------------------------------------------------
       
   171 
       
   172 void CTestGMXMLParser::ParseFileCompleteL()
       
   173 	{
       
   174 	// Success.
       
   175 	iParseFileCompleted = ETrue;
       
   176 
       
   177 	TRequestStatus *s = &iStatus;
       
   178 	User::RequestComplete(s, KErrNone);
       
   179 	}
       
   180 
       
   181 //------------------------------------------------------------------------------
       
   182 
       
   183 LOCAL_C void doTestsL()
       
   184 	{
       
   185 	scheduler = new (ELeave) CActiveScheduler;
       
   186 	CleanupStack::PushL(scheduler);
       
   187 	CActiveScheduler::Install( scheduler );
       
   188 	testParser = CTestGMXMLParser::NewLC();
       
   189 
       
   190 	theUtils->WriteComment(_L("\nPerforming Tests\n"));
       
   191 	theUtils->Start(_L("Testing GMXMLParser receiving KDataStreamError from MMDXMLParserDataProvider::GetData()"));
       
   192 	CActiveScheduler::Start();
       
   193 	theUtils->Complete();
       
   194 
       
   195 	// Succeed only if the GMXMLParser::ParserSource was called and it resulted
       
   196 	// in ParseFileCompleted being called.
       
   197 	if (!testParser->iParserCalled || !testParser->iParseFileCompleted)
       
   198 		{
       
   199 		theUtils->WriteComment(_L("\nParseFileCompleted was not called within the timeout (10s)!"));
       
   200 		User::Leave(KErrGeneral);
       
   201 		}
       
   202 
       
   203 	CleanupStack::PopAndDestroy(2);  //testParser, ischeduler
       
   204 	}
       
   205 
       
   206 //------------------------------------------------------------------------------
       
   207