textrendering/texthandling/ttext/T_LAYDOC.CPP
changeset 32 8b9155204a54
parent 0 1fb32624e06b
child 51 a7c938434754
equal deleted inserted replaced
31:b9ad20498fb4 32:8b9155204a54
       
     1 /*
       
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32test.h>
       
    20 #include <txtlaydc.h>
       
    21 #include <txtglobl.h>
       
    22 #include <txtfrmat.h>
       
    23 #include <txtfmlyr.h>
       
    24 
       
    25 #define UNUSED_VAR(a) a = a
       
    26 
       
    27 LOCAL_D RTest test(_L("TGlobalLayDoc - Layout class"));
       
    28 LOCAL_D TPtrC defaultText(_L("This is default text"));
       
    29 LOCAL_D TPtrC comp1(_L("fault text"));
       
    30 LOCAL_D TPtrC comp2(_L("t"));
       
    31 LOCAL_D TPtrC view(_L("x"));
       
    32 
       
    33 GLDEF_C void CheckView(TPtrC& aView,TPtrC& aControl)
       
    34 //
       
    35 // Test that aView matches the contol descriptor in
       
    36 // the expected metrics.
       
    37 //
       
    38 	{
       
    39 	test(aView.Length()==(aControl.Length()+1));
       
    40 	TInt index=0;
       
    41 	for (index=0;index<aControl.Length();index++)
       
    42 		{
       
    43 		test(aView[index]==aControl[index]);
       
    44 		}
       
    45 	test(aView[index]==CEditableText::EParagraphDelimiter);
       
    46 	}
       
    47 
       
    48 
       
    49 GLDEF_C void TestDocumentLength(CEditableText::TDocumentStorage aStorage)
       
    50 //
       
    51 //
       
    52 //
       
    53 	{
       
    54 	__UHEAP_MARK;
       
    55 	// Make the global format layers...
       
    56 	CParaFormatLayer* paraLayer=CParaFormatLayer::NewL();
       
    57 	CCharFormatLayer* charLayer=CCharFormatLayer::NewL();
       
    58 
       
    59 // Create Document
       
    60 	CGlobalText* doc=CGlobalText::NewL(paraLayer,charLayer,aStorage);
       
    61 	TInt length=doc->LdDocumentLength();
       
    62 	test(length==0);
       
    63 // Insert document content
       
    64 	doc->InsertL(0,defaultText);
       
    65 	length=doc->LdDocumentLength();
       
    66 	test(length==20);	
       
    67 
       
    68 	delete doc;
       
    69 	delete paraLayer;
       
    70 	delete charLayer;
       
    71 	__UHEAP_MARKEND;
       
    72 	}
       
    73 
       
    74 
       
    75 GLDEF_C void TestRead(CEditableText::TDocumentStorage aStorage)
       
    76 //
       
    77 // Test this class' GetChars method
       
    78 // functions as predicted.
       
    79 //
       
    80 	{
       
    81 	__UHEAP_MARK;
       
    82 	// Make the global format layers...
       
    83 	CParaFormatLayer* paraLayer=CParaFormatLayer::NewL();
       
    84 	CCharFormatLayer* charLayer=CCharFormatLayer::NewL();
       
    85 
       
    86 // Create Document
       
    87 	CGlobalText* doc=CGlobalText::NewL(paraLayer,charLayer,aStorage);
       
    88 	TRAPD(ret,doc->InsertL(0,defaultText));
       
    89     UNUSED_VAR(ret);
       
    90 
       
    91 // Create LayDoc
       
    92 	TCharFormat format;
       
    93 	TPtrC view;
       
    94 	
       
    95 	test.Start(_L("Sensing at start"));
       
    96 	doc->GetChars(view,format,0);
       
    97 	CheckView(view,defaultText);
       
    98 	
       
    99 	test.Next(_L("Sensing from char.pos.10"));
       
   100 	doc->GetChars(view,format,10);
       
   101 	CheckView(view,comp1);
       
   102 	
       
   103 	test.Next(_L("Sensing from end-1"));
       
   104 	doc->GetChars(view,format,19);
       
   105 	CheckView(view,comp2);
       
   106 	
       
   107 	test.Next(_L("Sensing from end"));
       
   108 	doc->GetChars(view,format,20);
       
   109 	test(*view.Ptr()==CEditableText::EParagraphDelimiter);
       
   110 	
       
   111 	test.End();
       
   112 	delete doc;
       
   113 	delete paraLayer;
       
   114 	delete charLayer;
       
   115 	__UHEAP_MARKEND;
       
   116 	}
       
   117 
       
   118 
       
   119 GLDEF_C void CheckParagraphStart(TInt aCalculated,TInt aExpected)
       
   120 //
       
   121 // Checks the value returned from ParagraphStart(aPos) is what
       
   122 // it is expected to be.
       
   123 //
       
   124 	{
       
   125 	test(aCalculated==aExpected);
       
   126 	}
       
   127 
       
   128 
       
   129 GLDEF_C void CheckCharsSkipped(TInt aCalculated,TInt aExpected)
       
   130 //
       
   131 // Check the number of characters skipped following a  ParagraphStart()
       
   132 // is as expected.
       
   133 //
       
   134 	{
       
   135 	test(aCalculated==aExpected);
       
   136 	}
       
   137 
       
   138 
       
   139 GLDEF_C void DoParagraphStart(TInt aStartPos,TInt aLength,MLayDoc* aLayout)
       
   140 //
       
   141 // Parametric testing of the ParagraphStart method of the
       
   142 // document class hierarchy.
       
   143 //
       
   144 	{
       
   145 	User::Heap().Check();
       
   146 	TInt tempPos=0;
       
   147 	TInt charsSkipped=0;
       
   148 	for (TInt charPos=aStartPos;charPos<aStartPos+aLength;charPos++)
       
   149 		{// Check Paragraph
       
   150 		tempPos=charPos;
       
   151 		charsSkipped=aLayout->LdToParagraphStart(charPos);
       
   152 		// charPos is updated to paragraph start character position.
       
   153 		CheckParagraphStart(charPos,aStartPos);
       
   154 		charPos=tempPos;  // Reset charPos following it's update.
       
   155 		CheckCharsSkipped(charsSkipped,charPos-aStartPos);
       
   156 		}
       
   157 	User::Heap().Check();
       
   158 	}
       
   159 
       
   160 
       
   161 GLDEF_C void TestParagraphStart(CEditableText::TDocumentStorage aStorage)
       
   162 //
       
   163 // Tests the ParagraphStart method.
       
   164 //
       
   165 	{
       
   166 	User::Heap().Check();
       
   167 	__UHEAP_MARK;
       
   168 	// Make the global format layers...
       
   169 	CParaFormatLayer* paraLayer=CParaFormatLayer::NewL();
       
   170 	CCharFormatLayer* charLayer=CCharFormatLayer::NewL();
       
   171 
       
   172 	TBuf<128> content;
       
   173 	content.Append(_L("Paragraph one. Complete with sentence and word breaks."));
       
   174 	content.Append(CEditableText::EParagraphDelimiter);
       
   175 	content.Append(_L("This is paragraph two."));
       
   176 	content.Append(CEditableText::EParagraphDelimiter);
       
   177 	content.Append(_L("This is paragraph 3"));
       
   178 	// Create document.
       
   179 	CGlobalText* document=CGlobalText::NewL(paraLayer,charLayer,aStorage);
       
   180 	TPtrC body(content);
       
   181 	document->InsertL(0,body);
       
   182 	// Now do the tests.
       
   183 	test.Start(_L("Paragraph 1"));
       
   184 	DoParagraphStart(0,55,document);  // Paragraph 1
       
   185 	test.Next(_L("Paragraph 2"));
       
   186 	DoParagraphStart(55,23,document);  // Paragraph 2
       
   187 	test.Next(_L("Paragraph 3"));
       
   188 	DoParagraphStart(78,20,document);  // Paragraph 3
       
   189 
       
   190 	delete paraLayer;
       
   191 	delete charLayer;
       
   192 	delete document;
       
   193 	test.End();
       
   194 	__UHEAP_MARKEND;
       
   195 	User::Heap().Check();
       
   196 	}
       
   197 
       
   198 
       
   199 GLDEF_C void TestGetParagraphFormatL(CEditableText::TDocumentStorage aStorage)
       
   200 //
       
   201 //
       
   202 //
       
   203 	{
       
   204 	__UHEAP_MARK;
       
   205 	User::Heap().Check();
       
   206 	// Make the global format layers...
       
   207 	CParaFormatLayer* paraLayer=CParaFormatLayer::NewL();
       
   208 	CCharFormatLayer* charLayer=CCharFormatLayer::NewL();
       
   209 
       
   210 	CGlobalText* globalDoc=CGlobalText::NewL(paraLayer,charLayer,aStorage);
       
   211 
       
   212 	CParaFormat* format=CParaFormat::NewL();
       
   213 	CParaFormat* control=CParaFormat::NewL();
       
   214 	TParaFormatMask cMask;
       
   215 	control->iLeftMarginInTwips=1440; cMask.SetAttrib(EAttLeftMargin);
       
   216 	TTabStop tab1;
       
   217 	tab1.iTwipsPosition=1440; tab1.iType=TTabStop::ERightTab;
       
   218 	control->StoreTabL(tab1); cMask.SetAttrib(EAttTabStop);
       
   219 	
       
   220 	TParaBorder border;
       
   221 	border.iLineStyle=TParaBorder::ESolid;
       
   222 	border.iThickness=1;
       
   223 	control->SetParaBorderL(CParaFormat::EParaBorderTop,border); cMask.SetAttrib(EAttTopBorder);
       
   224 	control->iKeepTogether=ETrue; cMask.SetAttrib(EAttKeepTogether);
       
   225 	CParaFormatLayer* layer=CParaFormatLayer::NewL(control,cMask);
       
   226 	globalDoc->SetGlobalParaFormat(layer);
       
   227 
       
   228 	globalDoc->GetParagraphFormatL(format,0);
       
   229 	TInt c1=format->TabCount();
       
   230 	TInt c2=control->TabCount();
       
   231 	test(c1==c2);
       
   232 	test(format->IsEqual(*control));
       
   233 
       
   234 	__UHEAP_MARK;	
       
   235 	delete layer;
       
   236 	delete control;
       
   237 	delete format;
       
   238 	__UHEAP_MARKEND;
       
   239 	delete globalDoc;	
       
   240 	delete charLayer;
       
   241 	delete paraLayer;
       
   242 	User::Heap().Check();
       
   243 	__UHEAP_MARKEND;
       
   244 	}
       
   245 
       
   246 GLDEF_C void TestRegister(CEditableText::TDocumentStorage aStorage)
       
   247 //
       
   248 // Checks all declared base class methods have been provided.
       
   249 //
       
   250 	{
       
   251 	User::Heap().Check();
       
   252 	__UHEAP_MARK;
       
   253 	// Make the global format layers...
       
   254 	CParaFormatLayer* paraLayer=CParaFormatLayer::NewL();
       
   255 	CCharFormatLayer* charLayer=CCharFormatLayer::NewL();
       
   256 
       
   257 // Create a para format layer
       
   258 	CParaFormat* format=CParaFormat::NewL();
       
   259 	TParaFormatMask mask;
       
   260 	mask.SetAttrib(EAttLeftMargin);
       
   261 	mask.SetAttrib(EAttRightMargin);
       
   262 	CParaFormatLayer* layer=CParaFormatLayer::NewL(format,mask);
       
   263 // Create global text document to layout
       
   264 	CGlobalText* doc=NULL;
       
   265 	TRAPD(ret,doc=CGlobalText::NewL(paraLayer,charLayer,aStorage));
       
   266     UNUSED_VAR(ret);
       
   267 	TInt pos=0;
       
   268 	doc->SetGlobalParaFormat(layer);
       
   269 
       
   270 // Check constructor
       
   271 	test.Start(_L("Constructor"));
       
   272 
       
   273 // Check DocumentLength
       
   274 	test.Next(_L("DocumentLength()"));
       
   275 	doc->LdDocumentLength();
       
   276 
       
   277 // Check ParagraphStart
       
   278 	test.Next(_L("ParagraphStart()"));
       
   279 	doc->LdToParagraphStart(pos);
       
   280 
       
   281 // Check GetParagraphFormatL
       
   282 	test.Next(_L("GetParagraphFormatL()"));
       
   283 	doc->GetParagraphFormatL(format,pos);
       
   284 
       
   285 // Check GetChars
       
   286 	test.Next(_L("GetChars()"));
       
   287 	TPtrC view;
       
   288 	TCharFormat charFormat;
       
   289 	doc->GetChars(view,charFormat,pos);
       
   290 
       
   291 // check EnquirePage
       
   292 	test.Next(_L("EnquirePageBreak()"));
       
   293 	doc->EnquirePageBreak(pos,0);
       
   294 
       
   295 	delete paraLayer;
       
   296 	delete charLayer;
       
   297 	delete doc;
       
   298 	delete layer;
       
   299 	delete format;
       
   300 	test.End();
       
   301 	__UHEAP_MARKEND;
       
   302 	User::Heap().Check();
       
   303 	}
       
   304 
       
   305 GLDEF_C void Test()
       
   306 //
       
   307 // Run the tests
       
   308 //
       
   309 	{
       
   310 	__UHEAP_MARK;
       
   311 	test.Start(_L("Checking all methods present"));
       
   312 	TestRegister(CEditableText::EFlatStorage);
       
   313 
       
   314 	test.Next(_L("DocumentLength()"));
       
   315 	TestDocumentLength(CEditableText::EFlatStorage);
       
   316 
       
   317 	test.Next(_L("EnquirePage()"));
       
   318 	test.Start(_L("Always returns 0"));
       
   319 	test.End();
       
   320 
       
   321 	test.Next(_L("ParagraphStart()"));
       
   322 	TestParagraphStart(CEditableText::EFlatStorage);
       
   323 
       
   324 	test.Next(_L("GetParagraphFormatL()"));
       
   325 	TestGetParagraphFormatL(CEditableText::EFlatStorage);
       
   326 		
       
   327 	test.Next(_L("GetChars"));
       
   328 	TestRead(CEditableText::EFlatStorage);
       
   329 	test.End();
       
   330 	__UHEAP_MARKEND;
       
   331 	}
       
   332 	
       
   333 	 
       
   334 GLDEF_C void TestSeg()
       
   335 //
       
   336 // Run the tests
       
   337 //
       
   338 	{
       
   339 	__UHEAP_MARK;
       
   340 	test.Next(_L("Checking all methods present"));
       
   341 	TestRegister(CEditableText::ESegmentedStorage);
       
   342 
       
   343 	test.Next(_L("DocumentLength()"));
       
   344 	TestDocumentLength(CEditableText::ESegmentedStorage);
       
   345 
       
   346 	test.Next(_L("EnquirePage()"));
       
   347 	test.Start(_L("Always returns 0"));
       
   348 	test.End();
       
   349 
       
   350 	test.Next(_L("ParagraphStart()"));
       
   351 	TestParagraphStart(CEditableText::ESegmentedStorage);
       
   352 
       
   353 	test.Next(_L("SenseParagraphFormatL()"));
       
   354 	TestGetParagraphFormatL(CEditableText::ESegmentedStorage);
       
   355 		
       
   356 	test.Next(_L("GetChars"));
       
   357 	//TestRead();
       
   358 	//test.End();
       
   359 	test.Start(_L("Test not yet implemented"));
       
   360 	test.End();
       
   361 	__UHEAP_MARKEND;
       
   362 	}
       
   363 	
       
   364 	 
       
   365 GLDEF_C TInt E32Main()							 
       
   366 //
       
   367 // Drives the test program
       
   368 //
       
   369 	{
       
   370 	CTrapCleanup* cleanup=CTrapCleanup::New();
       
   371 	
       
   372 
       
   373 	test.Start(_L(" @SYMTestCaseID:SYSLIB-TTEXT-LEGACY-T_LAYDOC-0001 MLayDoc - Using Flat document "));
       
   374 	
       
   375 	__UHEAP_MARK;
       
   376 	TRAPD(ret,Test());
       
   377 	__UHEAP_MARKEND;
       
   378     test(ret == KErrNone);
       
   379 	
       
   380 	test.Next(_L("MLaydoc - Using Segmented document"));
       
   381 	__UHEAP_MARK;
       
   382 	TRAP(ret,TestSeg());
       
   383 	__UHEAP_MARKEND;
       
   384     test(ret == KErrNone);
       
   385 
       
   386 	test.End();
       
   387 	test.Close();
       
   388 
       
   389 	delete cleanup;
       
   390 
       
   391 	return(0);
       
   392 	}