lowlevellibsandfws/apputils/tsrc/T_CLIPB.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 1997-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 // Started by DWW, October 1996
       
    15 // Tests cliboard
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <e32test.h>
       
    20 #include <baclipb.h>
       
    21 #include <bautils.h>
       
    22 #include <f32file.h>
       
    23 #include <s32strm.h>
       
    24 #include <s32stor.h>
       
    25 #include <s32file.h>
       
    26 
       
    27 LOCAL_D RTest test(_L("T_CLIPB"));
       
    28 
       
    29 const TUid KClipboardFileUid={268435515};
       
    30 const TUid KUidNameClipboardType={77001};
       
    31 const TUid KUidAddressClipboardType={77002};
       
    32 const TUid KUidNumberClipboardType={77003};
       
    33 const TUid KUidTestType={77004};
       
    34 
       
    35 const TPtrC KClipboardFileCDrive=_L("C:\\System\\Data\\ClpBoard.cbd");
       
    36 const TPtrC KClipboardFileDDrive=_L("D:\\System\\Data\\ClpBoard.cbd");
       
    37 const TPtrC KClipboardFileEDrive=_L("E:\\System\\Data\\ClpBoard.cbd");
       
    38 /**
       
    39  * NOTE :- On order to run this test in WINS, there must exist a mapping for the drives
       
    40  * used above. So in Epoc32\Data add the following lines to epoc.ini...
       
    41  *
       
    42  * _epoc_drive_d \epoc32\wins\d
       
    43  * _epoc_drive_e \epoc32\wins\e
       
    44  *
       
    45  * only needed for drive D & E as there is a mapping for drive C.
       
    46  * Plus make sure that the the above directories (\epoc32\wins\d & e) exist.
       
    47  */
       
    48 
       
    49 class TClBase
       
    50 	{
       
    51 public:
       
    52 	virtual void CopyToClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary) const=0;
       
    53 	virtual TBool PasteFromClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary)=0;
       
    54 	void CopyL();
       
    55 	TBool PasteL();
       
    56 	TBool PasteWithoutClipboardL();
       
    57 private:
       
    58 	void DoCopyL(RFs& aFsSession);
       
    59 	TBool DoPasteL(RFs& aFsSession);
       
    60 	};
       
    61 
       
    62 void TClBase::CopyL()
       
    63 	{
       
    64 	RFs fsSession;
       
    65 	User::LeaveIfError(fsSession.Connect());
       
    66 	TRAPD(err,DoCopyL(fsSession));
       
    67 	fsSession.Close();
       
    68 	User::LeaveIfError(err);
       
    69 	}
       
    70 
       
    71 void TClBase::DoCopyL(RFs& aFsSession)
       
    72 	{
       
    73 	CClipboard* cb=CClipboard::NewForWritingLC(aFsSession);
       
    74 	CopyToClipboardL(cb->Store(),cb->StreamDictionary());
       
    75 	cb->CommitL();
       
    76 	CleanupStack::PopAndDestroy();
       
    77 	}
       
    78 
       
    79 TBool TClBase::PasteL()
       
    80 	{
       
    81 	RFs fsSession;
       
    82 	User::LeaveIfError(fsSession.Connect());
       
    83 	TBool res=EFalse;
       
    84 	TRAPD(err,res=DoPasteL(fsSession));
       
    85 	fsSession.Close();
       
    86 	User::LeaveIfError(err);
       
    87 	return(res);
       
    88 	}
       
    89 
       
    90 TBool TClBase::DoPasteL(RFs& aFsSession)
       
    91 	{
       
    92 	CClipboard* cb=CClipboard::NewForReadingLC(aFsSession);
       
    93 	TBool res=PasteFromClipboardL(cb->Store(),cb->StreamDictionary());
       
    94 	CleanupStack::PopAndDestroy();
       
    95 	return(res);
       
    96 	}
       
    97 
       
    98 class TClName : public TClBase
       
    99 	{
       
   100 public:
       
   101 	void CopyToClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary) const;
       
   102 	TBool PasteFromClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary);
       
   103 public:
       
   104 	TBuf<4> iName;
       
   105 	};
       
   106 
       
   107 class TClNameWithAddress : public TClName
       
   108 	{
       
   109 	void CopyToClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary) const;
       
   110 	TBool PasteFromClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary);
       
   111 public:
       
   112 	TBuf<10> iAddress;
       
   113 	};
       
   114 
       
   115 class TClNameWithNumber : public TClName
       
   116 	{
       
   117 	void CopyToClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary) const;
       
   118 	TBool PasteFromClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary);
       
   119 public:
       
   120 	TInt32 iNumber;
       
   121 	};
       
   122 
       
   123 class TClInteger : public TClBase
       
   124 	{
       
   125 public:
       
   126 	void CopyToClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary) const;
       
   127 	TBool PasteFromClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary);
       
   128 public:
       
   129 	TInt32 iInteger;
       
   130 	};
       
   131 
       
   132 void TClName::CopyToClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary) const
       
   133 	{
       
   134 	RStoreWriteStream stream;
       
   135 	TStreamId streamId=stream.CreateLC(aStore);
       
   136 	stream<<iName;
       
   137 	CleanupStack::PopAndDestroy();
       
   138 	aDictionary.AssignL(KUidNameClipboardType,streamId);
       
   139 	}
       
   140 
       
   141 TBool TClName::PasteFromClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary)
       
   142 	{
       
   143 	TStreamId streamId=aDictionary.At(KUidNameClipboardType);
       
   144 	if (streamId==KNullStreamId)
       
   145 		return(EFalse);
       
   146 	RStoreReadStream stream;
       
   147 	stream.OpenLC(aStore,streamId);
       
   148 	stream>>iName;
       
   149 	CleanupStack::PopAndDestroy();
       
   150 	return(ETrue);
       
   151 	}
       
   152 
       
   153 void TClNameWithAddress::CopyToClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary) const
       
   154 	{
       
   155 	TClName::CopyToClipboardL(aStore,aDictionary);
       
   156 	RStoreWriteStream stream;
       
   157 	TStreamId streamId=stream.CreateLC(aStore);
       
   158 	stream<<iAddress;
       
   159 	CleanupStack::PopAndDestroy();
       
   160 	aDictionary.AssignL(KUidAddressClipboardType,streamId);
       
   161 	}
       
   162 
       
   163 TBool TClNameWithAddress::PasteFromClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary)
       
   164 	{
       
   165 	TBool namePasted=TClName::PasteFromClipboardL(aStore,aDictionary);
       
   166 	TStreamId streamId=aDictionary.At(KUidAddressClipboardType);
       
   167 	if (streamId==KNullStreamId)
       
   168 		return(namePasted);
       
   169 	RStoreReadStream stream;
       
   170 	stream.OpenLC(aStore,streamId);
       
   171 	stream>>iAddress;
       
   172 	CleanupStack::PopAndDestroy();
       
   173 	return(ETrue);
       
   174 	}
       
   175 
       
   176 void TClNameWithNumber::CopyToClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary) const
       
   177 	{
       
   178 	TClName::CopyToClipboardL(aStore,aDictionary);
       
   179 	RStoreWriteStream stream;
       
   180 	TStreamId streamId=stream.CreateLC(aStore);
       
   181 	stream<<iNumber;
       
   182 	CleanupStack::PopAndDestroy();
       
   183 	aDictionary.AssignL(KUidNumberClipboardType,streamId);
       
   184 	}
       
   185 
       
   186 TBool TClNameWithNumber::PasteFromClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary)
       
   187 	{
       
   188 	TBool namePasted=TClName::PasteFromClipboardL(aStore,aDictionary);
       
   189 	TStreamId streamId=aDictionary.At(KUidNumberClipboardType);
       
   190 	if (streamId==KNullStreamId)
       
   191 		return(namePasted);
       
   192 	RStoreReadStream stream;
       
   193 	stream.OpenLC(aStore,streamId);
       
   194 	stream>>iNumber;
       
   195 	CleanupStack::PopAndDestroy();
       
   196 	return(ETrue);
       
   197 	}
       
   198 
       
   199 void TClInteger::CopyToClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary) const
       
   200 	{
       
   201 	RStoreWriteStream stream;
       
   202 	TStreamId streamId=stream.CreateLC(aStore);
       
   203 	stream<<iInteger;
       
   204 	CleanupStack::PopAndDestroy();
       
   205 	aDictionary.AssignL(KUidNumberClipboardType,streamId);
       
   206 	}
       
   207 
       
   208 TBool TClInteger::PasteFromClipboardL(CStreamStore& aStore,CStreamDictionary& aDictionary)
       
   209 	{
       
   210 	TStreamId streamId=aDictionary.At(KUidNumberClipboardType);
       
   211 	if (streamId==KNullStreamId)
       
   212 		return(EFalse);
       
   213 	RStoreReadStream stream;
       
   214 	stream.OpenLC(aStore,streamId);
       
   215 	stream>>iInteger;
       
   216 	CleanupStack::PopAndDestroy();
       
   217 	return(ETrue);
       
   218 	}
       
   219 
       
   220 /**
       
   221 @SYMTestCaseID          SYSLIB-BAFL-CT-0404
       
   222 @SYMTestCaseDesc        Tests the  CClipboard::Store(),StreamDictionary functions
       
   223 @SYMTestPriority        High
       
   224 @SYMTestActions         Tests the clipboard's file store with various cases(no file,file in use,corrupt file,non-store file)
       
   225 @SYMTestExpectedResults Tests must not fail
       
   226 @SYMREQ                 REQ0000
       
   227 */
       
   228 void TestErrorHandling(const TDesC& aTestPath)
       
   229 	{
       
   230 
       
   231 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0404 "));
       
   232 	TPtrC KClipboardFile(aTestPath);
       
   233 	RFs fs;
       
   234 	test (fs.Connect()==KErrNone);
       
   235 //
       
   236 	test.Next(_L("No clipboard file"));
       
   237 	fs.Delete(KClipboardFile);
       
   238 	test (CClipboard::Clear(fs)==KErrNone);
       
   239 	CClipboard* cb=NULL;
       
   240 	TRAPD(err,cb=CClipboard::NewForReadingL(fs));
       
   241 	test (err==KErrNone);
       
   242 	test (&cb->Store()==NULL);
       
   243 	test (&cb->StreamDictionary()!=NULL);
       
   244 	test (cb->StreamDictionary().IsNull());
       
   245 	delete cb;
       
   246 //
       
   247 	test.Next(_L("In use"));
       
   248 	RFile file;
       
   249 	test (file.Create(fs,KClipboardFile,EFileRead|EFileWrite)==KErrNone);
       
   250 	TRAP(err,cb=CClipboard::NewForReadingL(fs));
       
   251 	test (err==KErrNone);
       
   252 	test (&cb->Store()==NULL);
       
   253 	test (&cb->StreamDictionary()!=NULL);
       
   254 	test (cb->StreamDictionary().IsNull());
       
   255 	delete cb;
       
   256 //
       
   257 	test.Next(_L("Clear while in use"));
       
   258 	test (CClipboard::Clear(fs)==KErrInUse);
       
   259 //
       
   260 	test.Next(_L("Bogus clipboard file"));
       
   261 	file.Close();
       
   262 	TRAP(err,cb=CClipboard::NewForReadingL(fs));
       
   263 	test (err==KErrNone);
       
   264 	test (&cb->Store()==NULL);
       
   265 	test (&cb->StreamDictionary()!=NULL);
       
   266 	test (cb->StreamDictionary().IsNull());
       
   267 	delete cb;
       
   268 //
       
   269 	test.Next(_L("Non-store file"));
       
   270 	test (file.Replace(fs,KClipboardFile,EFileRead|EFileWrite)==KErrNone);
       
   271 	test (file.Write(_L8("some data which does not make this a file store"))==KErrNone);
       
   272 	file.Close();
       
   273 	TRAP(err,cb=CClipboard::NewForReadingL(fs));
       
   274 	test (err==KErrNone);
       
   275 	test (&cb->Store()==NULL);
       
   276 	test (&cb->StreamDictionary()!=NULL);
       
   277 	test (cb->StreamDictionary().IsNull());
       
   278 	delete cb;
       
   279 //
       
   280 	test.Next(_L("Wrong type file"));
       
   281 	CFileStore* store=CDirectFileStore::ReplaceLC(fs,KClipboardFile,EFileRead|EFileWrite);
       
   282 	store->SetTypeL(TUidType(KDirectFileStoreLayoutUid,KUidTestType));
       
   283 	store->CommitL();
       
   284 	CleanupStack::PopAndDestroy();
       
   285 	TRAP(err,cb=CClipboard::NewForReadingL(fs));
       
   286 	test (err==KErrNone);
       
   287 	test (&cb->Store()==NULL);
       
   288 	test (&cb->StreamDictionary()!=NULL);
       
   289 	test (cb->StreamDictionary().IsNull());
       
   290 	delete cb;
       
   291 //
       
   292 	test.Next(_L("Corrupted clipboard"));
       
   293 	store=CDirectFileStore::ReplaceLC(fs,KClipboardFile,EFileRead|EFileWrite);
       
   294 	store->SetTypeL(TUidType(KDirectFileStoreLayoutUid,KClipboardFileUid));
       
   295 	store->CommitL();
       
   296 	CleanupStack::PopAndDestroy();
       
   297 	TRAP(err,cb=CClipboard::NewForReadingL(fs));
       
   298 	test (err==KErrNone);
       
   299 	test (&cb->Store()==NULL);
       
   300 	test (&cb->StreamDictionary()!=NULL);
       
   301 	test (cb->StreamDictionary().IsNull());
       
   302 	delete cb;
       
   303 //
       
   304 	test.Next(_L("Clear clipboard"));
       
   305 	test (CClipboard::Clear(fs)==KErrNone);
       
   306 	TRAP(err,cb=CClipboard::NewForReadingL(fs));
       
   307 	test (err==KErrNone);
       
   308 	test (&cb->Store()==NULL);
       
   309 	test (&cb->StreamDictionary()!=NULL);
       
   310 	test (cb->StreamDictionary().IsNull());
       
   311 	delete cb;
       
   312 //
       
   313 #if defined(_DEBUG)
       
   314 	test.Next(_L("Out of memory failure"));
       
   315 //
       
   316 	cb=CClipboard::NewForWritingLC(fs);
       
   317 	RStoreWriteStream stream;
       
   318 	TStreamId testId=stream.CreateLC(cb->Store());
       
   319 	stream.WriteInt32L(0);
       
   320 	stream.CommitL();
       
   321 	CleanupStack::PopAndDestroy();
       
   322 	cb->StreamDictionary().AssignL(KUidTestType,testId);
       
   323 	cb->CommitL();
       
   324 	CleanupStack::PopAndDestroy();
       
   325 //
       
   326 	__UHEAP_MARK;
       
   327 	for (TInt ii=0;;++ii)
       
   328 		{
       
   329 		__UHEAP_FAILNEXT(ii);
       
   330 		TRAP(err,cb=CClipboard::NewForReadingL(fs));
       
   331 		if (err==KErrNone)
       
   332 			break;
       
   333 		test(err==KErrNoMemory);
       
   334 		__UHEAP_CHECK(0);
       
   335 		}
       
   336 	__UHEAP_RESET;
       
   337 	test (&cb->Store()!=NULL);
       
   338 	test (cb->StreamDictionary().At(KUidTestType)==testId);
       
   339 	delete cb;
       
   340 	__UHEAP_MARKEND;
       
   341 //
       
   342 #endif
       
   343 //
       
   344 	test.Next(_L("Fail to commit clipboard"));
       
   345 	TRAP(err,cb=CClipboard::NewForWritingLC(fs);CleanupStack::Pop());
       
   346 	test (err==KErrNone);
       
   347 	delete cb;
       
   348 	test (!BaflUtils::FileExists(fs,KClipboardFile));
       
   349 //
       
   350 	fs.Close();
       
   351 	}
       
   352 
       
   353 /**
       
   354 @SYMTestCaseID          SYSLIB-BAFL-CT-0405
       
   355 @SYMTestCaseDesc        Tests the CClipboard::NewForWritingLC,NewForReadingL
       
   356 @SYMTestPriority        High
       
   357 @SYMTestActions         Tests for copy and paste operations on TClName,TClNameWithAddress,TClInteger,TClNameWithNumber
       
   358 @SYMTestExpectedResults Tests must not fail
       
   359 @SYMREQ                 REQ0000
       
   360 */
       
   361 void DoTestsOnDrive(const TDesC& aDrivePath)
       
   362     {
       
   363 	test.Start(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0405 Test single paste "));
       
   364 	TClName n1,n2;
       
   365 	n1.iName=_L("Fred");
       
   366 	n2.iName=_L("Dino");
       
   367 	n1.CopyL();
       
   368 	TClNameWithAddress na1,na2;
       
   369 	test(na1.PasteL());
       
   370 	n2.CopyL();
       
   371 	test(na2.PasteL());
       
   372 	test(na1.iName==_L("Fred"));
       
   373 	test(na2.iName==_L("Dino"));
       
   374 	na1.iAddress=_L("Bedrock");
       
   375 	test.Next(_L("Test double paste"));
       
   376 	na1.CopyL();
       
   377 	test(na2.PasteL());
       
   378 	test(na2.iAddress==_L("Bedrock"));
       
   379 	test(na2.iName==_L("Fred"));
       
   380 	test.Next(_L("Test paste failure if type not present"));
       
   381 	TClInteger i1,i2;
       
   382 	test(!i1.PasteL());
       
   383 	test.Next(_L("Test selecting the right data type"));
       
   384 	TClNameWithNumber nn1,nn2;
       
   385 	nn1.iNumber=36363;
       
   386 	test(nn1.PasteL());
       
   387 	test(nn1.iNumber==36363);
       
   388 	test(nn1.iName==_L("Fred"));
       
   389 	nn1.iName=_L("Wilm");
       
   390 	nn1.CopyL();
       
   391 	test(i2.PasteL());
       
   392 	test(i2.iInteger==36363);
       
   393 	test(n1.PasteL());
       
   394 	test(n1.iName==_L("Wilm"));
       
   395 	test(nn2.PasteL());
       
   396 	test(nn2.iName==_L("Wilm"));
       
   397 	test(nn2.iNumber==36363);
       
   398 //
       
   399 	TestErrorHandling(aDrivePath);
       
   400 	test.End();
       
   401     }
       
   402 
       
   403 void DoTests()
       
   404 	{
       
   405 	// Tests for the HAL attribute ClipboardDrive set in the device.
       
   406 	// C Drive
       
   407 	DoTestsOnDrive(KClipboardFileCDrive);
       
   408 	}
       
   409 
       
   410 GLDEF_C TInt E32Main()
       
   411 	{
       
   412     __UHEAP_MARK;
       
   413     CTrapCleanup *cleanup=CTrapCleanup::New();
       
   414 	test.Title();
       
   415 	test.Start(_L("Testing CClipboard "));
       
   416     TRAPD(err,DoTests());
       
   417     test(err==KErrNone);
       
   418 	test.End();
       
   419     test.Close();
       
   420     delete cleanup;
       
   421     __UHEAP_MARKEND;
       
   422 	return(0);
       
   423     }