LCOV - code coverage report
Current view: directory - editor/libeditor/html - TextEditorTest.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 165 0 0.0 %
Date: 2012-06-02 Functions: 7 0 0.0 %

       1                 : /* -*- Mode: C++ tab-width: 2 indent-tabs-mode: nil c-basic-offset: 2 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is mozilla.org code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * Netscape Communications Corporation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 1998
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *
      24                 :  * Alternatively, the contents of this file may be used under the terms of
      25                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
      26                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      27                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      28                 :  * of those above. If you wish to allow use of your version of this file only
      29                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      30                 :  * use your version of this file under the terms of the MPL, indicate your
      31                 :  * decision by deleting the provisions above and replace them with the notice
      32                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      33                 :  * the provisions above, a recipient may use your version of this file under
      34                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      35                 :  *
      36                 :  * ***** END LICENSE BLOCK ***** */
      37                 : 
      38                 : #ifdef DEBUG
      39                 : 
      40                 : #include <stdio.h>
      41                 : 
      42                 : #include "nsIEditor.h"
      43                 : #include "nsIHTMLEditor.h"
      44                 : #include "TextEditorTest.h"
      45                 : #include "nsISelection.h"
      46                 : #include "nsIDOMCharacterData.h"
      47                 : #include "nsIDOMDocument.h"
      48                 : #include "nsIDOMNode.h"
      49                 : #include "nsIDOMNodeList.h"
      50                 : #include "nsEditProperty.h"
      51                 : #include "nsString.h"
      52                 : #include "nsReadableUtils.h"
      53                 : 
      54                 : #define TEST_RESULT(r) { if (NS_FAILED(r)) {printf("FAILURE result=%X\n", r); return r; } }
      55                 : #define TEST_POINTER(p) { if (!p) {printf("FAILURE null pointer\n"); return NS_ERROR_NULL_POINTER; } }
      56                 : 
      57               0 : TextEditorTest::TextEditorTest()
      58                 : {
      59               0 :   printf("constructed a TextEditorTest\n");
      60               0 : }
      61                 : 
      62               0 : TextEditorTest::~TextEditorTest()
      63                 : {
      64               0 :   printf("destroyed a TextEditorTest\n");
      65               0 : }
      66                 : 
      67               0 : void TextEditorTest::Run(nsIEditor *aEditor, PRInt32 *outNumTests, PRInt32 *outNumTestsFailed)
      68                 : {
      69               0 :   if (!aEditor) return;
      70               0 :   mTextEditor = do_QueryInterface(aEditor);
      71               0 :   mEditor = do_QueryInterface(aEditor);
      72               0 :   RunUnitTest(outNumTests, outNumTestsFailed);
      73                 : }
      74                 : 
      75               0 : nsresult TextEditorTest::RunUnitTest(PRInt32 *outNumTests, PRInt32 *outNumTestsFailed)
      76                 : {
      77                 :   nsresult result;
      78                 :   
      79               0 :   NS_ENSURE_TRUE(outNumTests && outNumTestsFailed, NS_ERROR_NULL_POINTER);
      80                 :   
      81               0 :   *outNumTests = 0;
      82               0 :   *outNumTestsFailed = 0;
      83                 :   
      84               0 :   result = InitDoc();
      85               0 :   TEST_RESULT(result);
      86                 :   // shouldn't we just bail on error here?
      87                 :   
      88                 :   // insert some simple text
      89               0 :   result = mTextEditor->InsertText(NS_LITERAL_STRING("1234567890abcdefghij1234567890"));
      90               0 :   TEST_RESULT(result);
      91               0 :   (*outNumTests)++;
      92               0 :   if (NS_FAILED(result))
      93               0 :     ++(*outNumTestsFailed);
      94                 :   
      95                 :   // insert some more text
      96               0 :   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"));
      97               0 :   TEST_RESULT(result);
      98               0 :   (*outNumTests)++;
      99               0 :   if (NS_FAILED(result))
     100               0 :     ++(*outNumTestsFailed);
     101                 : 
     102               0 :   result = TestInsertBreak();
     103               0 :   TEST_RESULT(result);
     104               0 :   (*outNumTests)++;
     105               0 :   if (NS_FAILED(result))
     106               0 :     ++(*outNumTestsFailed);
     107                 : 
     108               0 :   result = TestTextProperties();
     109               0 :   TEST_RESULT(result);
     110               0 :   (*outNumTests)++;
     111               0 :   if (NS_FAILED(result))
     112               0 :     ++(*outNumTestsFailed);
     113                 : 
     114                 :   // get us back to the original document
     115               0 :   result = mEditor->Undo(12);
     116               0 :   TEST_RESULT(result);
     117                 : 
     118               0 :   return result;
     119                 : }
     120                 : 
     121               0 : nsresult TextEditorTest::InitDoc()
     122                 : {
     123               0 :   nsresult result = mEditor->SelectAll();
     124               0 :   TEST_RESULT(result);
     125               0 :   result = mEditor->DeleteSelection(nsIEditor::eNext);
     126               0 :   TEST_RESULT(result);
     127               0 :   return result;
     128                 : }
     129                 : 
     130               0 : nsresult TextEditorTest::TestInsertBreak()
     131                 : {
     132               0 :   nsCOMPtr<nsISelection>selection;
     133               0 :   nsresult result = mEditor->GetSelection(getter_AddRefs(selection));
     134               0 :   TEST_RESULT(result);
     135               0 :   TEST_POINTER(selection.get());
     136               0 :   nsCOMPtr<nsIDOMNode>anchor;
     137               0 :   result = selection->GetAnchorNode(getter_AddRefs(anchor));
     138               0 :   TEST_RESULT(result);
     139               0 :   TEST_POINTER(anchor.get());
     140               0 :   selection->Collapse(anchor, 0);
     141                 :   // insert one break
     142               0 :   printf("inserting a break\n");
     143               0 :   result = mTextEditor->InsertLineBreak();
     144               0 :   TEST_RESULT(result);
     145               0 :   mEditor->DebugDumpContent();
     146                 : 
     147                 :   // insert a second break adjacent to the first
     148               0 :   printf("inserting a second break\n");
     149               0 :   result = mTextEditor->InsertLineBreak();
     150               0 :   TEST_RESULT(result);
     151               0 :   mEditor->DebugDumpContent();
     152                 :     
     153               0 :   return result;
     154                 : }
     155                 : 
     156               0 : nsresult TextEditorTest::TestTextProperties()
     157                 : {
     158               0 :   nsCOMPtr<nsIDOMDocument>doc;
     159               0 :   nsresult result = mEditor->GetDocument(getter_AddRefs(doc));
     160               0 :   TEST_RESULT(result);
     161               0 :   TEST_POINTER(doc.get());
     162               0 :   nsCOMPtr<nsIDOMNodeList>nodeList;
     163                 :   // XXX This is broken, text nodes are not elements.
     164               0 :   nsAutoString textTag(NS_LITERAL_STRING("#text"));
     165               0 :   result = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList));
     166               0 :   TEST_RESULT(result);
     167               0 :   TEST_POINTER(nodeList.get());
     168                 :   PRUint32 count;
     169               0 :   nodeList->GetLength(&count);
     170               0 :   NS_ASSERTION(0!=count, "there are no text nodes in the document!");
     171               0 :   nsCOMPtr<nsIDOMNode>textNode;
     172               0 :   result = nodeList->Item(count-1, getter_AddRefs(textNode));
     173               0 :   TEST_RESULT(result);
     174               0 :   TEST_POINTER(textNode.get());
     175                 : 
     176                 :   // set the whole text node to bold
     177               0 :   printf("set the whole first text node to bold\n");
     178               0 :   nsCOMPtr<nsISelection>selection;
     179               0 :   result = mEditor->GetSelection(getter_AddRefs(selection));
     180               0 :   TEST_RESULT(result);
     181               0 :   TEST_POINTER(selection.get());
     182               0 :   nsCOMPtr<nsIDOMCharacterData>textData;
     183               0 :   textData = do_QueryInterface(textNode);
     184                 :   PRUint32 length;
     185               0 :   textData->GetLength(&length);
     186               0 :   selection->Collapse(textNode, 0);
     187               0 :   selection->Extend(textNode, length);
     188                 : 
     189               0 :   nsCOMPtr<nsIHTMLEditor> htmlEditor (do_QueryInterface(mTextEditor));
     190               0 :   NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
     191                 : 
     192               0 :   bool any = false;
     193               0 :   bool all = false;
     194               0 :   bool first=false;
     195                 : 
     196               0 :   const nsAFlatString& empty = EmptyString();
     197                 : 
     198               0 :   result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
     199               0 :   TEST_RESULT(result);
     200               0 :   NS_ASSERTION(false==first, "first should be false");
     201               0 :   NS_ASSERTION(false==any, "any should be false");
     202               0 :   NS_ASSERTION(false==all, "all should be false");
     203               0 :   result = htmlEditor->SetInlineProperty(nsEditProperty::b, empty, empty);
     204               0 :   TEST_RESULT(result);
     205               0 :   result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
     206               0 :   TEST_RESULT(result);
     207               0 :   NS_ASSERTION(true==first, "first should be true");
     208               0 :   NS_ASSERTION(true==any, "any should be true");
     209               0 :   NS_ASSERTION(true==all, "all should be true");
     210               0 :   mEditor->DebugDumpContent();
     211                 : 
     212                 :   // remove the bold we just set
     213               0 :   printf("set the whole first text node to not bold\n");
     214               0 :   result = htmlEditor->RemoveInlineProperty(nsEditProperty::b, empty);
     215               0 :   TEST_RESULT(result);
     216               0 :   result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
     217               0 :   TEST_RESULT(result);
     218               0 :   NS_ASSERTION(false==first, "first should be false");
     219               0 :   NS_ASSERTION(false==any, "any should be false");
     220               0 :   NS_ASSERTION(false==all, "all should be false");
     221               0 :   mEditor->DebugDumpContent();
     222                 : 
     223                 :   // set all but the first and last character to bold
     224               0 :   printf("set the first text node (1, length-1) to bold and italic, and (2, length-1) to underline.\n");
     225               0 :   selection->Collapse(textNode, 1);
     226               0 :   selection->Extend(textNode, length-1);
     227               0 :   result = htmlEditor->SetInlineProperty(nsEditProperty::b, empty, empty);
     228               0 :   TEST_RESULT(result);
     229               0 :   result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
     230               0 :   TEST_RESULT(result);
     231               0 :   NS_ASSERTION(true==first, "first should be true");
     232               0 :   NS_ASSERTION(true==any, "any should be true");
     233               0 :   NS_ASSERTION(true==all, "all should be true");
     234               0 :   mEditor->DebugDumpContent();
     235                 :   // make all that same text italic
     236               0 :   result = htmlEditor->SetInlineProperty(nsEditProperty::i, empty, empty);
     237               0 :   TEST_RESULT(result);
     238               0 :   result = htmlEditor->GetInlineProperty(nsEditProperty::i, empty, empty, &first, &any, &all);
     239               0 :   TEST_RESULT(result);
     240               0 :   NS_ASSERTION(true==first, "first should be true");
     241               0 :   NS_ASSERTION(true==any, "any should be true");
     242               0 :   NS_ASSERTION(true==all, "all should be true");
     243               0 :   result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
     244               0 :   TEST_RESULT(result);
     245               0 :   NS_ASSERTION(true==first, "first should be true");
     246               0 :   NS_ASSERTION(true==any, "any should be true");
     247               0 :   NS_ASSERTION(true==all, "all should be true");
     248               0 :   mEditor->DebugDumpContent();
     249                 : 
     250                 :   // make all the text underlined, except for the first 2 and last 2 characters
     251               0 :   result = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList));
     252               0 :   TEST_RESULT(result);
     253               0 :   TEST_POINTER(nodeList.get());
     254               0 :   nodeList->GetLength(&count);
     255               0 :   NS_ASSERTION(0!=count, "there are no text nodes in the document!");
     256               0 :   result = nodeList->Item(count-2, getter_AddRefs(textNode));
     257               0 :   TEST_RESULT(result);
     258               0 :   TEST_POINTER(textNode.get());
     259               0 :   textData = do_QueryInterface(textNode);
     260               0 :   textData->GetLength(&length);
     261               0 :   NS_ASSERTION(length==915, "wrong text node");
     262               0 :   selection->Collapse(textNode, 1);
     263               0 :   selection->Extend(textNode, length-2);
     264               0 :   result = htmlEditor->SetInlineProperty(nsEditProperty::u, empty, empty);
     265               0 :   TEST_RESULT(result);
     266               0 :   result = htmlEditor->GetInlineProperty(nsEditProperty::u, empty, empty, &first, &any, &all);
     267               0 :   TEST_RESULT(result);
     268               0 :   NS_ASSERTION(true==first, "first should be true");
     269               0 :   NS_ASSERTION(true==any, "any should be true");
     270               0 :   NS_ASSERTION(true==all, "all should be true");
     271               0 :   mEditor->DebugDumpContent();
     272                 : 
     273               0 :   return result;
     274                 : }
     275                 : 
     276                 : 
     277                 : 
     278                 : #endif
     279                 : 
     280                 : 

Generated by: LCOV version 1.7