summaryrefslogtreecommitdiff
path: root/editor/libeditor/TextEditorTest.cpp
blob: 5378bc33be6ea2f3595dc235f66c7407ed36aded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* -*- Mode: C++ tab-width: 2 indent-tabs-mode: nil c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifdef DEBUG

#include "TextEditorTest.h"

#include <stdio.h>

#include "nsDebug.h"
#include "nsError.h"
#include "nsGkAtoms.h"
#include "nsIDOMCharacterData.h"
#include "nsIDOMDocument.h"
#include "nsIDOMNode.h"
#include "nsIDOMNodeList.h"
#include "nsIEditor.h"
#include "nsIHTMLEditor.h"
#include "nsIPlaintextEditor.h"
#include "nsISelection.h"
#include "nsLiteralString.h"
#include "nsReadableUtils.h"
#include "nsString.h"
#include "nsStringFwd.h"

#define TEST_RESULT(r) { if (NS_FAILED(r)) {printf("FAILURE result=%X\n", static_cast<uint32_t>(r)); return r; } }
#define TEST_POINTER(p) { if (!p) {printf("FAILURE null pointer\n"); return NS_ERROR_NULL_POINTER; } }

TextEditorTest::TextEditorTest()
{
  printf("constructed a TextEditorTest\n");
}

TextEditorTest::~TextEditorTest()
{
  printf("destroyed a TextEditorTest\n");
}

void TextEditorTest::Run(nsIEditor *aEditor, int32_t *outNumTests, int32_t *outNumTestsFailed)
{
  if (!aEditor) return;
  mTextEditor = do_QueryInterface(aEditor);
  mEditor = do_QueryInterface(aEditor);
  RunUnitTest(outNumTests, outNumTestsFailed);
}

nsresult TextEditorTest::RunUnitTest(int32_t *outNumTests, int32_t *outNumTestsFailed)
{
  NS_ENSURE_TRUE(outNumTests && outNumTestsFailed, NS_ERROR_NULL_POINTER);

  *outNumTests = 0;
  *outNumTestsFailed = 0;

  nsresult rv = InitDoc();
  TEST_RESULT(rv);
  // shouldn't we just bail on error here?

  // insert some simple text
  rv = mTextEditor->InsertText(NS_LITERAL_STRING("1234567890abcdefghij1234567890"));
  TEST_RESULT(rv);
  (*outNumTests)++;
  if (NS_FAILED(rv)) {
    ++(*outNumTestsFailed);
  }

  // insert some more text
  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"));
  TEST_RESULT(rv);
  (*outNumTests)++;
  if (NS_FAILED(rv)) {
    ++(*outNumTestsFailed);
  }

  rv = TestInsertBreak();
  TEST_RESULT(rv);
  (*outNumTests)++;
  if (NS_FAILED(rv)) {
    ++(*outNumTestsFailed);
  }

  rv = TestTextProperties();
  TEST_RESULT(rv);
  (*outNumTests)++;
  if (NS_FAILED(rv)) {
    ++(*outNumTestsFailed);
  }

  // get us back to the original document
  rv = mEditor->Undo(12);
  TEST_RESULT(rv);

  return rv;
}

nsresult TextEditorTest::InitDoc()
{
  nsresult rv = mEditor->SelectAll();
  TEST_RESULT(rv);
  rv = mEditor->DeleteSelection(nsIEditor::eNext, nsIEditor::eStrip);
  TEST_RESULT(rv);
  return rv;
}

nsresult TextEditorTest::TestInsertBreak()
{
  nsCOMPtr<nsISelection>selection;
  nsresult rv = mEditor->GetSelection(getter_AddRefs(selection));
  TEST_RESULT(rv);
  TEST_POINTER(selection.get());
  nsCOMPtr<nsIDOMNode>anchor;
  rv = selection->GetAnchorNode(getter_AddRefs(anchor));
  TEST_RESULT(rv);
  TEST_POINTER(anchor.get());
  selection->Collapse(anchor, 0);
  // insert one break
  printf("inserting a break\n");
  rv = mTextEditor->InsertLineBreak();
  TEST_RESULT(rv);
  mEditor->DebugDumpContent();

  // insert a second break adjacent to the first
  printf("inserting a second break\n");
  rv = mTextEditor->InsertLineBreak();
  TEST_RESULT(rv);
  mEditor->DebugDumpContent();

  return rv;
}

nsresult TextEditorTest::TestTextProperties()
{
  nsCOMPtr<nsIDOMDocument>doc;
  nsresult rv = mEditor->GetDocument(getter_AddRefs(doc));
  TEST_RESULT(rv);
  TEST_POINTER(doc.get());
  nsCOMPtr<nsIDOMNodeList>nodeList;
  // XXX This is broken, text nodes are not elements.
  nsAutoString textTag(NS_LITERAL_STRING("#text"));
  rv = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList));
  TEST_RESULT(rv);
  TEST_POINTER(nodeList.get());
  uint32_t count;
  nodeList->GetLength(&count);
  NS_ASSERTION(0!=count, "there are no text nodes in the document!");
  nsCOMPtr<nsIDOMNode>textNode;
  rv = nodeList->Item(count - 1, getter_AddRefs(textNode));
  TEST_RESULT(rv);
  TEST_POINTER(textNode.get());

  // set the whole text node to bold
  printf("set the whole first text node to bold\n");
  nsCOMPtr<nsISelection>selection;
  rv = mEditor->GetSelection(getter_AddRefs(selection));
  TEST_RESULT(rv);
  TEST_POINTER(selection.get());
  nsCOMPtr<nsIDOMCharacterData>textData;
  textData = do_QueryInterface(textNode);
  uint32_t length;
  textData->GetLength(&length);
  selection->Collapse(textNode, 0);
  selection->Extend(textNode, length);

  nsCOMPtr<nsIHTMLEditor> htmlEditor (do_QueryInterface(mTextEditor));
  NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);

  bool any = false;
  bool all = false;
  bool first=false;

  const nsAFlatString& empty = EmptyString();

  rv = htmlEditor->GetInlineProperty(nsGkAtoms::b, empty, empty, &first,
                                     &any, &all);
  TEST_RESULT(rv);
  NS_ASSERTION(false==first, "first should be false");
  NS_ASSERTION(false==any, "any should be false");
  NS_ASSERTION(false==all, "all should be false");
  rv = htmlEditor->SetInlineProperty(nsGkAtoms::b, empty, empty);
  TEST_RESULT(rv);
  rv = htmlEditor->GetInlineProperty(nsGkAtoms::b, empty, empty, &first,
                                     &any, &all);
  TEST_RESULT(rv);
  NS_ASSERTION(true==first, "first should be true");
  NS_ASSERTION(true==any, "any should be true");
  NS_ASSERTION(true==all, "all should be true");
  mEditor->DebugDumpContent();

  // remove the bold we just set
  printf("set the whole first text node to not bold\n");
  rv = htmlEditor->RemoveInlineProperty(nsGkAtoms::b, empty);
  TEST_RESULT(rv);
  rv = htmlEditor->GetInlineProperty(nsGkAtoms::b, empty, empty, &first,
                                     &any, &all);
  TEST_RESULT(rv);
  NS_ASSERTION(false==first, "first should be false");
  NS_ASSERTION(false==any, "any should be false");
  NS_ASSERTION(false==all, "all should be false");
  mEditor->DebugDumpContent();

  // set all but the first and last character to bold
  printf("set the first text node (1, length-1) to bold and italic, and (2, length-1) to underline.\n");
  selection->Collapse(textNode, 1);
  selection->Extend(textNode, length-1);
  rv = htmlEditor->SetInlineProperty(nsGkAtoms::b, empty, empty);
  TEST_RESULT(rv);
  rv = htmlEditor->GetInlineProperty(nsGkAtoms::b, empty, empty, &first,
                                     &any, &all);
  TEST_RESULT(rv);
  NS_ASSERTION(true==first, "first should be true");
  NS_ASSERTION(true==any, "any should be true");
  NS_ASSERTION(true==all, "all should be true");
  mEditor->DebugDumpContent();
  // make all that same text italic
  rv = htmlEditor->SetInlineProperty(nsGkAtoms::i, empty, empty);
  TEST_RESULT(rv);
  rv = htmlEditor->GetInlineProperty(nsGkAtoms::i, empty, empty, &first,
                                     &any, &all);
  TEST_RESULT(rv);
  NS_ASSERTION(true==first, "first should be true");
  NS_ASSERTION(true==any, "any should be true");
  NS_ASSERTION(true==all, "all should be true");
  rv = htmlEditor->GetInlineProperty(nsGkAtoms::b, empty, empty, &first,
                                     &any, &all);
  TEST_RESULT(rv);
  NS_ASSERTION(true==first, "first should be true");
  NS_ASSERTION(true==any, "any should be true");
  NS_ASSERTION(true==all, "all should be true");
  mEditor->DebugDumpContent();

  // make all the text underlined, except for the first 2 and last 2 characters
  rv = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList));
  TEST_RESULT(rv);
  TEST_POINTER(nodeList.get());
  nodeList->GetLength(&count);
  NS_ASSERTION(0!=count, "there are no text nodes in the document!");
  rv = nodeList->Item(count-2, getter_AddRefs(textNode));
  TEST_RESULT(rv);
  TEST_POINTER(textNode.get());
  textData = do_QueryInterface(textNode);
  textData->GetLength(&length);
  NS_ASSERTION(length==915, "wrong text node");
  selection->Collapse(textNode, 1);
  selection->Extend(textNode, length-2);
  rv = htmlEditor->SetInlineProperty(nsGkAtoms::u, empty, empty);
  TEST_RESULT(rv);
  rv = htmlEditor->GetInlineProperty(nsGkAtoms::u, empty, empty, &first,
                                     &any, &all);
  TEST_RESULT(rv);
  NS_ASSERTION(true==first, "first should be true");
  NS_ASSERTION(true==any, "any should be true");
  NS_ASSERTION(true==all, "all should be true");
  mEditor->DebugDumpContent();

  return rv;
}

#endif