editor/libeditor/html/TextEditorTest.cpp

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial