1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
23
* Pierre Phaneuf <pp@ludusdesign.com>
26
* Alternatively, the contents of this file may be used under the terms of
27
* either the GNU General Public License Version 2 or later (the "GPL"), or
28
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the NPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the NPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
40
#include "nsISupports.h"
42
#include "nsIServiceManager.h"
43
#include "nsIComponentManager.h"
44
#include "nsICaseConversion.h"
45
#include "nsIEntityConverter.h"
46
#include "nsISaveAsCharset.h"
47
#include "nsIUnicodeEncoder.h"
48
#include "nsUnicharUtilCIID.h"
49
#include "nsIPersistentProperties2.h"
51
#include "nsNetUtil.h"
53
#include "nsReadableUtils.h"
55
#include "nsIUnicodeNormalizer.h"
58
NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID);
59
NS_DEFINE_IID(kCaseConversionIID, NS_ICASECONVERSION_IID);
60
NS_DEFINE_CID(kEntityConverterCID, NS_ENTITYCONVERTER_CID);
61
NS_DEFINE_CID(kSaveAsCharsetCID, NS_SAVEASCHARSET_CID);
62
NS_DEFINE_CID(kUnicodeNormalizerCID, NS_UNICODE_NORMALIZER_CID);
69
// test data for ToUpper
70
static PRUnichar t2data [T2LEN+1] = {
102
// expected result for ToUpper
103
static PRUnichar t2result[T2LEN+1] = {
135
// test data for ToLower
136
static PRUnichar t3data [T3LEN+1] = {
168
// expected result for ToLower
169
static PRUnichar t3result[T3LEN+1] = {
201
// test data for ToTitle
202
static PRUnichar t4data [T4LEN+1] = {
234
// expected result for ToTitle
235
static PRUnichar t4result[T4LEN+1] = {
268
void TestCaseConversion()
270
printf("==============================\n");
271
printf("Start nsICaseConversion Test \n");
272
printf("==============================\n");
273
nsICaseConversion *t = NULL;
275
res = nsServiceManager::GetService(kUnicharUtilCID,
279
printf("Test 1 - GetService():\n");
280
if(NS_FAILED(res) || ( t == NULL ) ) {
281
printf("\t1st GetService failed\n");
283
res = nsServiceManager::ReleaseService(kUnicharUtilCID, t);
286
res = nsServiceManager::GetService(kUnicharUtilCID,
290
if(NS_FAILED(res) || ( t == NULL ) ) {
291
printf("\t2nd GetService failed\n");
297
printf("Test 2 - ToUpper(PRUnichar, PRUnichar*):\n");
298
for(i=0;i < T2LEN ; i++)
300
res = t->ToUpper(t2data[i], &ch);
302
printf("\tFailed!! return value != NS_OK\n");
305
if(ch != t2result[i])
306
printf("\tFailed!! result unexpected %d\n", i);
310
printf("Test 3 - ToLower(PRUnichar, PRUnichar*):\n");
311
for(i=0;i < T3LEN; i++)
313
res = t->ToLower(t3data[i], &ch);
315
printf("\tFailed!! return value != NS_OK\n");
318
if(ch != t3result[i])
319
printf("\tFailed!! result unexpected %d\n", i);
323
printf("Test 4 - ToTitle(PRUnichar, PRUnichar*):\n");
324
for(i=0;i < T4LEN; i++)
326
res = t->ToTitle(t4data[i], &ch);
328
printf("\tFailed!! return value != NS_OK\n");
331
if(ch != t4result[i])
332
printf("\tFailed!! result unexpected %d\n", i);
336
printf("Test 5 - ToUpper(PRUnichar*, PRUnichar*, PRUint32):\n");
337
res = t->ToUpper(t2data, buf, T2LEN);
339
printf("\tFailed!! return value != NS_OK\n");
341
for(i = 0; i < T2LEN; i++)
343
if(buf[i] != t2result[i])
345
printf("\tFailed!! result unexpected %d\n", i);
351
printf("Test 6 - ToLower(PRUnichar*, PRUnichar*, PRUint32):\n");
352
res = t->ToLower(t3data, buf, T3LEN);
354
printf("\tFailed!! return value != NS_OK\n");
356
for(i = 0; i < T3LEN; i++)
358
if(buf[i] != t3result[i])
360
printf("\tFailed!! result unexpected %d\n", i);
366
printf("Test 7 - ToTitle(PRUnichar*, PRUnichar*, PRUint32):\n");
367
printf("!!! To Be Implemented !!!\n");
369
res = nsServiceManager::ReleaseService(kUnicharUtilCID, t);
371
printf("==============================\n");
372
printf("Finish nsICaseConversion Test \n");
373
printf("==============================\n");
377
static void TestEntityConversion(PRUint32 version)
379
printf("==============================\n");
380
printf("Start nsIEntityConverter Test \n");
381
printf("==============================\n");
389
inString.Assign(NS_ConvertASCIItoUCS2("\xA0\xA1\xA2\xA3"));
390
uChar = (PRUnichar) 8364; //euro
391
inString.Append(&uChar, 1);
392
uChar = (PRUnichar) 9830; //
393
inString.Append(&uChar, 1);
395
nsCOMPtr <nsIEntityConverter> entityConv;
396
res = nsComponentManager::CreateInstance(kEntityConverterCID, NULL, NS_GET_IID(nsIEntityConverter), getter_AddRefs(entityConv));
397
if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n"); return;}
400
// convert char by char
401
for (i = 0; i < inString.Length(); i++) {
403
res = entityConv->ConvertToEntity(inString[i], version, &entity);
404
if (NS_SUCCEEDED(res) && NULL != entity) {
405
printf("%c %s\n", inString[i], entity);
406
nsMemory::Free(entity);
410
// convert at once as a string
412
res = entityConv->ConvertToEntities(inString.get(), version, &entities);
413
if (NS_SUCCEEDED(res) && NULL != entities) {
414
for (i = 0; i < nsCRT::strlen(entities); i++) {
415
printf("%c", (char) entities[i]);
416
if (';' == (char) entities[i])
419
nsMemory::Free(entities);
422
printf("==============================\n");
423
printf("Finish nsIEntityConverter Test \n");
424
printf("==============================\n\n");
427
static void TestSaveAsCharset()
429
printf("==============================\n");
430
printf("Start nsISaveAsCharset Test \n");
431
printf("==============================\n");
436
inString.Assign(NS_ConvertASCIItoUCS2("\x61\x62\x80\xA0\x63"));
439
// first, dump input string
440
for (PRUint32 i = 0; i < inString.Length(); i++) {
441
printf("%c ", inString[i]);
445
nsCOMPtr <nsISaveAsCharset> saveAsCharset;
446
res = nsComponentManager::CreateInstance(kSaveAsCharsetCID, NULL, NS_GET_IID(nsISaveAsCharset), getter_AddRefs(saveAsCharset));
447
if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
449
printf("ISO-8859-1 attr_plainTextDefault entityNone\n");
450
res = saveAsCharset->Init("ISO-8859-1",
451
nsISaveAsCharset::attr_plainTextDefault,
452
nsIEntityConverter::entityNone);
453
if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
454
res = saveAsCharset->Convert(inString.get(), &outString);
455
if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
456
if (NULL == outString) {printf("\tFailed!! output null\n");}
457
else {printf("%s\n", outString); nsMemory::Free(outString);}
459
printf("ISO-2022-JP attr_plainTextDefault entityNone\n");
460
res = saveAsCharset->Init("ISO-2022-JP",
461
nsISaveAsCharset::attr_plainTextDefault,
462
nsIEntityConverter::entityNone);
463
if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
464
res = saveAsCharset->Convert(inString.get(), &outString);
465
if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
466
if (NULL == outString) {printf("\tFailed!! output null\n");}
467
else {printf("%s\n", outString); nsMemory::Free(outString);}
468
if (NS_ERROR_UENC_NOMAPPING == res) {
469
outString = ToNewUTF8String(inString);
470
if (NULL == outString) {printf("\tFailed!! output null\n");}
471
else {printf("Fall back to UTF-8: %s\n", outString); nsMemory::Free(outString);}
474
printf("ISO-2022-JP attr_FallbackQuestionMark entityNone\n");
475
res = saveAsCharset->Init("ISO-2022-JP",
476
nsISaveAsCharset::attr_FallbackQuestionMark,
477
nsIEntityConverter::entityNone);
478
if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
479
res = saveAsCharset->Convert(inString.get(), &outString);
480
if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
481
if (NULL == outString) {printf("\tFailed!! output null\n");}
482
else {printf("%s\n", outString); nsMemory::Free(outString);}
484
printf("ISO-2022-JP attr_FallbackEscapeU entityNone\n");
485
res = saveAsCharset->Init("ISO-2022-JP",
486
nsISaveAsCharset::attr_FallbackEscapeU,
487
nsIEntityConverter::entityNone);
488
if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
489
res = saveAsCharset->Convert(inString.get(), &outString);
490
if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
491
if (NULL == outString) {printf("\tFailed!! output null\n");}
492
else {printf("%s\n", outString); nsMemory::Free(outString);}
494
printf("ISO-8859-1 attr_htmlTextDefault html40Latin1\n");
495
res = saveAsCharset->Init("ISO-8859-1",
496
nsISaveAsCharset::attr_htmlTextDefault,
497
nsIEntityConverter::html40Latin1);
498
if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
499
res = saveAsCharset->Convert(inString.get(), &outString);
500
if (NS_ERROR_UENC_NOMAPPING != res && NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
501
if (NULL == outString) {printf("\tFailed!! output null\n");}
502
else {printf("%s\n", outString); nsMemory::Free(outString);}
504
printf("ISO-8859-1 attr_FallbackHexNCR+attr_EntityAfterCharsetConv html40Latin1 \n");
505
res = saveAsCharset->Init("ISO-8859-1",
506
nsISaveAsCharset::attr_FallbackHexNCR +
507
nsISaveAsCharset::attr_EntityAfterCharsetConv,
508
nsIEntityConverter::html40Latin1);
509
if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
510
res = saveAsCharset->Convert(inString.get(), &outString);
511
if (NS_ERROR_UENC_NOMAPPING != res && NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
512
if (NULL == outString) {printf("\tFailed!! output null\n");}
513
else {printf("%s\n", outString); nsMemory::Free(outString);}
516
printf("==============================\n");
517
printf("Finish nsISaveAsCharset Test \n");
518
printf("==============================\n\n");
521
static PRUnichar normStr[] =
532
static PRUnichar nfdForm[] =
545
void TestNormalization()
547
printf("==============================\n");
548
printf("Start nsIUnicodeNormalizer Test \n");
549
printf("==============================\n");
550
nsIUnicodeNormalizer *t = NULL;
552
res = nsServiceManager::GetService(kUnicodeNormalizerCID,
553
NS_GET_IID(nsIUnicodeNormalizer),
556
printf("Test 1 - GetService():\n");
557
if(NS_FAILED(res) || ( t == NULL ) ) {
558
printf("\t1st Norm GetService failed\n");
560
res = nsServiceManager::ReleaseService(kUnicodeNormalizerCID, t);
563
res = nsServiceManager::GetService(kUnicodeNormalizerCID,
564
NS_GET_IID(nsIUnicodeNormalizer),
567
if(NS_FAILED(res) || ( t == NULL ) ) {
568
printf("\t2nd GetService failed\n");
570
printf("Test 2 - NormalizeUnicode(PRUint32, const nsAString&, nsAString&):\n");
571
nsAutoString resultStr;
572
res = t->NormalizeUnicodeNFD(nsDependentString(normStr), resultStr);
573
if (resultStr.Equals(nfdForm)) {
574
printf(" Succeeded in NFD UnicodeNormalizer test. \n");
576
printf(" Failed in NFD UnicodeNormalizer test. \n");
580
res = nsServiceManager::ReleaseService(kUnicodeNormalizerCID, t);
582
printf("==============================\n");
583
printf("Finish nsIUnicodeNormalizer Test \n");
584
printf("==============================\n");
589
int main(int argc, char** argv) {
591
nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
593
printf("NS_InitXPCOM2 failed\n");
597
// --------------------------------------------
599
TestCaseConversion();
601
// --------------------------------------------
603
TestEntityConversion(nsIEntityConverter::html40);
605
// --------------------------------------------
609
// --------------------------------------------
613
// --------------------------------------------
614
printf("Finish All The Test Cases\n");
615
nsresult res = NS_OK;
616
res = nsComponentManager::FreeLibraries();
619
printf("nsComponentManager failed\n");
621
printf("nsComponentManager FreeLibraries Done\n");