Back to index

lightning-sunbird  0.9+nobinonly
nsICommandMgr.cpp
Go to the documentation of this file.
00001 // nsICommandMgr.cpp : implementation file
00002 //
00003 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00004 /* ***** BEGIN LICENSE BLOCK *****
00005  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006  *
00007  * The contents of this file are subject to the Mozilla Public License Version
00008  * 1.1 (the "License"); you may not use this file except in compliance with
00009  * the License. You may obtain a copy of the License at
00010  * http://www.mozilla.org/MPL/
00011  *
00012  * Software distributed under the License is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00014  * for the specific language governing rights and limitations under the
00015  * License.
00016  *
00017  * The Original Code is mozilla.org code.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   David Epstein <depstein@netscape.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 // File Overview....
00042 //
00043 // nsICommandMgr.cpp : test implementations for nsICommandManager interface
00044 //
00045 #include "stdafx.h"
00046 #include "testembed.h"
00047 #include "nsICommandMgr.h"
00048 #include "nsICmdParams.h"
00049 #include "QaUtils.h"
00050 #include "BrowserFrm.h"
00051 #include "BrowserImpl.h"
00052 #include "BrowserView.h"
00053 #include "Tests.h"
00054 
00055 #ifdef _DEBUG
00056 #define new DEBUG_NEW
00057 #undef THIS_FILE
00058 static char THIS_FILE[] = __FILE__;
00059 #endif
00060 
00062 // nsICommandMgr
00063 
00064 CnsICommandMgr::CnsICommandMgr(nsIWebBrowser *mWebBrowser)
00065 {
00066        qaWebBrowser = mWebBrowser;
00067 }
00068 
00069 CnsICommandMgr::~CnsICommandMgr()
00070 {
00071 }
00072 
00073 // 1st column: command; 2nd column: DoCommand state, 3rd column: CmdParam state; 
00074 CommandTest CommandTable[] = {
00075        {"cmd_bold",   "",    "state_all", PR_TRUE, 50000,    5.798,     "hello",                                        "HELLO"},
00076        {"cmd_italic", "",    "state_all", PR_TRUE, 10000000, 2.27,      "This is a complete sentence", "This isn't!"},
00077        {"cmd_underline", "", "state_all", PR_TRUE, 17,           7839.3480, "A longer string than the previous one.", "Shorter string, but still pretty long."},
00078        {"cmd_indent", "",     "state_enabled", PR_TRUE, 7480, -1.487, "Another string input for testing ...", "How about them Giants?!"},
00079        {"cmd_outdent", "",   "state_enabled", PR_FALSE, 0, 24987.2465, "A few numbers: 1 2 3,  A few letters: C A B,  A few characters: Mickey Goofy $%*&@", "nothing here"},
00080        {"cmd_increaseFont", "", "state_enabled", PR_TRUE, 500000000, 16, "hi", "HI"},
00081        {"cmd_undo", "",       "state_enabled", PR_TRUE, 987352487, 36.489, "x ", "x"},
00082        {"cmd_redo", "",       "state_enabled", PR_FALSE, 90, -24, "", " "},
00083        {"cmd_decreaseFont", "", "", PR_TRUE, 0.0,    0.0,     "hello",                                           "HELLO"},
00084        {"cmd_fontColor", "state_attribute", "state_attribute", PR_TRUE, 25,    100,     "#FF0000",                                           "#000000"},
00085        {"cmd_backgroundColor", "state_attribute", "state_attribute", PR_TRUE, -35871678,    15.345363645,     "#FF1234",                                          "#001234"},
00086        {"cmd_fontFace", "state_attribute", "state_attribute", PR_TRUE, 50000,    5.798,     "Times New Roman, Times, serif",                                      "Courier New, Courier, monospace"},
00087        {"cmd_align", "state_attribute", "state_attribute", PR_TRUE, 10000,    5.798,     "right",                                     "center"},
00088        {"cmd_charSet", "state_attribute", "state_attribute", PR_TRUE, 20000,    5.798,     "hello",                                          "HELLO"},
00089        {"cmd_copy", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                         "HELLO"},
00090        {"cmd_delete", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                       "HELLO"},
00091        {"cmd_deleteCharBackward", "", "state_enabled", PR_TRUE, 30000,    245.2323,     "hello",                                      "HELLO"},
00092        {"cmd_deleteCharForward", "", "state_enabled", PR_TRUE, 50000,    -24235.2346,     "a very very very very very very very very looooooooooooooooooooong stringgggggggggggggggggggggggggggggggggggggggggggg!!!",                                        "HELLO"},
00093        {"cmd_deleteWordForward", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                          "HELLO"},
00094        {"cmd_deleteWordBackward", "", "state_enabled", PR_TRUE, 6034600,    5.798,     "hello",                                       "HELLO"},
00095        {"cmd_deleteToBeginningOfLine", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                           "HELLO"},
00096        {"cmd_deleteToEndOfLine", "", "state_enabled", PR_TRUE, -5434,    5.798,     "hello",                                          "HELLO"},
00097        {"cmd_scrollTop", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                           "HELLO"},
00098        {"cmd_scrollBottom", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                        "HELLO"},
00099        {"cmd_scrollPageUp", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                        "HELLO"},
00100        {"cmd_scrollPageDown", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                      "HELLO"},
00101        {"cmd_movePageUp", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                          "HELLO"},
00102        {"cmd_movePageDown", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                        "HELLO"},
00103        {"cmd_moveTop", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                      "HELLO"},
00104        {"cmd_moveBottom", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                          "HELLO"},
00105        {"cmd_selectTop", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                           "HELLO"},
00106        {"cmd_selectBottom", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                        "HELLO"},
00107        {"cmd_lineNext", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                     "HELLO"},
00108        {"cmd_linePrevious", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                        "HELLO"},
00109        {"cmd_selectLineNext", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                      "HELLO"},
00110        {"cmd_selectLinePrevious", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                         "HELLO"},
00111        {"cmd_charPrevious", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                        "HELLO"},
00112        {"cmd_charNext", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                     "HELLO"},
00113        {"cmd_selectCharPrevious", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                         "HELLO"},
00114        {"cmd_selectCharNext", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                      "HELLO"},
00115        {"cmd_beginLine", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                           "HELLO"},
00116        {"cmd_endLine", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                      "HELLO"},
00117        {"cmd_selectBeginLine", "", "state_enabled", PR_FALSE, 50000,    5.798,     "hello",                                           "HELLO"},
00118        {"cmd_selectEndLine", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                       "HELLO"},
00119        {"cmd_wordPrevious", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                        "HELLO"},
00120        {"cmd_wordNext", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                     "HELLO"},
00121        {"cmd_selectWordPrevious", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                         "HELLO"},
00122        {"cmd_selectWordNext", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                      "HELLO"},
00123        {"cmd_cut", "", "state_enabled", PR_TRUE, 50000,    5.798,     "hello",                                          "HELLO"},
00124        {"cmd_cutOrDelete", "", "state_enabled", PR_FALSE, 50000,    5.798,     "hello",                                        "HELLO"},
00125 };
00126 
00127 nsICommandManager * CnsICommandMgr::GetCommandMgrObject(nsIWebBrowser *aWebBrowser, PRInt16 displayMethod)
00128 {
00129        nsCOMPtr<nsIWebBrowser> wb(aWebBrowser);
00130        nsCOMPtr<nsICommandManager> cmdMgrObj = do_GetInterface(wb, &rv);
00131        RvTestResult(rv, "GetCommandMgrObject() test", displayMethod);
00132     if (!cmdMgrObj) {
00133         QAOutput("Didn't get nsICommandManager object.");
00134               return nsnull;
00135        }
00136        return cmdMgrObj;
00137 }
00138 
00139 nsICommandManager * CnsICommandMgr::GetCommandMgrWithContractIDObject(PRInt16 displayMethod)
00140 {
00141        nsCOMPtr<nsICommandManager> cmdMgrObj = do_CreateInstance(NS_COMMAND_MANAGER_CONTRACTID, &rv);
00142        RvTestResult(rv, "GetCommandMgrWithContractIDObject() test", 1); 
00143     if (!cmdMgrObj) {
00144         QAOutput("Didn't get nsICommandManager object.");
00145               return nsnull;
00146        }
00147        return cmdMgrObj;
00148 }
00149 
00150 void CnsICommandMgr::IsCommandSupportedTest(const char *aCommandName, PRInt16 displayMethod)
00151 {
00152        PRBool isSupported;
00153 
00154        FormatAndPrintOutput("the Command input = ", aCommandName, displayMethod);
00155        cmdMgrObj = GetCommandMgrObject(qaWebBrowser, displayMethod);
00156        if (!cmdMgrObj) {
00157         QAOutput("Didn't get nsICommandManager object. Test failed.");
00158               return;
00159        }
00160        rv = cmdMgrObj->IsCommandSupported(aCommandName, nsnull, &isSupported);
00161        RvTestResult(rv, "IsCommandSupported() test", displayMethod);
00162        if (displayMethod == 1)
00163               RvTestResultDlg(rv, "IsCommandSupported() test", true);
00164        FormatAndPrintOutput("isSupported boolean = ", isSupported, displayMethod);
00165 }
00166 
00167 void CnsICommandMgr::IsCommandEnabledTest(const char *aCommandName, PRInt16 displayMethod)
00168 {
00169        PRBool isEnabled;
00170 
00171        FormatAndPrintOutput("the Command input = ", aCommandName, displayMethod);
00172        cmdMgrObj = GetCommandMgrObject(qaWebBrowser, displayMethod);
00173        if (!cmdMgrObj) {
00174         QAOutput("Didn't get nsICommandManager object.");
00175               return;
00176        }
00177        rv = cmdMgrObj->IsCommandEnabled(aCommandName, nsnull, &isEnabled);
00178        RvTestResult(rv, "IsCommandEnabled() test", displayMethod);
00179        if (displayMethod == 1)
00180               RvTestResultDlg(rv, "IsCommandEnabled() test");
00181        FormatAndPrintOutput("isEnabled boolean = ", isEnabled, displayMethod);
00182 }
00183 
00184 void CnsICommandMgr::GetCommandStateTest(const char *aCommandName, PRInt16 displayMethod)
00185 {
00186        PRBool enabled = PR_FALSE;
00187 
00188        FormatAndPrintOutput("the Command input = ", aCommandName, displayMethod);
00189        cmdMgrObj = GetCommandMgrObject(qaWebBrowser, displayMethod);
00190        cmdParamObj = CnsICmdParams::GetCommandParamObject();
00191        if (!cmdMgrObj) {
00192         QAOutput("Didn't get nsICommandManager object.");
00193               return;
00194        }
00195        rv = cmdMgrObj->GetCommandState(aCommandName, nsnull, cmdParamObj);
00196        RvTestResult(rv, "GetCommandState() test", displayMethod);
00197        if (displayMethod == 1)
00198               RvTestResultDlg(rv, "GetCommandState() test");
00199        if (!cmdParamObj) 
00200         QAOutput("Didn't get nsICommandParams object for GetCommandStateTest.");
00201 }
00202 
00203 void CnsICommandMgr::DoCommandTest(const char *aCommandName,
00204                                                            const char *doCommandState,
00205                                                            PRInt16 displayMethod)
00206 {
00207        nsCAutoString value;
00208 
00209        FormatAndPrintOutput("the Command input = ", aCommandName, displayMethod);
00210        cmdMgrObj = GetCommandMgrObject(qaWebBrowser, displayMethod);
00211        cmdParamObj = CnsICmdParams::GetCommandParamObject();
00212        if (!cmdMgrObj) {
00213         QAOutput("Didn't get nsICommandManager object. Tests fail");
00214               return;
00215        }
00216 
00217        if (strcmp(doCommandState, "state_attribute") == 0)
00218        {
00219               if (strcmp(aCommandName,"cmd_fontColor") == 0 ||
00220                   strcmp(aCommandName,"cmd_backgroundColor") == 0)
00221                      value = "#FF0000";
00222               else if (strcmp(aCommandName,"cmd_fontFace") == 0)
00223                      value = "Helvetica, Ariel, san-serif";
00224               else
00225                      value = "left";
00226 
00227               if (cmdParamObj)
00228                      cmdParamObj->SetCStringValue("state_attribute", value.get());
00229               else
00230                      QAOutput("Didn't get nsICommandParam object for nsICommandMgr test.");
00231        }
00232        rv = cmdMgrObj->DoCommand(aCommandName, cmdParamObj, nsnull);
00233        RvTestResult(rv, "DoCommand() test", displayMethod);
00234        if (displayMethod == 1)
00235               RvTestResultDlg(rv, "DoCommand() test");
00236 }
00237 
00238 
00239 void CnsICommandMgr::OnStartTests(UINT nMenuID)
00240 {
00241        switch(nMenuID)
00242        {
00243               case ID_INTERFACES_NSICOMMANDMANAGER_RUNALLTESTS :
00244                      RunAllTests();
00245                      break;
00246               case ID_INTERFACES_NSICOMMANDMANAGER_ADDCOMMANDOBSERVER :
00247                      QAOutput("Not implemented yet.", 2);
00248                      break;
00249               case ID_INTERFACES_NSICOMMANDMANAGER_REMOVECOMMANDOBSERVER :
00250                      QAOutput("Not implemented yet.", 2);
00251                      break;
00252               case ID_INTERFACES_NSICOMMANDMANAGER_ISCOMMANDESUPPORTED  :
00253                      IsCommandSupportedTest("cmd_bold", 2);
00254                      break;
00255               case ID_INTERFACES_NSICOMMANDMANAGER_ISCOMMANDENABLED :
00256                      IsCommandEnabledTest("cmd_bold", 2);
00257                      break;
00258               case ID_INTERFACES_NSICOMMANDMANAGER_GETCOMMANDSTATE :
00259                      GetCommandStateTest("cmd_charSet", 2);
00260                      break;
00261               case ID_INTERFACES_NSICOMMANDMANAGER_DOCOMMAND :
00262                      DoCommandTest("cmd_fontColor", "state_attribute", 2);
00263                      break;
00264        }
00265 }
00266 
00267 void CnsICommandMgr::RunAllTests()
00268 {
00269        PRInt16 i;
00270 
00271        QAOutput("Run All nsICommandManager tests. Check C:/Temp/TestOutput.txt for test results.", 2);
00272        for (i=0; i < 50; i++)
00273        {
00274               FormatAndPrintOutput("loop cnt = ", i, 1);
00275               IsCommandSupportedTest(CommandTable[i].mCmdName, 1);
00276               IsCommandEnabledTest(CommandTable[i].mCmdName, 1);
00277               GetCommandStateTest(CommandTable[i].mCmdName, 1);
00278               DoCommandTest(CommandTable[i].mCmdName,
00279                                      CommandTable[i].mDoCmdState, 1);
00280        }
00281 }
00282 
00283 
00285 // CnsICommandMgr message handlers