Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / embedding / qa / testembed / Selection.cpp
blob1e385eb491db9b8b1fa886a8dcf5f671ba6d2784
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
13 * License.
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.
22 * Contributor(s):
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 ***** */
39 // File Overview....
41 // Test cases for the nsISelection Interface
43 #include "stdafx.h"
44 #include "QaUtils.h"
45 #include <stdio.h>
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.
64 switch(nMenuID)
66 case ID_INTERFACES_NSISELECTION_RUNALLTESTS :
67 RunAllTests();
68 break ;
69 case ID_INTERFACES_NSISELECTION_GETANCHORNODE :
70 GetAnchorNode();
71 break ;
72 case ID_INTERFACES_NSISELECTION_GETANCHOROFFSET :
73 GetAnchorOffset();
74 break ;
75 case ID_INTERFACES_NSISELECTION_GETFOCUSNODE :
76 GetFocusNode() ;
77 break ;
78 case ID_INTERFACES_NSISELECTION_GETFOCUSOFFSET :
79 GetFocusOffset() ;
80 break ;
81 case ID_INTERFACES_NSISELECTION_GETISCOLLAPSED :
82 GetIsCollapsed() ;
83 break ;
84 case ID_INTERFACES_NSISELECTION_GETRANGECOUNT :
85 GetRangecount() ;
86 break ;
87 case ID_INTERFACES_NSISELECTION_GETRANGEAT :
88 GetRangeAt() ;
89 break ;
90 case ID_INTERFACES_NSISELECTION_COLLAPSE :
91 Collapse() ;
92 break ;
93 case ID_INTERFACES_NSISELECTION_EXTEND :
94 Extend() ;
95 break ;
96 case ID_INTERFACES_NSISELECTION_COLLAPSETOSTART :
97 CollapseToStart() ;
98 break ;
99 case ID_INTERFACES_NSISELECTION_COLLAPSETOEND :
100 CollapseToEnd() ;
101 break ;
102 case ID_INTERFACES_NSISELECTION_CONTAINSNODE :
103 ContainsNode() ;
104 break ;
105 case ID_INTERFACES_NSISELECTION_SELECTALLCHILDREN :
106 SelectAllChildren();
107 break ;
108 case ID_INTERFACES_NSISELECTION_ADDRANGE :
109 Addrange();
110 break ;
111 case ID_INTERFACES_NSISELECTION_REMOVERANGE :
112 RemoveRange();
113 break ;
114 case ID_INTERFACES_NSISELECTION_REMOVEALLRANGES :
115 RemoveAllRanges();
116 break ;
117 case ID_INTERFACES_NSISELECTION_DELETEFROMDOCUMENT :
118 DeleteFromDocument();
119 break ;
120 case ID_INTERFACES_NSISELECTION_SELECTIONLANGUAGECHANGE :
121 SelectionLanguageChange();
122 break ;
123 case ID_INTERFACES_NSISELECTION_TOSTRING :
124 ToString();
125 break ;
130 void CSelection::RunAllTests()
133 PRInt32 ancoroffset = 0;
134 PRInt32 focusoffset = 0;
135 PRInt32 rangecount = 0;
136 PRInt32 index = 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);
147 if (!oNsSelection)
148 return ;
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()");
179 if(bisCollapsed)
180 RvTestResultDlg(rv, "------> Returned value for IsCollapsed is true");
181 else
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() ");
195 if (!oNsDOMRange)
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() ");
223 if(bContains)
224 RvTestResultDlg(rv, "------> Returned value for ContainsNode is true");
225 else
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();
273 if (!oSelection)
274 return ;
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;
287 //char szstr[10] ;
289 nsCOMPtr<nsISelection> oSelection ;
291 oSelection = GetSelectionObject();
292 if (!oSelection)
293 return ;
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();
307 if (!oSelection)
308 return ;
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();
322 if (!oSelection)
323 return ;
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();
336 if (!oSelection)
337 return ;
339 rv = oSelection->GetIsCollapsed(&bisCollapsed);
340 RvTestResult(rv, "nsISelection::GetIsCollapsed()",0);
341 if(bisCollapsed)
342 RvTestResult(rv, "------> Returned value for IsCollapsed is true",0);
343 else
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();
353 if (!oSelection)
354 return ;
355 rv = oSelection->GetRangeCount(&rangecount);
356 RvTestResult(rv, "nsISelection::GetRangeCount()",0);
357 char szstr[10] ;
358 ltoa(rangecount, szstr,10);
359 AfxMessageBox(szstr);
363 void CSelection::GetRangeAt()
365 PRInt32 index = 0;
366 nsCOMPtr<nsIDOMRange> oNsDOMRange ;
368 nsCOMPtr<nsISelection> oSelection ;
369 oSelection = GetSelectionObject();
370 if (!oSelection)
371 return ;
372 rv = oSelection->GetRangeAt(index,getter_AddRefs(oNsDOMRange));
373 RvTestResult(rv, "nsISelection::GetRangeAt() ",0);
374 if (!oNsDOMRange)
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();
387 if (!oSelection)
388 return ;
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();
406 if (!oSelection)
407 return ;
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();
425 if (!oSelection)
426 return ;
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();
448 if (!oSelection)
449 return ;
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);
459 if(bContains)
460 RvTestResult(rv, "------> Returned value for ContainsNode is true",0);
461 else
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();
471 if (!oSelection)
472 return ;
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()
487 PRInt32 index= 0;
489 nsCOMPtr<nsIDOMRange> oNsDOMRange ;
490 nsCOMPtr<nsISelection> oSelection ;
492 oSelection = GetSelectionObject();
493 if (!oSelection)
494 return ;
496 rv = oSelection->GetRangeAt(index,getter_AddRefs(oNsDOMRange));
497 RvTestResult(rv, "nsISelection::GetRangeAt() ",0);
498 if (!oNsDOMRange)
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()
508 PRInt32 index= 0;
510 nsCOMPtr<nsIDOMRange> oNsDOMRange ;
511 nsCOMPtr<nsISelection> oSelection ;
513 oSelection = GetSelectionObject();
514 if (!oSelection)
515 return ;
517 rv = oSelection->GetRangeAt(index,getter_AddRefs(oNsDOMRange));
518 RvTestResult(rv, "nsISelection::GetRangeAt() ",0);
519 if (!oNsDOMRange)
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();
530 if (!oSelection)
531 return ;
532 rv = oSelection->RemoveAllRanges();
533 RvTestResult(rv, "nsISelection::RemoveAllRanges() ",0);
537 void CSelection::DeleteFromDocument()
539 nsCOMPtr<nsISelection> oSelection ;
540 oSelection = GetSelectionObject();
541 if (!oSelection)
542 return ;
543 rv = oSelection->DeleteFromDocument();
544 RvTestResult(rv, "nsISelection::DeleteFromDocument() ",0);
548 void CSelection::SelectionLanguageChange()
550 nsCOMPtr<nsISelection> oSelection ;
551 oSelection = GetSelectionObject();
552 if (!oSelection)
553 return ;
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();
566 if (!oSelection)
567 return ;
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));
582 if(bShowDialog)
583 RvTestResultDlg(rv, "nsIWebBrowser::GetContentDOMWindow()' rv test",true);
584 else
585 RvTestResult(rv, "nsIWebBrowser::GetContentDOMWindow()' rv test", 0);
586 if (!oDomWindow)
588 if(bShowDialog)
589 RvTestResultDlg(rv, "------>Cannot get the nsIDOMNode Object");
590 else
591 RvTestResult(rv, "------>Cannot get the nsIDOMNode Object", 0);
592 return NULL ;
595 if(bShowDialog)
596 RvTestResultDlg(rv, "------>nsIDOMWindow is required in order to get the nsISelection interface");
597 else
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));
603 if(bShowDialog)
604 RvTestResultDlg(rv, "nsIDOMWindow::GetSelection()");
605 else
606 RvTestResult(rv, "nsIDOMWindow::GetSelection()", 0);
608 if (!oNsSelection)
610 if(bShowDialog)
611 RvTestResultDlg(rv, "------>Cannot get the Selection Object");
612 else
613 RvTestResult(rv, "Cannot get the Selection Object", 0);
615 return NULL ;
618 return oNsSelection ;