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