editor/libeditor/html/TextEditorTest.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/editor/libeditor/html/TextEditorTest.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,253 @@
     1.4 +/* -*- Mode: C++ tab-width: 2 indent-tabs-mode: nil c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifdef DEBUG
    1.10 +
    1.11 +#include <stdio.h>
    1.12 +
    1.13 +#include "TextEditorTest.h"
    1.14 +#include "nsDebug.h"
    1.15 +#include "nsEditProperty.h"
    1.16 +#include "nsError.h"
    1.17 +#include "nsIDOMCharacterData.h"
    1.18 +#include "nsIDOMDocument.h"
    1.19 +#include "nsIDOMNode.h"
    1.20 +#include "nsIDOMNodeList.h"
    1.21 +#include "nsIEditor.h"
    1.22 +#include "nsIHTMLEditor.h"
    1.23 +#include "nsIPlaintextEditor.h"
    1.24 +#include "nsISelection.h"
    1.25 +#include "nsLiteralString.h"
    1.26 +#include "nsReadableUtils.h"
    1.27 +#include "nsString.h"
    1.28 +#include "nsStringFwd.h"
    1.29 +
    1.30 +#define TEST_RESULT(r) { if (NS_FAILED(r)) {printf("FAILURE result=%X\n", static_cast<uint32_t>(r)); return r; } }
    1.31 +#define TEST_POINTER(p) { if (!p) {printf("FAILURE null pointer\n"); return NS_ERROR_NULL_POINTER; } }
    1.32 +
    1.33 +TextEditorTest::TextEditorTest()
    1.34 +{
    1.35 +  printf("constructed a TextEditorTest\n");
    1.36 +}
    1.37 +
    1.38 +TextEditorTest::~TextEditorTest()
    1.39 +{
    1.40 +  printf("destroyed a TextEditorTest\n");
    1.41 +}
    1.42 +
    1.43 +void TextEditorTest::Run(nsIEditor *aEditor, int32_t *outNumTests, int32_t *outNumTestsFailed)
    1.44 +{
    1.45 +  if (!aEditor) return;
    1.46 +  mTextEditor = do_QueryInterface(aEditor);
    1.47 +  mEditor = do_QueryInterface(aEditor);
    1.48 +  RunUnitTest(outNumTests, outNumTestsFailed);
    1.49 +}
    1.50 +
    1.51 +nsresult TextEditorTest::RunUnitTest(int32_t *outNumTests, int32_t *outNumTestsFailed)
    1.52 +{
    1.53 +  nsresult result;
    1.54 +  
    1.55 +  NS_ENSURE_TRUE(outNumTests && outNumTestsFailed, NS_ERROR_NULL_POINTER);
    1.56 +  
    1.57 +  *outNumTests = 0;
    1.58 +  *outNumTestsFailed = 0;
    1.59 +  
    1.60 +  result = InitDoc();
    1.61 +  TEST_RESULT(result);
    1.62 +  // shouldn't we just bail on error here?
    1.63 +  
    1.64 +  // insert some simple text
    1.65 +  result = mTextEditor->InsertText(NS_LITERAL_STRING("1234567890abcdefghij1234567890"));
    1.66 +  TEST_RESULT(result);
    1.67 +  (*outNumTests)++;
    1.68 +  if (NS_FAILED(result))
    1.69 +    ++(*outNumTestsFailed);
    1.70 +  
    1.71 +  // insert some more text
    1.72 +  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"));
    1.73 +  TEST_RESULT(result);
    1.74 +  (*outNumTests)++;
    1.75 +  if (NS_FAILED(result))
    1.76 +    ++(*outNumTestsFailed);
    1.77 +
    1.78 +  result = TestInsertBreak();
    1.79 +  TEST_RESULT(result);
    1.80 +  (*outNumTests)++;
    1.81 +  if (NS_FAILED(result))
    1.82 +    ++(*outNumTestsFailed);
    1.83 +
    1.84 +  result = TestTextProperties();
    1.85 +  TEST_RESULT(result);
    1.86 +  (*outNumTests)++;
    1.87 +  if (NS_FAILED(result))
    1.88 +    ++(*outNumTestsFailed);
    1.89 +
    1.90 +  // get us back to the original document
    1.91 +  result = mEditor->Undo(12);
    1.92 +  TEST_RESULT(result);
    1.93 +
    1.94 +  return result;
    1.95 +}
    1.96 +
    1.97 +nsresult TextEditorTest::InitDoc()
    1.98 +{
    1.99 +  nsresult result = mEditor->SelectAll();
   1.100 +  TEST_RESULT(result);
   1.101 +  result = mEditor->DeleteSelection(nsIEditor::eNext, nsIEditor::eStrip);
   1.102 +  TEST_RESULT(result);
   1.103 +  return result;
   1.104 +}
   1.105 +
   1.106 +nsresult TextEditorTest::TestInsertBreak()
   1.107 +{
   1.108 +  nsCOMPtr<nsISelection>selection;
   1.109 +  nsresult result = mEditor->GetSelection(getter_AddRefs(selection));
   1.110 +  TEST_RESULT(result);
   1.111 +  TEST_POINTER(selection.get());
   1.112 +  nsCOMPtr<nsIDOMNode>anchor;
   1.113 +  result = selection->GetAnchorNode(getter_AddRefs(anchor));
   1.114 +  TEST_RESULT(result);
   1.115 +  TEST_POINTER(anchor.get());
   1.116 +  selection->Collapse(anchor, 0);
   1.117 +  // insert one break
   1.118 +  printf("inserting a break\n");
   1.119 +  result = mTextEditor->InsertLineBreak();
   1.120 +  TEST_RESULT(result);
   1.121 +  mEditor->DebugDumpContent();
   1.122 +
   1.123 +  // insert a second break adjacent to the first
   1.124 +  printf("inserting a second break\n");
   1.125 +  result = mTextEditor->InsertLineBreak();
   1.126 +  TEST_RESULT(result);
   1.127 +  mEditor->DebugDumpContent();
   1.128 +    
   1.129 +  return result;
   1.130 +}
   1.131 +
   1.132 +nsresult TextEditorTest::TestTextProperties()
   1.133 +{
   1.134 +  nsCOMPtr<nsIDOMDocument>doc;
   1.135 +  nsresult result = mEditor->GetDocument(getter_AddRefs(doc));
   1.136 +  TEST_RESULT(result);
   1.137 +  TEST_POINTER(doc.get());
   1.138 +  nsCOMPtr<nsIDOMNodeList>nodeList;
   1.139 +  // XXX This is broken, text nodes are not elements.
   1.140 +  nsAutoString textTag(NS_LITERAL_STRING("#text"));
   1.141 +  result = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList));
   1.142 +  TEST_RESULT(result);
   1.143 +  TEST_POINTER(nodeList.get());
   1.144 +  uint32_t count;
   1.145 +  nodeList->GetLength(&count);
   1.146 +  NS_ASSERTION(0!=count, "there are no text nodes in the document!");
   1.147 +  nsCOMPtr<nsIDOMNode>textNode;
   1.148 +  result = nodeList->Item(count-1, getter_AddRefs(textNode));
   1.149 +  TEST_RESULT(result);
   1.150 +  TEST_POINTER(textNode.get());
   1.151 +
   1.152 +  // set the whole text node to bold
   1.153 +  printf("set the whole first text node to bold\n");
   1.154 +  nsCOMPtr<nsISelection>selection;
   1.155 +  result = mEditor->GetSelection(getter_AddRefs(selection));
   1.156 +  TEST_RESULT(result);
   1.157 +  TEST_POINTER(selection.get());
   1.158 +  nsCOMPtr<nsIDOMCharacterData>textData;
   1.159 +  textData = do_QueryInterface(textNode);
   1.160 +  uint32_t length;
   1.161 +  textData->GetLength(&length);
   1.162 +  selection->Collapse(textNode, 0);
   1.163 +  selection->Extend(textNode, length);
   1.164 +
   1.165 +  nsCOMPtr<nsIHTMLEditor> htmlEditor (do_QueryInterface(mTextEditor));
   1.166 +  NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
   1.167 +
   1.168 +  bool any = false;
   1.169 +  bool all = false;
   1.170 +  bool first=false;
   1.171 +
   1.172 +  const nsAFlatString& empty = EmptyString();
   1.173 +
   1.174 +  result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
   1.175 +  TEST_RESULT(result);
   1.176 +  NS_ASSERTION(false==first, "first should be false");
   1.177 +  NS_ASSERTION(false==any, "any should be false");
   1.178 +  NS_ASSERTION(false==all, "all should be false");
   1.179 +  result = htmlEditor->SetInlineProperty(nsEditProperty::b, empty, empty);
   1.180 +  TEST_RESULT(result);
   1.181 +  result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
   1.182 +  TEST_RESULT(result);
   1.183 +  NS_ASSERTION(true==first, "first should be true");
   1.184 +  NS_ASSERTION(true==any, "any should be true");
   1.185 +  NS_ASSERTION(true==all, "all should be true");
   1.186 +  mEditor->DebugDumpContent();
   1.187 +
   1.188 +  // remove the bold we just set
   1.189 +  printf("set the whole first text node to not bold\n");
   1.190 +  result = htmlEditor->RemoveInlineProperty(nsEditProperty::b, empty);
   1.191 +  TEST_RESULT(result);
   1.192 +  result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
   1.193 +  TEST_RESULT(result);
   1.194 +  NS_ASSERTION(false==first, "first should be false");
   1.195 +  NS_ASSERTION(false==any, "any should be false");
   1.196 +  NS_ASSERTION(false==all, "all should be false");
   1.197 +  mEditor->DebugDumpContent();
   1.198 +
   1.199 +  // set all but the first and last character to bold
   1.200 +  printf("set the first text node (1, length-1) to bold and italic, and (2, length-1) to underline.\n");
   1.201 +  selection->Collapse(textNode, 1);
   1.202 +  selection->Extend(textNode, length-1);
   1.203 +  result = htmlEditor->SetInlineProperty(nsEditProperty::b, empty, empty);
   1.204 +  TEST_RESULT(result);
   1.205 +  result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
   1.206 +  TEST_RESULT(result);
   1.207 +  NS_ASSERTION(true==first, "first should be true");
   1.208 +  NS_ASSERTION(true==any, "any should be true");
   1.209 +  NS_ASSERTION(true==all, "all should be true");
   1.210 +  mEditor->DebugDumpContent();
   1.211 +  // make all that same text italic
   1.212 +  result = htmlEditor->SetInlineProperty(nsEditProperty::i, empty, empty);
   1.213 +  TEST_RESULT(result);
   1.214 +  result = htmlEditor->GetInlineProperty(nsEditProperty::i, empty, empty, &first, &any, &all);
   1.215 +  TEST_RESULT(result);
   1.216 +  NS_ASSERTION(true==first, "first should be true");
   1.217 +  NS_ASSERTION(true==any, "any should be true");
   1.218 +  NS_ASSERTION(true==all, "all should be true");
   1.219 +  result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
   1.220 +  TEST_RESULT(result);
   1.221 +  NS_ASSERTION(true==first, "first should be true");
   1.222 +  NS_ASSERTION(true==any, "any should be true");
   1.223 +  NS_ASSERTION(true==all, "all should be true");
   1.224 +  mEditor->DebugDumpContent();
   1.225 +
   1.226 +  // make all the text underlined, except for the first 2 and last 2 characters
   1.227 +  result = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList));
   1.228 +  TEST_RESULT(result);
   1.229 +  TEST_POINTER(nodeList.get());
   1.230 +  nodeList->GetLength(&count);
   1.231 +  NS_ASSERTION(0!=count, "there are no text nodes in the document!");
   1.232 +  result = nodeList->Item(count-2, getter_AddRefs(textNode));
   1.233 +  TEST_RESULT(result);
   1.234 +  TEST_POINTER(textNode.get());
   1.235 +  textData = do_QueryInterface(textNode);
   1.236 +  textData->GetLength(&length);
   1.237 +  NS_ASSERTION(length==915, "wrong text node");
   1.238 +  selection->Collapse(textNode, 1);
   1.239 +  selection->Extend(textNode, length-2);
   1.240 +  result = htmlEditor->SetInlineProperty(nsEditProperty::u, empty, empty);
   1.241 +  TEST_RESULT(result);
   1.242 +  result = htmlEditor->GetInlineProperty(nsEditProperty::u, empty, empty, &first, &any, &all);
   1.243 +  TEST_RESULT(result);
   1.244 +  NS_ASSERTION(true==first, "first should be true");
   1.245 +  NS_ASSERTION(true==any, "any should be true");
   1.246 +  NS_ASSERTION(true==all, "all should be true");
   1.247 +  mEditor->DebugDumpContent();
   1.248 +
   1.249 +  return result;
   1.250 +}
   1.251 +
   1.252 +
   1.253 +
   1.254 +#endif
   1.255 +
   1.256 +

mercurial