editor/libeditor/html/TextEditorTest.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++ tab-width: 2 indent-tabs-mode: nil c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifdef DEBUG
michael@0 7
michael@0 8 #include <stdio.h>
michael@0 9
michael@0 10 #include "TextEditorTest.h"
michael@0 11 #include "nsDebug.h"
michael@0 12 #include "nsEditProperty.h"
michael@0 13 #include "nsError.h"
michael@0 14 #include "nsIDOMCharacterData.h"
michael@0 15 #include "nsIDOMDocument.h"
michael@0 16 #include "nsIDOMNode.h"
michael@0 17 #include "nsIDOMNodeList.h"
michael@0 18 #include "nsIEditor.h"
michael@0 19 #include "nsIHTMLEditor.h"
michael@0 20 #include "nsIPlaintextEditor.h"
michael@0 21 #include "nsISelection.h"
michael@0 22 #include "nsLiteralString.h"
michael@0 23 #include "nsReadableUtils.h"
michael@0 24 #include "nsString.h"
michael@0 25 #include "nsStringFwd.h"
michael@0 26
michael@0 27 #define TEST_RESULT(r) { if (NS_FAILED(r)) {printf("FAILURE result=%X\n", static_cast<uint32_t>(r)); return r; } }
michael@0 28 #define TEST_POINTER(p) { if (!p) {printf("FAILURE null pointer\n"); return NS_ERROR_NULL_POINTER; } }
michael@0 29
michael@0 30 TextEditorTest::TextEditorTest()
michael@0 31 {
michael@0 32 printf("constructed a TextEditorTest\n");
michael@0 33 }
michael@0 34
michael@0 35 TextEditorTest::~TextEditorTest()
michael@0 36 {
michael@0 37 printf("destroyed a TextEditorTest\n");
michael@0 38 }
michael@0 39
michael@0 40 void TextEditorTest::Run(nsIEditor *aEditor, int32_t *outNumTests, int32_t *outNumTestsFailed)
michael@0 41 {
michael@0 42 if (!aEditor) return;
michael@0 43 mTextEditor = do_QueryInterface(aEditor);
michael@0 44 mEditor = do_QueryInterface(aEditor);
michael@0 45 RunUnitTest(outNumTests, outNumTestsFailed);
michael@0 46 }
michael@0 47
michael@0 48 nsresult TextEditorTest::RunUnitTest(int32_t *outNumTests, int32_t *outNumTestsFailed)
michael@0 49 {
michael@0 50 nsresult result;
michael@0 51
michael@0 52 NS_ENSURE_TRUE(outNumTests && outNumTestsFailed, NS_ERROR_NULL_POINTER);
michael@0 53
michael@0 54 *outNumTests = 0;
michael@0 55 *outNumTestsFailed = 0;
michael@0 56
michael@0 57 result = InitDoc();
michael@0 58 TEST_RESULT(result);
michael@0 59 // shouldn't we just bail on error here?
michael@0 60
michael@0 61 // insert some simple text
michael@0 62 result = mTextEditor->InsertText(NS_LITERAL_STRING("1234567890abcdefghij1234567890"));
michael@0 63 TEST_RESULT(result);
michael@0 64 (*outNumTests)++;
michael@0 65 if (NS_FAILED(result))
michael@0 66 ++(*outNumTestsFailed);
michael@0 67
michael@0 68 // insert some more text
michael@0 69 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 70 TEST_RESULT(result);
michael@0 71 (*outNumTests)++;
michael@0 72 if (NS_FAILED(result))
michael@0 73 ++(*outNumTestsFailed);
michael@0 74
michael@0 75 result = TestInsertBreak();
michael@0 76 TEST_RESULT(result);
michael@0 77 (*outNumTests)++;
michael@0 78 if (NS_FAILED(result))
michael@0 79 ++(*outNumTestsFailed);
michael@0 80
michael@0 81 result = TestTextProperties();
michael@0 82 TEST_RESULT(result);
michael@0 83 (*outNumTests)++;
michael@0 84 if (NS_FAILED(result))
michael@0 85 ++(*outNumTestsFailed);
michael@0 86
michael@0 87 // get us back to the original document
michael@0 88 result = mEditor->Undo(12);
michael@0 89 TEST_RESULT(result);
michael@0 90
michael@0 91 return result;
michael@0 92 }
michael@0 93
michael@0 94 nsresult TextEditorTest::InitDoc()
michael@0 95 {
michael@0 96 nsresult result = mEditor->SelectAll();
michael@0 97 TEST_RESULT(result);
michael@0 98 result = mEditor->DeleteSelection(nsIEditor::eNext, nsIEditor::eStrip);
michael@0 99 TEST_RESULT(result);
michael@0 100 return result;
michael@0 101 }
michael@0 102
michael@0 103 nsresult TextEditorTest::TestInsertBreak()
michael@0 104 {
michael@0 105 nsCOMPtr<nsISelection>selection;
michael@0 106 nsresult result = mEditor->GetSelection(getter_AddRefs(selection));
michael@0 107 TEST_RESULT(result);
michael@0 108 TEST_POINTER(selection.get());
michael@0 109 nsCOMPtr<nsIDOMNode>anchor;
michael@0 110 result = selection->GetAnchorNode(getter_AddRefs(anchor));
michael@0 111 TEST_RESULT(result);
michael@0 112 TEST_POINTER(anchor.get());
michael@0 113 selection->Collapse(anchor, 0);
michael@0 114 // insert one break
michael@0 115 printf("inserting a break\n");
michael@0 116 result = mTextEditor->InsertLineBreak();
michael@0 117 TEST_RESULT(result);
michael@0 118 mEditor->DebugDumpContent();
michael@0 119
michael@0 120 // insert a second break adjacent to the first
michael@0 121 printf("inserting a second break\n");
michael@0 122 result = mTextEditor->InsertLineBreak();
michael@0 123 TEST_RESULT(result);
michael@0 124 mEditor->DebugDumpContent();
michael@0 125
michael@0 126 return result;
michael@0 127 }
michael@0 128
michael@0 129 nsresult TextEditorTest::TestTextProperties()
michael@0 130 {
michael@0 131 nsCOMPtr<nsIDOMDocument>doc;
michael@0 132 nsresult result = mEditor->GetDocument(getter_AddRefs(doc));
michael@0 133 TEST_RESULT(result);
michael@0 134 TEST_POINTER(doc.get());
michael@0 135 nsCOMPtr<nsIDOMNodeList>nodeList;
michael@0 136 // XXX This is broken, text nodes are not elements.
michael@0 137 nsAutoString textTag(NS_LITERAL_STRING("#text"));
michael@0 138 result = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList));
michael@0 139 TEST_RESULT(result);
michael@0 140 TEST_POINTER(nodeList.get());
michael@0 141 uint32_t count;
michael@0 142 nodeList->GetLength(&count);
michael@0 143 NS_ASSERTION(0!=count, "there are no text nodes in the document!");
michael@0 144 nsCOMPtr<nsIDOMNode>textNode;
michael@0 145 result = nodeList->Item(count-1, getter_AddRefs(textNode));
michael@0 146 TEST_RESULT(result);
michael@0 147 TEST_POINTER(textNode.get());
michael@0 148
michael@0 149 // set the whole text node to bold
michael@0 150 printf("set the whole first text node to bold\n");
michael@0 151 nsCOMPtr<nsISelection>selection;
michael@0 152 result = mEditor->GetSelection(getter_AddRefs(selection));
michael@0 153 TEST_RESULT(result);
michael@0 154 TEST_POINTER(selection.get());
michael@0 155 nsCOMPtr<nsIDOMCharacterData>textData;
michael@0 156 textData = do_QueryInterface(textNode);
michael@0 157 uint32_t length;
michael@0 158 textData->GetLength(&length);
michael@0 159 selection->Collapse(textNode, 0);
michael@0 160 selection->Extend(textNode, length);
michael@0 161
michael@0 162 nsCOMPtr<nsIHTMLEditor> htmlEditor (do_QueryInterface(mTextEditor));
michael@0 163 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 164
michael@0 165 bool any = false;
michael@0 166 bool all = false;
michael@0 167 bool first=false;
michael@0 168
michael@0 169 const nsAFlatString& empty = EmptyString();
michael@0 170
michael@0 171 result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
michael@0 172 TEST_RESULT(result);
michael@0 173 NS_ASSERTION(false==first, "first should be false");
michael@0 174 NS_ASSERTION(false==any, "any should be false");
michael@0 175 NS_ASSERTION(false==all, "all should be false");
michael@0 176 result = htmlEditor->SetInlineProperty(nsEditProperty::b, empty, empty);
michael@0 177 TEST_RESULT(result);
michael@0 178 result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
michael@0 179 TEST_RESULT(result);
michael@0 180 NS_ASSERTION(true==first, "first should be true");
michael@0 181 NS_ASSERTION(true==any, "any should be true");
michael@0 182 NS_ASSERTION(true==all, "all should be true");
michael@0 183 mEditor->DebugDumpContent();
michael@0 184
michael@0 185 // remove the bold we just set
michael@0 186 printf("set the whole first text node to not bold\n");
michael@0 187 result = htmlEditor->RemoveInlineProperty(nsEditProperty::b, empty);
michael@0 188 TEST_RESULT(result);
michael@0 189 result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
michael@0 190 TEST_RESULT(result);
michael@0 191 NS_ASSERTION(false==first, "first should be false");
michael@0 192 NS_ASSERTION(false==any, "any should be false");
michael@0 193 NS_ASSERTION(false==all, "all should be false");
michael@0 194 mEditor->DebugDumpContent();
michael@0 195
michael@0 196 // set all but the first and last character to bold
michael@0 197 printf("set the first text node (1, length-1) to bold and italic, and (2, length-1) to underline.\n");
michael@0 198 selection->Collapse(textNode, 1);
michael@0 199 selection->Extend(textNode, length-1);
michael@0 200 result = htmlEditor->SetInlineProperty(nsEditProperty::b, empty, empty);
michael@0 201 TEST_RESULT(result);
michael@0 202 result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
michael@0 203 TEST_RESULT(result);
michael@0 204 NS_ASSERTION(true==first, "first should be true");
michael@0 205 NS_ASSERTION(true==any, "any should be true");
michael@0 206 NS_ASSERTION(true==all, "all should be true");
michael@0 207 mEditor->DebugDumpContent();
michael@0 208 // make all that same text italic
michael@0 209 result = htmlEditor->SetInlineProperty(nsEditProperty::i, empty, empty);
michael@0 210 TEST_RESULT(result);
michael@0 211 result = htmlEditor->GetInlineProperty(nsEditProperty::i, empty, empty, &first, &any, &all);
michael@0 212 TEST_RESULT(result);
michael@0 213 NS_ASSERTION(true==first, "first should be true");
michael@0 214 NS_ASSERTION(true==any, "any should be true");
michael@0 215 NS_ASSERTION(true==all, "all should be true");
michael@0 216 result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
michael@0 217 TEST_RESULT(result);
michael@0 218 NS_ASSERTION(true==first, "first should be true");
michael@0 219 NS_ASSERTION(true==any, "any should be true");
michael@0 220 NS_ASSERTION(true==all, "all should be true");
michael@0 221 mEditor->DebugDumpContent();
michael@0 222
michael@0 223 // make all the text underlined, except for the first 2 and last 2 characters
michael@0 224 result = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList));
michael@0 225 TEST_RESULT(result);
michael@0 226 TEST_POINTER(nodeList.get());
michael@0 227 nodeList->GetLength(&count);
michael@0 228 NS_ASSERTION(0!=count, "there are no text nodes in the document!");
michael@0 229 result = nodeList->Item(count-2, getter_AddRefs(textNode));
michael@0 230 TEST_RESULT(result);
michael@0 231 TEST_POINTER(textNode.get());
michael@0 232 textData = do_QueryInterface(textNode);
michael@0 233 textData->GetLength(&length);
michael@0 234 NS_ASSERTION(length==915, "wrong text node");
michael@0 235 selection->Collapse(textNode, 1);
michael@0 236 selection->Extend(textNode, length-2);
michael@0 237 result = htmlEditor->SetInlineProperty(nsEditProperty::u, empty, empty);
michael@0 238 TEST_RESULT(result);
michael@0 239 result = htmlEditor->GetInlineProperty(nsEditProperty::u, empty, empty, &first, &any, &all);
michael@0 240 TEST_RESULT(result);
michael@0 241 NS_ASSERTION(true==first, "first should be true");
michael@0 242 NS_ASSERTION(true==any, "any should be true");
michael@0 243 NS_ASSERTION(true==all, "all should be true");
michael@0 244 mEditor->DebugDumpContent();
michael@0 245
michael@0 246 return result;
michael@0 247 }
michael@0 248
michael@0 249
michael@0 250
michael@0 251 #endif
michael@0 252
michael@0 253

mercurial