1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is mozilla.org code.
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.
23 * Ashish Bhatt <ashishbhatt@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
41 // Test cases for the nsISelection Interface
46 #include "Selection.h"
48 CSelection::CSelection(nsIWebBrowser
* mWebBrowser
)
50 qaWebBrowser
= mWebBrowser
;
54 CSelection::~CSelection()
59 void CSelection::OnStartTests(UINT nMenuID
)
61 // Calls all or indivdual test cases on the basis of the
62 // option selected from menu.
66 case ID_INTERFACES_NSISELECTION_RUNALLTESTS
:
69 case ID_INTERFACES_NSISELECTION_GETANCHORNODE
:
72 case ID_INTERFACES_NSISELECTION_GETANCHOROFFSET
:
75 case ID_INTERFACES_NSISELECTION_GETFOCUSNODE
:
78 case ID_INTERFACES_NSISELECTION_GETFOCUSOFFSET
:
81 case ID_INTERFACES_NSISELECTION_GETISCOLLAPSED
:
84 case ID_INTERFACES_NSISELECTION_GETRANGECOUNT
:
87 case ID_INTERFACES_NSISELECTION_GETRANGEAT
:
90 case ID_INTERFACES_NSISELECTION_COLLAPSE
:
93 case ID_INTERFACES_NSISELECTION_EXTEND
:
96 case ID_INTERFACES_NSISELECTION_COLLAPSETOSTART
:
99 case ID_INTERFACES_NSISELECTION_COLLAPSETOEND
:
102 case ID_INTERFACES_NSISELECTION_CONTAINSNODE
:
105 case ID_INTERFACES_NSISELECTION_SELECTALLCHILDREN
:
108 case ID_INTERFACES_NSISELECTION_ADDRANGE
:
111 case ID_INTERFACES_NSISELECTION_REMOVERANGE
:
114 case ID_INTERFACES_NSISELECTION_REMOVEALLRANGES
:
117 case ID_INTERFACES_NSISELECTION_DELETEFROMDOCUMENT
:
118 DeleteFromDocument();
120 case ID_INTERFACES_NSISELECTION_SELECTIONLANGUAGECHANGE
:
121 SelectionLanguageChange();
123 case ID_INTERFACES_NSISELECTION_TOSTRING
:
130 void CSelection::RunAllTests()
133 PRInt32 ancoroffset
= 0;
134 PRInt32 focusoffset
= 0;
135 PRInt32 rangecount
= 0;
137 PRBool bisCollapsed
= false;
138 PRBool bContains
= false;
139 nsXPIDLString szText
;
141 nsCOMPtr
<nsISelection
> oNsSelection
;
142 nsCOMPtr
<nsIDOMNode
> oNsDOMAnchorNode
;
143 nsCOMPtr
<nsIDOMNode
> oNsDOMFocusNode
;
144 nsCOMPtr
<nsIDOMRange
> oNsDOMRange
;
146 oNsSelection
= GetSelectionObject(true);
150 //---------------- Test case for AnchorNode Attribute
152 rv
= oNsSelection
->GetAnchorNode(getter_AddRefs(oNsDOMAnchorNode
));
153 RvTestResultDlg(rv
, "nsISelection::GetAnchorNode() ");
154 if (!oNsDOMAnchorNode
)
155 RvTestResultDlg(rv
, "------>No Selection made to get the AnchorNode object");
157 //---------------- Test case for AnchorOffset Attribute
159 rv
= oNsSelection
->GetAnchorOffset(&ancoroffset
);
160 RvTestResultDlg(rv
, "nsISelection::GetAnchorOffset()");
163 //---------------- Test case for FocusNode Attribute
165 rv
= oNsSelection
->GetFocusNode(getter_AddRefs(oNsDOMFocusNode
));
166 RvTestResultDlg(rv
, "nsISelection::GetFocusNode() ");
167 if (!oNsDOMFocusNode
)
168 RvTestResultDlg(rv
, "------>No Selection made to get the FocusNode object");
170 //---------------- Test case for FocusOffset Attribute
172 rv
= oNsSelection
->GetFocusOffset(&focusoffset
);
173 RvTestResultDlg(rv
, "nsISelection::GetFocusOffset()");
175 //---------------- Test case for isCollapsed Attribute
177 rv
= oNsSelection
->GetIsCollapsed(&bisCollapsed
);
178 RvTestResultDlg(rv
, "nsISelection::GetIsCollapsed()");
180 RvTestResultDlg(rv
, "------> Returned value for IsCollapsed is true");
182 RvTestResultDlg(rv
, "------> Returned value for IsCollapsed is false");
185 //---------------- Test case for isCollapsed Attribute
187 rv
= oNsSelection
->GetRangeCount(&rangecount
);
188 RvTestResultDlg(rv
, "nsISelection::GetRangeCount()");
191 //---------------- Test case for GetRangeAt function
193 rv
= oNsSelection
->GetRangeAt(index
,getter_AddRefs(oNsDOMRange
));
194 RvTestResultDlg(rv
, "nsISelection::GetRangeAt() ");
196 RvTestResultDlg(rv
, "------>Not able to get nsIDOMRange object");
199 //---------------- Test case for Collapse function
201 rv
= oNsSelection
->Collapse(oNsDOMFocusNode
,1) ;
202 RvTestResultDlg(rv
, "nsISelection::Collapse() ");
204 //---------------- Test case for Extend function
206 rv
= oNsSelection
->Extend(oNsDOMFocusNode
,10) ;
207 RvTestResultDlg(rv
, "nsISelection::Extend() ");
209 //---------------- Test case for CollapseToStart function
211 rv
= oNsSelection
->CollapseToStart();
212 RvTestResultDlg(rv
, "nsISelection::CollapseToStart() ");
214 //---------------- Test case for CollapseToEnd function
216 rv
= oNsSelection
->CollapseToEnd();
217 RvTestResultDlg(rv
, "nsISelection::CollapseToEnd() ");
219 //---------------- Test case for ContainsNode function
221 rv
= oNsSelection
->ContainsNode(oNsDOMFocusNode
,PR_TRUE
, &bContains
);
222 RvTestResultDlg(rv
, "nsISelection::ContainsNode() ");
224 RvTestResultDlg(rv
, "------> Returned value for ContainsNode is true");
226 RvTestResultDlg(rv
, "------> Returned value for ContainsNode is false");
229 //---------------- Test case for SelectAllChildren function
231 rv
= oNsSelection
->SelectAllChildren(oNsDOMFocusNode
);
232 RvTestResultDlg(rv
, "nsISelection::SelectAllChildren() ");
234 //---------------- Test case for Addrange function
236 rv
= oNsSelection
->AddRange(oNsDOMRange
);
237 RvTestResultDlg(rv
, "nsISelection::Addrange() ");
239 //---------------- Test case for RemoveRange function
241 rv
= oNsSelection
->RemoveRange(oNsDOMRange
);
242 RvTestResultDlg(rv
, "nsISelection::RemoveRange() ");
244 //---------------- Test case for RemoveAllRanges function
246 rv
= oNsSelection
->RemoveAllRanges();
247 RvTestResultDlg(rv
, "nsISelection::RemoveAllRanges() ");
249 //---------------- Test case for DeleteFromDocument function
251 rv
= oNsSelection
->DeleteFromDocument();
252 RvTestResultDlg(rv
, "nsISelection::DeleteFromDocument() ");
254 //---------------- Test case for SelectionLanguageChange function
256 rv
= oNsSelection
->SelectionLanguageChange(PR_FALSE
);
257 RvTestResultDlg(rv
, "nsISelection::SelectionLanguageChange() ");
259 //---------------- Test case for ToString function
261 rv
= oNsSelection
->ToString(getter_Copies(szText
));
262 RvTestResultDlg(rv
, "nsISelection::ToString() ");
266 void CSelection::GetAnchorNode()
269 nsCOMPtr
<nsIDOMNode
> oNsDOMAnchorNode
;
270 nsCOMPtr
<nsISelection
> oSelection
;
272 oSelection
= GetSelectionObject();
276 rv
= oSelection
->GetAnchorNode(getter_AddRefs(oNsDOMAnchorNode
));
277 RvTestResult(rv
, "nsISelection::GetAnchorNode() ",0);
278 if (!oNsDOMAnchorNode
)
279 RvTestResult(rv
, "------>No Selection made to get the AnchorNode object",0);
283 void CSelection::GetAnchorOffset()
286 PRInt32 ancoroffset
= 0;
289 nsCOMPtr
<nsISelection
> oSelection
;
291 oSelection
= GetSelectionObject();
295 rv
= oSelection
->GetAnchorOffset(&ancoroffset
);
296 RvTestResult(rv
, "nsISelection::GetAnchorOffset()",0);
297 //ltoa(ancoroffset, szstr,10);
298 //AfxMessageBox(szstr);
301 void CSelection::GetFocusNode()
303 nsCOMPtr
<nsIDOMNode
> oNsDOMFocusNode
;
304 nsCOMPtr
<nsISelection
> oSelection
;
306 oSelection
= GetSelectionObject();
310 rv
= oSelection
->GetFocusNode(getter_AddRefs(oNsDOMFocusNode
));
311 RvTestResult(rv
, "nsISelection::GetFocusNode() ",0);
312 if (!oNsDOMFocusNode
)
313 RvTestResult(rv
, "------>No Selection made to get the FocusNode object",0);
316 void CSelection::GetFocusOffset()
318 PRInt32 focusoffset
= 0;
319 nsCOMPtr
<nsISelection
> oSelection
;
321 oSelection
= GetSelectionObject();
325 rv
= oSelection
->GetFocusOffset(&focusoffset
);
326 RvTestResult(rv
, "nsISelection::GetFocusOffset()",0);
330 void CSelection::GetIsCollapsed()
332 PRBool bisCollapsed
= false;
334 nsCOMPtr
<nsISelection
> oSelection
;
335 oSelection
= GetSelectionObject();
339 rv
= oSelection
->GetIsCollapsed(&bisCollapsed
);
340 RvTestResult(rv
, "nsISelection::GetIsCollapsed()",0);
342 RvTestResult(rv
, "------> Returned value for IsCollapsed is true",0);
344 RvTestResult(rv
, "------> Returned value for IsCollapsed is false",0);
347 void CSelection::GetRangecount()
349 PRInt32 rangecount
= 0;
350 nsCOMPtr
<nsISelection
> oSelection
;
352 oSelection
= GetSelectionObject();
355 rv
= oSelection
->GetRangeCount(&rangecount
);
356 RvTestResult(rv
, "nsISelection::GetRangeCount()",0);
358 ltoa(rangecount
, szstr
,10);
359 AfxMessageBox(szstr
);
363 void CSelection::GetRangeAt()
366 nsCOMPtr
<nsIDOMRange
> oNsDOMRange
;
368 nsCOMPtr
<nsISelection
> oSelection
;
369 oSelection
= GetSelectionObject();
372 rv
= oSelection
->GetRangeAt(index
,getter_AddRefs(oNsDOMRange
));
373 RvTestResult(rv
, "nsISelection::GetRangeAt() ",0);
375 RvTestResult(rv
, "------>Not able to get nsIDOMRange object",0);
380 void CSelection::Collapse()
383 nsCOMPtr
<nsIDOMNode
> oNsDOMFocusNode
;
384 nsCOMPtr
<nsISelection
> oSelection
;
386 oSelection
= GetSelectionObject();
390 rv
= oSelection
->GetFocusNode(getter_AddRefs(oNsDOMFocusNode
));
391 RvTestResult(rv
, "nsISelection::GetFocusNode() ",0);
392 if (!oNsDOMFocusNode
)
393 RvTestResult(rv
, "------>No Selection made to get the FocusNode object",0);
395 rv
= oSelection
->Collapse(oNsDOMFocusNode
,1) ;
396 RvTestResult(rv
, "nsISelection::Collapse() ",0);
400 void CSelection::Extend()
402 nsCOMPtr
<nsIDOMNode
> oNsDOMFocusNode
;
403 nsCOMPtr
<nsISelection
> oSelection
;
405 oSelection
= GetSelectionObject();
409 rv
= oSelection
->GetFocusNode(getter_AddRefs(oNsDOMFocusNode
));
410 RvTestResult(rv
, "nsISelection::GetFocusNode() ",0);
411 if (!oNsDOMFocusNode
)
412 RvTestResult(rv
, "------>No Selection made to get the FocusNode object",0);
415 rv
= oSelection
->Extend(oNsDOMFocusNode
,10) ;
416 RvTestResult(rv
, "nsISelection::Extend() ",0);
421 void CSelection::CollapseToStart()
423 nsCOMPtr
<nsISelection
> oSelection
;
424 oSelection
= GetSelectionObject();
427 rv
= oSelection
->CollapseToStart();
428 RvTestResult(rv
, "nsISelection::CollapseToStart() ",0);
432 void CSelection::CollapseToEnd()
434 nsCOMPtr
<nsISelection
> oSelection
;
435 oSelection
= GetSelectionObject();
436 rv
= oSelection
->CollapseToEnd();
437 RvTestResult(rv
, "nsISelection::CollapseToEnd() ",0);
440 void CSelection::ContainsNode()
442 PRBool bContains
= false;
444 nsCOMPtr
<nsIDOMNode
> oNsDOMFocusNode
;
445 nsCOMPtr
<nsISelection
> oSelection
;
447 oSelection
= GetSelectionObject();
451 rv
= oSelection
->GetFocusNode(getter_AddRefs(oNsDOMFocusNode
));
452 RvTestResult(rv
, "nsISelection::GetFocusNode() ",0);
453 if (!oNsDOMFocusNode
)
454 RvTestResult(rv
, "------>No Selection made to get the FocusNode object",0);
457 rv
= oSelection
->ContainsNode(oNsDOMFocusNode
,PR_TRUE
, &bContains
);
458 RvTestResult(rv
, "nsISelection::ContainsNode() ",0);
460 RvTestResult(rv
, "------> Returned value for ContainsNode is true",0);
462 RvTestResult(rv
, "------> Returned value for ContainsNode is false",0);
465 void CSelection::SelectAllChildren()
467 nsCOMPtr
<nsIDOMNode
> oNsDOMFocusNode
;
468 nsCOMPtr
<nsISelection
> oSelection
;
470 oSelection
= GetSelectionObject();
474 rv
= oSelection
->GetFocusNode(getter_AddRefs(oNsDOMFocusNode
));
475 RvTestResult(rv
, "nsISelection::GetFocusNode() ",0);
476 if (!oNsDOMFocusNode
)
477 RvTestResult(rv
, "------>No Selection made to get the FocusNode object",0);
480 rv
= oSelection
->SelectAllChildren(oNsDOMFocusNode
);
481 RvTestResult(rv
, "nsISelection::SelectAllChildren() ",0);
485 void CSelection::Addrange()
489 nsCOMPtr
<nsIDOMRange
> oNsDOMRange
;
490 nsCOMPtr
<nsISelection
> oSelection
;
492 oSelection
= GetSelectionObject();
496 rv
= oSelection
->GetRangeAt(index
,getter_AddRefs(oNsDOMRange
));
497 RvTestResult(rv
, "nsISelection::GetRangeAt() ",0);
499 RvTestResult(rv
, "------>Not able to get nsIDOMRange object",0);
501 rv
= oSelection
->AddRange(oNsDOMRange
);
502 RvTestResult(rv
, "nsISelection::Addrange() ",0);
506 void CSelection::RemoveRange()
510 nsCOMPtr
<nsIDOMRange
> oNsDOMRange
;
511 nsCOMPtr
<nsISelection
> oSelection
;
513 oSelection
= GetSelectionObject();
517 rv
= oSelection
->GetRangeAt(index
,getter_AddRefs(oNsDOMRange
));
518 RvTestResult(rv
, "nsISelection::GetRangeAt() ",0);
520 RvTestResult(rv
, "------>Not able to get nsIDOMRange object",0);
522 rv
= oSelection
->RemoveRange(oNsDOMRange
);
523 RvTestResult(rv
, "nsISelection::RemoveRange() ",0);
526 void CSelection::RemoveAllRanges()
528 nsCOMPtr
<nsISelection
> oSelection
;
529 oSelection
= GetSelectionObject();
532 rv
= oSelection
->RemoveAllRanges();
533 RvTestResult(rv
, "nsISelection::RemoveAllRanges() ",0);
537 void CSelection::DeleteFromDocument()
539 nsCOMPtr
<nsISelection
> oSelection
;
540 oSelection
= GetSelectionObject();
543 rv
= oSelection
->DeleteFromDocument();
544 RvTestResult(rv
, "nsISelection::DeleteFromDocument() ",0);
548 void CSelection::SelectionLanguageChange()
550 nsCOMPtr
<nsISelection
> oSelection
;
551 oSelection
= GetSelectionObject();
555 rv
= oSelection
->SelectionLanguageChange(PR_TRUE
);
556 RvTestResult(rv
, "nsISelection::SelectionLanguageChange() ",0);
560 void CSelection::ToString()
562 nsXPIDLString szText
;
563 nsCOMPtr
<nsISelection
> oSelection
;
565 oSelection
= GetSelectionObject();
568 rv
= oSelection
->ToString(getter_Copies(szText
));
569 RvTestResult(rv
, "nsISelection::ToString() ",0);
573 nsISelection
* CSelection::GetSelectionObject(BOOL bShowDialog
)
575 nsCOMPtr
<nsISelection
> oNsSelection
;
577 nsCOMPtr
<nsIDOMWindow
> oDomWindow
;
579 // Get the nsIDOMWindow interface in order to get nsISelection
580 rv
= qaWebBrowser
->GetContentDOMWindow(getter_AddRefs(oDomWindow
));
583 RvTestResultDlg(rv
, "nsIWebBrowser::GetContentDOMWindow()' rv test",true);
585 RvTestResult(rv
, "nsIWebBrowser::GetContentDOMWindow()' rv test", 0);
589 RvTestResultDlg(rv
, "------>Cannot get the nsIDOMNode Object");
591 RvTestResult(rv
, "------>Cannot get the nsIDOMNode Object", 0);
596 RvTestResultDlg(rv
, "------>nsIDOMWindow is required in order to get the nsISelection interface");
598 RvTestResult(rv
, "nsIDOMWindow is required in order to get the nsISelection interface", 0);
600 // Now get the nsISelection interface
601 rv
= oDomWindow
->GetSelection(getter_AddRefs(oNsSelection
));
604 RvTestResultDlg(rv
, "nsIDOMWindow::GetSelection()");
606 RvTestResult(rv
, "nsIDOMWindow::GetSelection()", 0);
611 RvTestResultDlg(rv
, "------>Cannot get the Selection Object");
613 RvTestResult(rv
, "Cannot get the Selection Object", 0);
618 return oNsSelection
;