michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #include michael@0: #include "nsXPCOM.h" michael@0: #include "nsIComponentManager.h" michael@0: #include "nsISupports.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsILineBreaker.h" michael@0: #include "nsIWordBreaker.h" michael@0: #include "nsLWBrkCIID.h" michael@0: #include "nsStringAPI.h" michael@0: #include "nsEmbedString.h" michael@0: #include "TestHarness.h" michael@0: michael@0: #define WORK_AROUND_SERVICE_MANAGER_ASSERT michael@0: michael@0: NS_DEFINE_CID(kLBrkCID, NS_LBRK_CID); michael@0: NS_DEFINE_CID(kWBrkCID, NS_WBRK_CID); michael@0: michael@0: michael@0: static char teng1[] = michael@0: // 1 2 3 4 5 6 7 michael@0: //01234567890123456789012345678901234567890123456789012345678901234567890123456789 michael@0: "This is a test to test(reasonable) line break. This 0.01123 = 45 x 48."; michael@0: michael@0: static uint32_t exp1[] = { michael@0: 4,7,9,14,17,34,39,40,41,42,49,54,62,64,67,69,73 michael@0: }; michael@0: michael@0: static uint32_t wexp1[] = { michael@0: michael@0: 4,5,7,8,9,10,14,15,17,18,22,23,33,34,35,39,43,48,49,50,54,55,56,57,62,63, michael@0: 64,65,67,68,69,70,72 michael@0: }; michael@0: // 1 2 3 4 5 6 7 michael@0: //01234567890123456789012345678901234567890123456789012345678901234567890123456789 michael@0: static char teng2[] = michael@0: "()((reasonab(l)e) line break. .01123=45x48."; michael@0: michael@0: static uint32_t lexp2[] = { michael@0: 17,22,23,30,44 michael@0: }; michael@0: static uint32_t wexp2[] = { michael@0: 4,12,13,14,15,16,17,18,22,24,29,30,31,32,37,38,43 michael@0: }; michael@0: michael@0: // 1 2 3 4 5 6 7 michael@0: //01234567890123456789012345678901234567890123456789012345678901234567890123456789 michael@0: static char teng3[] = michael@0: "It's a test to test(ronae ) line break...."; michael@0: static uint32_t exp3[] = { michael@0: 4,6,11,14,25,27,32,42 michael@0: }; michael@0: static uint32_t wexp3[] = { michael@0: 2,3,4,5,6,7,11,12,14,15,19,20,25,26,27,28,32,33,38 michael@0: }; michael@0: michael@0: static char ruler1[] = michael@0: " 1 2 3 4 5 6 7 "; michael@0: static char ruler2[] = michael@0: "0123456789012345678901234567890123456789012345678901234567890123456789012"; michael@0: michael@0: michael@0: bool TestASCIILB(nsILineBreaker *lb, michael@0: const char* in, const uint32_t len, michael@0: const uint32_t* out, uint32_t outlen) michael@0: { michael@0: NS_ConvertASCIItoUTF16 eng1(in); michael@0: uint32_t i,j; michael@0: uint32_t res[256]; michael@0: bool ok = true; michael@0: int32_t curr; michael@0: for(i = 0, curr = 0; (curr != NS_LINEBREAKER_NEED_MORE_TEXT) && michael@0: (i < 256); i++) michael@0: { michael@0: curr = lb->Next(eng1.get(), eng1.Length(), curr); michael@0: res [i] = curr != NS_LINEBREAKER_NEED_MORE_TEXT ? curr : eng1.Length(); michael@0: michael@0: } michael@0: if (i != outlen) michael@0: { michael@0: ok = false; michael@0: printf("WARNING!!! return size wrong, expect %d but got %d \n", michael@0: outlen, i); michael@0: } michael@0: printf("string = \n%s\n", in); michael@0: printf("%s\n", ruler1); michael@0: printf("%s\n", ruler2); michael@0: printf("Expect = \n"); michael@0: for(j=0;jNextWord(eng1.get(), eng1.Length(), curr); michael@0: (curr != NS_WORDBREAKER_NEED_MORE_TEXT) && (i < 256); michael@0: curr = lb->NextWord(eng1.get(), eng1.Length(), curr), i++) michael@0: { michael@0: res [i] = curr != NS_WORDBREAKER_NEED_MORE_TEXT ? curr : eng1.Length(); michael@0: } michael@0: if (i != outlen) michael@0: { michael@0: ok = false; michael@0: printf("WARNING!!! return size wrong, expect %d but got %d\n", michael@0: outlen, i); michael@0: } michael@0: printf("string = \n%s\n", in); michael@0: printf("%s\n", ruler1); michael@0: printf("%s\n", ruler2); michael@0: printf("Expect = \n"); michael@0: for(j=0;jNextWord(fragText.get(), fragText.Length(), cur); michael@0: uint32_t start = 0; michael@0: for(uint32_t j = 0; cur != NS_WORDBREAKER_NEED_MORE_TEXT ; j++) michael@0: { michael@0: result.Append(Substring(fragText, start, cur - start)); michael@0: result.Append('^'); michael@0: start = (cur >= 0 ? cur : cur - start); michael@0: cur = wbk->NextWord(fragText.get(), fragText.Length(), cur); michael@0: } michael@0: michael@0: result.Append(Substring(fragText, fragText.Length() - start)); michael@0: michael@0: if( i != (numOfFragment -1 )) michael@0: { michael@0: NS_ConvertASCIItoUTF16 nextFragText(wb[i+1]); michael@0: michael@0: bool canBreak = true; michael@0: canBreak = wbk->BreakInBetween( fragText.get(), michael@0: fragText.Length(), michael@0: nextFragText.get(), michael@0: nextFragText.Length()); michael@0: if(canBreak) michael@0: result.Append('^'); michael@0: michael@0: fragText.Assign(nextFragText); michael@0: } michael@0: } michael@0: printf("Output From SamplePrintWordWithBreak() \n\n"); michael@0: printf("[%s]\n", NS_ConvertUTF16toUTF8(result).get()); michael@0: michael@0: NS_IF_RELEASE(wbk); michael@0: } michael@0: michael@0: void SampleFindWordBreakFromPosition(uint32_t fragN, uint32_t offset) michael@0: { michael@0: uint32_t numOfFragment = sizeof(wb) / sizeof(char*); michael@0: nsIWordBreaker *wbk = nullptr; michael@0: michael@0: CallGetService(kWBrkCID, &wbk); michael@0: michael@0: NS_ConvertASCIItoUTF16 fragText(wb[fragN]); michael@0: michael@0: nsWordRange res = wbk->FindWord(fragText.get(), fragText.Length(), offset); michael@0: michael@0: bool canBreak; michael@0: nsAutoString result(Substring(fragText, res.mBegin, res.mEnd-res.mBegin)); michael@0: michael@0: if((uint32_t)fragText.Length() == res.mEnd) // if we hit the end of the fragment michael@0: { michael@0: nsAutoString curFragText = fragText; michael@0: for(uint32_t p = fragN +1; p < numOfFragment ;p++) michael@0: { michael@0: NS_ConvertASCIItoUTF16 nextFragText(wb[p]); michael@0: canBreak = wbk->BreakInBetween(curFragText.get(), michael@0: curFragText.Length(), michael@0: nextFragText.get(), michael@0: nextFragText.Length()); michael@0: if(canBreak) michael@0: break; michael@0: michael@0: nsWordRange r = wbk->FindWord(nextFragText.get(), nextFragText.Length(), michael@0: 0); michael@0: michael@0: result.Append(Substring(nextFragText, r.mBegin, r.mEnd - r.mBegin)); michael@0: michael@0: if((uint32_t)nextFragText.Length() != r.mEnd) michael@0: break; michael@0: michael@0: nextFragText.Assign(curFragText); michael@0: } michael@0: } michael@0: michael@0: if(0 == res.mBegin) // if we hit the beginning of the fragment michael@0: { michael@0: nsAutoString curFragText = fragText; michael@0: for(uint32_t p = fragN ; p > 0 ;p--) michael@0: { michael@0: NS_ConvertASCIItoUTF16 prevFragText(wb[p-1]); michael@0: canBreak = wbk->BreakInBetween(prevFragText.get(), michael@0: prevFragText.Length(), michael@0: curFragText.get(), michael@0: curFragText.Length()); michael@0: if(canBreak) michael@0: break; michael@0: michael@0: nsWordRange r = wbk->FindWord(prevFragText.get(), prevFragText.Length(), michael@0: prevFragText.Length()); michael@0: michael@0: result.Insert(Substring(prevFragText, r.mBegin, r.mEnd - r.mBegin), 0); michael@0: michael@0: if(0 != r.mBegin) michael@0: break; michael@0: michael@0: prevFragText.Assign(curFragText); michael@0: } michael@0: } michael@0: michael@0: printf("Output From SamplePrintWordWithBreak() \n\n"); michael@0: printf("[%s]\n", NS_ConvertUTF16toUTF8(result).get()); michael@0: michael@0: NS_IF_RELEASE(wbk); michael@0: } michael@0: michael@0: // Main michael@0: michael@0: int main(int argc, char** argv) { michael@0: michael@0: int rv = 0; michael@0: ScopedXPCOM xpcom("TestLineBreak"); michael@0: if (xpcom.failed()) michael@0: return -1; michael@0: michael@0: // -------------------------------------------- michael@0: printf("Test Line Break\n"); michael@0: michael@0: bool lbok ; michael@0: bool wbok ; michael@0: lbok =TestWordBreaker(); michael@0: if(lbok) michael@0: passed("Line Break Test"); michael@0: else { michael@0: fail("Line Break Test"); michael@0: rv = -1; michael@0: } michael@0: michael@0: wbok = TestLineBreaker(); michael@0: if(wbok) michael@0: passed("Word Break Test"); michael@0: else { michael@0: fail("Word Break Test"); michael@0: rv = -1; michael@0: } michael@0: michael@0: SampleWordBreakUsage(); michael@0: michael@0: michael@0: // -------------------------------------------- michael@0: printf("Finish All The Test Cases\n"); michael@0: michael@0: if(lbok && wbok) michael@0: passed("Line/Word Break Test"); michael@0: else { michael@0: fail("Line/Word Break Test"); michael@0: rv = -1; michael@0: } michael@0: return rv; michael@0: }