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: michael@0: #ifdef DEBUG michael@0: michael@0: #include michael@0: michael@0: #include "TextEditorTest.h" michael@0: #include "nsDebug.h" michael@0: #include "nsEditProperty.h" michael@0: #include "nsError.h" michael@0: #include "nsIDOMCharacterData.h" michael@0: #include "nsIDOMDocument.h" michael@0: #include "nsIDOMNode.h" michael@0: #include "nsIDOMNodeList.h" michael@0: #include "nsIEditor.h" michael@0: #include "nsIHTMLEditor.h" michael@0: #include "nsIPlaintextEditor.h" michael@0: #include "nsISelection.h" michael@0: #include "nsLiteralString.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "nsString.h" michael@0: #include "nsStringFwd.h" michael@0: michael@0: #define TEST_RESULT(r) { if (NS_FAILED(r)) {printf("FAILURE result=%X\n", static_cast(r)); return r; } } michael@0: #define TEST_POINTER(p) { if (!p) {printf("FAILURE null pointer\n"); return NS_ERROR_NULL_POINTER; } } michael@0: michael@0: TextEditorTest::TextEditorTest() michael@0: { michael@0: printf("constructed a TextEditorTest\n"); michael@0: } michael@0: michael@0: TextEditorTest::~TextEditorTest() michael@0: { michael@0: printf("destroyed a TextEditorTest\n"); michael@0: } michael@0: michael@0: void TextEditorTest::Run(nsIEditor *aEditor, int32_t *outNumTests, int32_t *outNumTestsFailed) michael@0: { michael@0: if (!aEditor) return; michael@0: mTextEditor = do_QueryInterface(aEditor); michael@0: mEditor = do_QueryInterface(aEditor); michael@0: RunUnitTest(outNumTests, outNumTestsFailed); michael@0: } michael@0: michael@0: nsresult TextEditorTest::RunUnitTest(int32_t *outNumTests, int32_t *outNumTestsFailed) michael@0: { michael@0: nsresult result; michael@0: michael@0: NS_ENSURE_TRUE(outNumTests && outNumTestsFailed, NS_ERROR_NULL_POINTER); michael@0: michael@0: *outNumTests = 0; michael@0: *outNumTestsFailed = 0; michael@0: michael@0: result = InitDoc(); michael@0: TEST_RESULT(result); michael@0: // shouldn't we just bail on error here? michael@0: michael@0: // insert some simple text michael@0: result = mTextEditor->InsertText(NS_LITERAL_STRING("1234567890abcdefghij1234567890")); michael@0: TEST_RESULT(result); michael@0: (*outNumTests)++; michael@0: if (NS_FAILED(result)) michael@0: ++(*outNumTestsFailed); michael@0: michael@0: // insert some more text michael@0: result = mTextEditor->InsertText(NS_LITERAL_STRING("Moreover, I am cognizant of the interrelatedness of all communities and states. I cannot sit idly by in Atlanta and not be concerned about what happens in Birmingham. Injustice anywhere is a threat to justice everywhere")); michael@0: TEST_RESULT(result); michael@0: (*outNumTests)++; michael@0: if (NS_FAILED(result)) michael@0: ++(*outNumTestsFailed); michael@0: michael@0: result = TestInsertBreak(); michael@0: TEST_RESULT(result); michael@0: (*outNumTests)++; michael@0: if (NS_FAILED(result)) michael@0: ++(*outNumTestsFailed); michael@0: michael@0: result = TestTextProperties(); michael@0: TEST_RESULT(result); michael@0: (*outNumTests)++; michael@0: if (NS_FAILED(result)) michael@0: ++(*outNumTestsFailed); michael@0: michael@0: // get us back to the original document michael@0: result = mEditor->Undo(12); michael@0: TEST_RESULT(result); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: nsresult TextEditorTest::InitDoc() michael@0: { michael@0: nsresult result = mEditor->SelectAll(); michael@0: TEST_RESULT(result); michael@0: result = mEditor->DeleteSelection(nsIEditor::eNext, nsIEditor::eStrip); michael@0: TEST_RESULT(result); michael@0: return result; michael@0: } michael@0: michael@0: nsresult TextEditorTest::TestInsertBreak() michael@0: { michael@0: nsCOMPtrselection; michael@0: nsresult result = mEditor->GetSelection(getter_AddRefs(selection)); michael@0: TEST_RESULT(result); michael@0: TEST_POINTER(selection.get()); michael@0: nsCOMPtranchor; michael@0: result = selection->GetAnchorNode(getter_AddRefs(anchor)); michael@0: TEST_RESULT(result); michael@0: TEST_POINTER(anchor.get()); michael@0: selection->Collapse(anchor, 0); michael@0: // insert one break michael@0: printf("inserting a break\n"); michael@0: result = mTextEditor->InsertLineBreak(); michael@0: TEST_RESULT(result); michael@0: mEditor->DebugDumpContent(); michael@0: michael@0: // insert a second break adjacent to the first michael@0: printf("inserting a second break\n"); michael@0: result = mTextEditor->InsertLineBreak(); michael@0: TEST_RESULT(result); michael@0: mEditor->DebugDumpContent(); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: nsresult TextEditorTest::TestTextProperties() michael@0: { michael@0: nsCOMPtrdoc; michael@0: nsresult result = mEditor->GetDocument(getter_AddRefs(doc)); michael@0: TEST_RESULT(result); michael@0: TEST_POINTER(doc.get()); michael@0: nsCOMPtrnodeList; michael@0: // XXX This is broken, text nodes are not elements. michael@0: nsAutoString textTag(NS_LITERAL_STRING("#text")); michael@0: result = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList)); michael@0: TEST_RESULT(result); michael@0: TEST_POINTER(nodeList.get()); michael@0: uint32_t count; michael@0: nodeList->GetLength(&count); michael@0: NS_ASSERTION(0!=count, "there are no text nodes in the document!"); michael@0: nsCOMPtrtextNode; michael@0: result = nodeList->Item(count-1, getter_AddRefs(textNode)); michael@0: TEST_RESULT(result); michael@0: TEST_POINTER(textNode.get()); michael@0: michael@0: // set the whole text node to bold michael@0: printf("set the whole first text node to bold\n"); michael@0: nsCOMPtrselection; michael@0: result = mEditor->GetSelection(getter_AddRefs(selection)); michael@0: TEST_RESULT(result); michael@0: TEST_POINTER(selection.get()); michael@0: nsCOMPtrtextData; michael@0: textData = do_QueryInterface(textNode); michael@0: uint32_t length; michael@0: textData->GetLength(&length); michael@0: selection->Collapse(textNode, 0); michael@0: selection->Extend(textNode, length); michael@0: michael@0: nsCOMPtr htmlEditor (do_QueryInterface(mTextEditor)); michael@0: NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); michael@0: michael@0: bool any = false; michael@0: bool all = false; michael@0: bool first=false; michael@0: michael@0: const nsAFlatString& empty = EmptyString(); michael@0: michael@0: result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all); michael@0: TEST_RESULT(result); michael@0: NS_ASSERTION(false==first, "first should be false"); michael@0: NS_ASSERTION(false==any, "any should be false"); michael@0: NS_ASSERTION(false==all, "all should be false"); michael@0: result = htmlEditor->SetInlineProperty(nsEditProperty::b, empty, empty); michael@0: TEST_RESULT(result); michael@0: result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all); michael@0: TEST_RESULT(result); michael@0: NS_ASSERTION(true==first, "first should be true"); michael@0: NS_ASSERTION(true==any, "any should be true"); michael@0: NS_ASSERTION(true==all, "all should be true"); michael@0: mEditor->DebugDumpContent(); michael@0: michael@0: // remove the bold we just set michael@0: printf("set the whole first text node to not bold\n"); michael@0: result = htmlEditor->RemoveInlineProperty(nsEditProperty::b, empty); michael@0: TEST_RESULT(result); michael@0: result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all); michael@0: TEST_RESULT(result); michael@0: NS_ASSERTION(false==first, "first should be false"); michael@0: NS_ASSERTION(false==any, "any should be false"); michael@0: NS_ASSERTION(false==all, "all should be false"); michael@0: mEditor->DebugDumpContent(); michael@0: michael@0: // set all but the first and last character to bold michael@0: printf("set the first text node (1, length-1) to bold and italic, and (2, length-1) to underline.\n"); michael@0: selection->Collapse(textNode, 1); michael@0: selection->Extend(textNode, length-1); michael@0: result = htmlEditor->SetInlineProperty(nsEditProperty::b, empty, empty); michael@0: TEST_RESULT(result); michael@0: result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all); michael@0: TEST_RESULT(result); michael@0: NS_ASSERTION(true==first, "first should be true"); michael@0: NS_ASSERTION(true==any, "any should be true"); michael@0: NS_ASSERTION(true==all, "all should be true"); michael@0: mEditor->DebugDumpContent(); michael@0: // make all that same text italic michael@0: result = htmlEditor->SetInlineProperty(nsEditProperty::i, empty, empty); michael@0: TEST_RESULT(result); michael@0: result = htmlEditor->GetInlineProperty(nsEditProperty::i, empty, empty, &first, &any, &all); michael@0: TEST_RESULT(result); michael@0: NS_ASSERTION(true==first, "first should be true"); michael@0: NS_ASSERTION(true==any, "any should be true"); michael@0: NS_ASSERTION(true==all, "all should be true"); michael@0: result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all); michael@0: TEST_RESULT(result); michael@0: NS_ASSERTION(true==first, "first should be true"); michael@0: NS_ASSERTION(true==any, "any should be true"); michael@0: NS_ASSERTION(true==all, "all should be true"); michael@0: mEditor->DebugDumpContent(); michael@0: michael@0: // make all the text underlined, except for the first 2 and last 2 characters michael@0: result = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList)); michael@0: TEST_RESULT(result); michael@0: TEST_POINTER(nodeList.get()); michael@0: nodeList->GetLength(&count); michael@0: NS_ASSERTION(0!=count, "there are no text nodes in the document!"); michael@0: result = nodeList->Item(count-2, getter_AddRefs(textNode)); michael@0: TEST_RESULT(result); michael@0: TEST_POINTER(textNode.get()); michael@0: textData = do_QueryInterface(textNode); michael@0: textData->GetLength(&length); michael@0: NS_ASSERTION(length==915, "wrong text node"); michael@0: selection->Collapse(textNode, 1); michael@0: selection->Extend(textNode, length-2); michael@0: result = htmlEditor->SetInlineProperty(nsEditProperty::u, empty, empty); michael@0: TEST_RESULT(result); michael@0: result = htmlEditor->GetInlineProperty(nsEditProperty::u, empty, empty, &first, &any, &all); michael@0: TEST_RESULT(result); michael@0: NS_ASSERTION(true==first, "first should be true"); michael@0: NS_ASSERTION(true==any, "any should be true"); michael@0: NS_ASSERTION(true==all, "all should be true"); michael@0: mEditor->DebugDumpContent(); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: michael@0: michael@0: #endif michael@0: michael@0: