Back to index

lightning-sunbird  0.9+nobinonly
TextEditorTest.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++ tab-width: 2 indent-tabs-mode: nil c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifdef DEBUG
00039 
00040 #include <stdio.h>
00041 
00042 #include "nsIEditor.h"
00043 #include "nsIHTMLEditor.h"
00044 #include "TextEditorTest.h"
00045 #include "nsISelection.h"
00046 #include "nsIDOMCharacterData.h"
00047 #include "nsIDOMDocument.h"
00048 #include "nsIDOMNode.h"
00049 #include "nsIDOMNodeList.h"
00050 #include "nsEditProperty.h"
00051 #include "nsString.h"
00052 #include "nsReadableUtils.h"
00053 
00054 #define TEST_RESULT(r) { if (NS_FAILED(r)) {printf("FAILURE result=%X\n", r); return r; } }
00055 #define TEST_POINTER(p) { if (!p) {printf("FAILURE null pointer\n"); return NS_ERROR_NULL_POINTER; } }
00056 
00057 TextEditorTest::TextEditorTest()
00058 {
00059   printf("constructed a TextEditorTest\n");
00060 }
00061 
00062 TextEditorTest::~TextEditorTest()
00063 {
00064   printf("destroyed a TextEditorTest\n");
00065 }
00066 
00067 void TextEditorTest::Run(nsIEditor *aEditor, PRInt32 *outNumTests, PRInt32 *outNumTestsFailed)
00068 {
00069   if (!aEditor) return;
00070   mTextEditor = do_QueryInterface(aEditor);
00071   mEditor = do_QueryInterface(aEditor);
00072   RunUnitTest(outNumTests, outNumTestsFailed);
00073 }
00074 
00075 nsresult TextEditorTest::RunUnitTest(PRInt32 *outNumTests, PRInt32 *outNumTestsFailed)
00076 {
00077   nsresult result;
00078   
00079   if (!outNumTests || !outNumTestsFailed)
00080     return NS_ERROR_NULL_POINTER;
00081   
00082   *outNumTests = 0;
00083   *outNumTestsFailed = 0;
00084   
00085   result = InitDoc();
00086   TEST_RESULT(result);
00087   // shouldn't we just bail on error here?
00088   
00089   // insert some simple text
00090   result = mTextEditor->InsertText(NS_LITERAL_STRING("1234567890abcdefghij1234567890"));
00091   TEST_RESULT(result);
00092   (*outNumTests)++;
00093   (*outNumTestsFailed) += (NS_FAILED(result) != NS_OK);
00094   
00095   // insert some more text
00096   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"));
00097   TEST_RESULT(result);
00098   (*outNumTests)++;
00099   (*outNumTestsFailed) += (NS_FAILED(result) != NS_OK);
00100 
00101   result = TestInsertBreak();
00102   TEST_RESULT(result);
00103   (*outNumTests)++;
00104   (*outNumTestsFailed) += (NS_FAILED(result) != NS_OK);
00105 
00106   result = TestTextProperties();
00107   TEST_RESULT(result);
00108   (*outNumTests)++;
00109   (*outNumTestsFailed) += (NS_FAILED(result) != NS_OK);
00110 
00111   // get us back to the original document
00112   result = mEditor->Undo(12);
00113   TEST_RESULT(result);
00114 
00115   return result;
00116 }
00117 
00118 nsresult TextEditorTest::InitDoc()
00119 {
00120   nsresult result = mEditor->SelectAll();
00121   TEST_RESULT(result);
00122   result = mEditor->DeleteSelection(nsIEditor::eNext);
00123   TEST_RESULT(result);
00124   return result;
00125 }
00126 
00127 nsresult TextEditorTest::TestInsertBreak()
00128 {
00129   nsCOMPtr<nsISelection>selection;
00130   nsresult result = mEditor->GetSelection(getter_AddRefs(selection));
00131   TEST_RESULT(result);
00132   TEST_POINTER(selection.get());
00133   nsCOMPtr<nsIDOMNode>anchor;
00134   result = selection->GetAnchorNode(getter_AddRefs(anchor));
00135   TEST_RESULT(result);
00136   TEST_POINTER(anchor.get());
00137   selection->Collapse(anchor, 0);
00138   // insert one break
00139   printf("inserting a break\n");
00140   result = mTextEditor->InsertLineBreak();
00141   TEST_RESULT(result);
00142   mEditor->DebugDumpContent();
00143 
00144   // insert a second break adjacent to the first
00145   printf("inserting a second break\n");
00146   result = mTextEditor->InsertLineBreak();
00147   TEST_RESULT(result);
00148   mEditor->DebugDumpContent();
00149     
00150   return result;
00151 }
00152 
00153 nsresult TextEditorTest::TestTextProperties()
00154 {
00155   nsCOMPtr<nsIDOMDocument>doc;
00156   nsresult result = mEditor->GetDocument(getter_AddRefs(doc));
00157   TEST_RESULT(result);
00158   TEST_POINTER(doc.get());
00159   nsCOMPtr<nsIDOMNodeList>nodeList;
00160   // XXX This is broken, text nodes are not elements.
00161   nsAutoString textTag(NS_LITERAL_STRING("#text"));
00162   result = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList));
00163   TEST_RESULT(result);
00164   TEST_POINTER(nodeList.get());
00165   PRUint32 count;
00166   nodeList->GetLength(&count);
00167   NS_ASSERTION(0!=count, "there are no text nodes in the document!");
00168   nsCOMPtr<nsIDOMNode>textNode;
00169   result = nodeList->Item(count-1, getter_AddRefs(textNode));
00170   TEST_RESULT(result);
00171   TEST_POINTER(textNode.get());
00172 
00173   // set the whole text node to bold
00174   printf("set the whole first text node to bold\n");
00175   nsCOMPtr<nsISelection>selection;
00176   result = mEditor->GetSelection(getter_AddRefs(selection));
00177   TEST_RESULT(result);
00178   TEST_POINTER(selection.get());
00179   nsCOMPtr<nsIDOMCharacterData>textData;
00180   textData = do_QueryInterface(textNode);
00181   PRUint32 length;
00182   textData->GetLength(&length);
00183   selection->Collapse(textNode, 0);
00184   selection->Extend(textNode, length);
00185 
00186   nsCOMPtr<nsIHTMLEditor> htmlEditor (do_QueryInterface(mTextEditor));
00187   if (!htmlEditor)
00188     return NS_ERROR_FAILURE;
00189 
00190   PRBool any = PR_FALSE;
00191   PRBool all = PR_FALSE;
00192   PRBool first=PR_FALSE;
00193 
00194   const nsAFlatString& empty = EmptyString();
00195 
00196   result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
00197   TEST_RESULT(result);
00198   NS_ASSERTION(PR_FALSE==first, "first should be false");
00199   NS_ASSERTION(PR_FALSE==any, "any should be false");
00200   NS_ASSERTION(PR_FALSE==all, "all should be false");
00201   result = htmlEditor->SetInlineProperty(nsEditProperty::b, empty, empty);
00202   TEST_RESULT(result);
00203   result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
00204   TEST_RESULT(result);
00205   NS_ASSERTION(PR_TRUE==first, "first should be true");
00206   NS_ASSERTION(PR_TRUE==any, "any should be true");
00207   NS_ASSERTION(PR_TRUE==all, "all should be true");
00208   mEditor->DebugDumpContent();
00209 
00210   // remove the bold we just set
00211   printf("set the whole first text node to not bold\n");
00212   result = htmlEditor->RemoveInlineProperty(nsEditProperty::b, empty);
00213   TEST_RESULT(result);
00214   result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
00215   TEST_RESULT(result);
00216   NS_ASSERTION(PR_FALSE==first, "first should be false");
00217   NS_ASSERTION(PR_FALSE==any, "any should be false");
00218   NS_ASSERTION(PR_FALSE==all, "all should be false");
00219   mEditor->DebugDumpContent();
00220 
00221   // set all but the first and last character to bold
00222   printf("set the first text node (1, length-1) to bold and italic, and (2, length-1) to underline.\n");
00223   selection->Collapse(textNode, 1);
00224   selection->Extend(textNode, length-1);
00225   result = htmlEditor->SetInlineProperty(nsEditProperty::b, empty, empty);
00226   TEST_RESULT(result);
00227   result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
00228   TEST_RESULT(result);
00229   NS_ASSERTION(PR_TRUE==first, "first should be true");
00230   NS_ASSERTION(PR_TRUE==any, "any should be true");
00231   NS_ASSERTION(PR_TRUE==all, "all should be true");
00232   mEditor->DebugDumpContent();
00233   // make all that same text italic
00234   result = htmlEditor->SetInlineProperty(nsEditProperty::i, empty, empty);
00235   TEST_RESULT(result);
00236   result = htmlEditor->GetInlineProperty(nsEditProperty::i, empty, empty, &first, &any, &all);
00237   TEST_RESULT(result);
00238   NS_ASSERTION(PR_TRUE==first, "first should be true");
00239   NS_ASSERTION(PR_TRUE==any, "any should be true");
00240   NS_ASSERTION(PR_TRUE==all, "all should be true");
00241   result = htmlEditor->GetInlineProperty(nsEditProperty::b, empty, empty, &first, &any, &all);
00242   TEST_RESULT(result);
00243   NS_ASSERTION(PR_TRUE==first, "first should be true");
00244   NS_ASSERTION(PR_TRUE==any, "any should be true");
00245   NS_ASSERTION(PR_TRUE==all, "all should be true");
00246   mEditor->DebugDumpContent();
00247 
00248   // make all the text underlined, except for the first 2 and last 2 characters
00249   result = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList));
00250   TEST_RESULT(result);
00251   TEST_POINTER(nodeList.get());
00252   nodeList->GetLength(&count);
00253   NS_ASSERTION(0!=count, "there are no text nodes in the document!");
00254   result = nodeList->Item(count-2, getter_AddRefs(textNode));
00255   TEST_RESULT(result);
00256   TEST_POINTER(textNode.get());
00257   textData = do_QueryInterface(textNode);
00258   textData->GetLength(&length);
00259   NS_ASSERTION(length==915, "wrong text node");
00260   selection->Collapse(textNode, 1);
00261   selection->Extend(textNode, length-2);
00262   result = htmlEditor->SetInlineProperty(nsEditProperty::u, empty, empty);
00263   TEST_RESULT(result);
00264   result = htmlEditor->GetInlineProperty(nsEditProperty::u, empty, empty, &first, &any, &all);
00265   TEST_RESULT(result);
00266   NS_ASSERTION(PR_TRUE==first, "first should be true");
00267   NS_ASSERTION(PR_TRUE==any, "any should be true");
00268   NS_ASSERTION(PR_TRUE==all, "all should be true");
00269   mEditor->DebugDumpContent();
00270 
00271   return result;
00272 }
00273 
00274 
00275 
00276 #endif
00277 
00278