Back to index

lightning-sunbird  0.9+nobinonly
nsIFile.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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  *   David Epstein <depstein@netscape.com>
00024  *   Ashish Bhatt <ashishbhatt@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 // File Overview....
00041 //
00042 // Test cases for the nsIClipBoardCommand Interface
00043 
00044 
00045 #include "stdafx.h"
00046 #include "QaUtils.h"
00047 #include <stdio.h>
00048 #include "nsIFile.h"
00049 
00050 CNsIFile::CNsIFile()
00051 {
00052 
00053 }
00054 
00055 
00056 CNsIFile::~CNsIFile()
00057 {
00058 
00059 }
00060 
00061 void CNsIFile::OnStartTests(UINT nMenuID)
00062 {
00063        // Calls  all or indivdual test cases on the basis of the 
00064        // option selected from menu.
00065 
00066    nsCOMPtr<nsILocalFile> theTestFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
00067    nsCOMPtr<nsILocalFile> theFileOpDir(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
00068 
00069     if (!theTestFile)
00070        {
00071               QAOutput("File object doesn't exist. No File tests performed.", 2);
00072               return;
00073        }
00074 
00075        if (!theFileOpDir)
00076        {
00077               QAOutput("File object doesn't exist. No File tests performed.", 2);
00078               return;
00079        }
00080 
00081        switch(nMenuID)
00082        {             
00083               case ID_INTERFACES_NSIFILE_RUNALLTESTS    :
00084                      QAOutput("Begin nsIFile tests.", 1);
00085                      RunAllTests(theTestFile,theFileOpDir);
00086                      QAOutput("End nsIFile tests.", 1);
00087                      break ;
00088 
00089               case ID_INTERFACES_NSIFILE_INITWITHPATH   :
00090                      InitWithPathTest(theTestFile, 2);
00091                      break ;
00092 
00093               case ID_INTERFACES_NSIFILE_APPENDRELATICEPATH :
00094                      AppendRelativePathTest(theTestFile, 2);
00095                      break ;
00096 
00097               case ID_INTERFACES_NSIFILE_EXISTS :
00098                      FileCreateTest(theTestFile, 2);
00099                      break ;
00100 
00101               case ID_INTERFACES_NSIFILE_CREATE :
00102                      FileExistsTest(theTestFile, 2);
00103                      break ;
00104 
00105               case ID_INTERFACES_NSIFILE_COPYTO :
00106                      FileCopyTest(theTestFile, theFileOpDir, 2);      
00107                      break ;
00108 
00109               case ID_INTERFACES_NSIFILE_MOVETO :
00110                      FileMoveTest(theTestFile, theFileOpDir, 2);      
00111                      break ;
00112        }
00113 }
00114 
00115 
00116 // ***********************************************************************
00117 // ************************** Interface Tests ****************************
00118 // ***********************************************************************
00119 
00120 // nsIFile:
00121 
00122 void CNsIFile::RunAllTests(nsILocalFile *theTestFile, nsILocalFile *theFileOpDir)
00123 {
00124        InitWithPathTest(theTestFile, 1);
00125        AppendRelativePathTest(theTestFile, 1);
00126        FileCreateTest(theTestFile, 1);
00127        FileExistsTest(theTestFile, 1);
00128        FileCopyTest(theTestFile, theFileOpDir, 1);      
00129        FileMoveTest(theTestFile, theFileOpDir, 1);      
00130 }
00131 
00132 // ***********************************************************************
00133 
00134 // Individual nsIFile tests
00135 
00136 
00137 
00138 void CNsIFile::InitWithPathTest(nsILocalFile *theTestFile, PRInt16 displayMode)
00139 {
00140        rv = theTestFile->InitWithNativePath(NS_LITERAL_CSTRING("c:\\temp\\"));
00141        RvTestResult(rv, "InitWithNativePath() test (initializing file path)", displayMode);
00142        RvTestResultDlg(rv, "InitWithNativePath() test (initializing file path)", true);
00143 }
00144 
00145 void CNsIFile::AppendRelativePathTest(nsILocalFile *theTestFile, PRInt16 displayMode)
00146 {
00147        rv = theTestFile->AppendRelativeNativePath(NS_LITERAL_CSTRING("myFile.txt"));
00148        RvTestResult(rv, "AppendRelativeNativePath() test (append file to the path)", displayMode);
00149        RvTestResultDlg(rv, "AppendRelativeNativePath() test (append file to the path)");
00150 }
00151 
00152 void CNsIFile::FileCreateTest(nsILocalFile *theTestFile, PRInt16 displayMode)
00153 {
00154        PRBool exists =  PR_TRUE;
00155 
00156        rv = theTestFile->InitWithNativePath(NS_LITERAL_CSTRING("c:\\temp\\"));
00157        rv = theTestFile->AppendRelativeNativePath(NS_LITERAL_CSTRING("myFile.txt"));
00158 
00159        rv = theTestFile->Exists(&exists);
00160        if (!exists)
00161        {
00162               QAOutput("File doesn't exist. We'll try creating it.", displayMode);
00163               rv = theTestFile->Create(nsIFile::NORMAL_FILE_TYPE, 0777);
00164               RvTestResult(rv, "File Create() test ('myFile.txt')", displayMode);
00165               RvTestResultDlg(rv, "File Create() test ('myFile.txt')");
00166        }
00167        else
00168               QAOutput("File already exists (myFile.txt). We won't create it.", displayMode);
00169 }
00170 
00171 void CNsIFile::FileExistsTest(nsILocalFile *theTestFile, PRInt16 displayMode)
00172 {
00173        PRBool exists =  PR_TRUE;
00174 
00175        rv = theTestFile->InitWithNativePath(NS_LITERAL_CSTRING("c:\\temp\\"));
00176        rv = theTestFile->AppendRelativeNativePath(NS_LITERAL_CSTRING("myFile.txt"));
00177 
00178        rv = theTestFile->Exists(&exists);
00179        RvTestResult(rv, "File Exists() test ('myFile.txt')", displayMode);
00180        RvTestResultDlg(rv, "File Exists() test ('myFile.txt')");
00181        if (!exists)
00182               QAOutput("Exists() test Failed. File (myFile.txt) doesn't exist.", displayMode);
00183        else
00184               QAOutput("Exists() test Passed. File (myFile.txt) exists.", displayMode);
00185 }
00186 
00187 void CNsIFile::FileCopyTest(nsILocalFile *theTestFile, nsILocalFile *theFileOpDir,
00188                                                  PRInt16 displayMode)
00189 {
00190        PRBool exists =  PR_TRUE;
00191 
00192        QAOutput("Start File Copy test.", displayMode);
00193        rv = theFileOpDir->InitWithNativePath(NS_LITERAL_CSTRING("c:\\temp\\"));
00194 
00195        if (NS_FAILED(rv))
00196               QAOutput("The target dir wasn't found.", displayMode);
00197        else
00198               QAOutput("The target dir was found.", displayMode);
00199 
00200        rv = theTestFile->InitWithNativePath(NS_LITERAL_CSTRING("c:\\temp\\myFile.txt"));
00201        if (NS_FAILED(rv))
00202               QAOutput("The path wasn't found.", displayMode);
00203        else
00204               QAOutput("The path was found.", displayMode);
00205 
00206        rv = theTestFile->CopyToNative(theFileOpDir, NS_LITERAL_CSTRING("myFile2.txt"));
00207        RvTestResult(rv, "rv CopyToNative() test", displayMode);
00208        RvTestResultDlg(rv, "File CopyToNative() test ('myFile.txt')");
00209 
00210        rv = theTestFile->InitWithNativePath(NS_LITERAL_CSTRING("c:\\temp\\myFile2.txt"));
00211 
00212        rv = theTestFile->Exists(&exists);
00213        if (!exists)
00214               QAOutput("File didn't copy. CopyToNative() test Failed.", displayMode);
00215        else
00216               QAOutput("File copied. CopyToNative() test Passed.", displayMode);
00217 }
00218 
00219 void CNsIFile::FileMoveTest(nsILocalFile *theTestFile, nsILocalFile *theFileOpDir,
00220                                                  PRInt16 displayMode)
00221 {
00222        PRBool exists =  PR_TRUE;
00223 
00224        QAOutput("Start File Move test.", displayMode);
00225 
00226        rv = theFileOpDir->InitWithNativePath(NS_LITERAL_CSTRING("c:\\Program Files\\"));
00227        if (NS_FAILED(rv))
00228               QAOutput("The target dir wasn't found.", displayMode);
00229 
00230        rv = theTestFile->InitWithNativePath(NS_LITERAL_CSTRING("c:\\temp\\myFile2.txt"));
00231        if (NS_FAILED(rv))
00232               QAOutput("The path wasn't found.", displayMode);
00233 
00234        rv = theTestFile->MoveToNative(theFileOpDir, NS_LITERAL_CSTRING("myFile2.txt"));
00235        RvTestResult(rv, "MoveToNative() test", displayMode);
00236        RvTestResultDlg(rv, "File MoveToNative() test ('myFile.txt')");
00237 
00238        rv = theTestFile->InitWithNativePath(NS_LITERAL_CSTRING("c:\\Program Files\\myFile2.txt"));
00239 
00240        rv = theTestFile->Exists(&exists);
00241        if (!exists)
00242               QAOutput("File wasn't moved. MoveToNative() test Failed.", displayMode);
00243        else
00244               QAOutput("File was moved. MoveToNative() test Passed.", displayMode);
00245 }
00246