diff --git a/webshell/tests/viewer/makefile.win b/webshell/tests/viewer/makefile.win index 1c318ded153b..118452d1ad8d 100644 --- a/webshell/tests/viewer/makefile.win +++ b/webshell/tests/viewer/makefile.win @@ -31,7 +31,8 @@ MISCDEP= \ OBJS = \ .\$(OBJDIR)\winmain.obj \ - .\$(OBJDIR)\JSConsole.obj \ + .\$(OBJDIR)\JSConsole.obj \ + .\$(OBJDIR)\nsDocLoader.obj \ $(NULL) LINCS=-I$(PUBLIC)\raptor -I$(PUBLIC)\xpcom -I$(PUBLIC)\dom -I$(PUBLIC)\js diff --git a/webshell/tests/viewer/nsDocLoader.cpp b/webshell/tests/viewer/nsDocLoader.cpp new file mode 100644 index 000000000000..e54ca43844b5 --- /dev/null +++ b/webshell/tests/viewer/nsDocLoader.cpp @@ -0,0 +1,254 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ + +#include "nsDocLoader.h" +#include "nsITimer.h" +#include "nsITimerCallback.h" +#include "nsVoidArray.h" +#include "plstr.h" +#include "nsIWebWidget.h" +#include "resources.h" +#include "nsString.h" + +/* + This class loads creates and loads URLs until finished. + When the doc counter reaches zero, the DocLoader (optionally) + posts an exit application message + +*/ + +nsDocLoader::nsDocLoader(nsIWebWidget* aWebWidget, PRInt32 aSeconds, PRBool aPostExit) +{ + mStart = PR_FALSE; + mDelay = aSeconds; + mPostExit = aPostExit; + mDocNum = 0; + mWebWidget = aWebWidget; + + mTimers = new nsVoidArray(); + mURLList = new nsVoidArray(); +} + +nsDocLoader::~nsDocLoader() +{ + if (mURLList) + { + PRInt32 count = mURLList->Count(); + for (int i = 0; i < count; i++) + { + nsString* s = (nsString*)mURLList->ElementAt(i); + delete s; + } + delete mURLList; + mURLList = nsnull; + } + + if (mTimers) + { + delete mTimers; + mTimers = nsnull; + } + +} + +// Set the delay (by default, the timer is set to one second) +void nsDocLoader::SetDelay(PRInt32 aSeconds) +{ + mDelay = aSeconds; +} + + +// Add a URL to the doc loader +void nsDocLoader::AddURL(char* aURL) +{ + if (aURL) + { + if (mStart == PR_FALSE) + { + nsString* url; + + if (mURLList == nsnull) + mURLList = new nsVoidArray(); + + url = new nsString(aURL); + mURLList->AppendElement((void*)url); + } + } +} + +// Add a URL to the doc loader +void nsDocLoader::AddURL(nsString* aURL) +{ + if (aURL) + { + if (mStart == PR_FALSE) + { + nsString* url; + + if (mURLList == nsnull) + mURLList = new nsVoidArray(); + url = new nsString(*aURL); + mURLList->AppendElement((void*)url); + } + } +} + +void nsDocLoader::StartTimedLoading() +{ + // Only start once! + if (mStart == PR_FALSE) + { + mStart = PR_TRUE; + if (mURLList) + { + CreateRepeat(mDelay*1000); + } + } +} + + + + +/* +* +* BEGIN PURIFY METHODS +* +*/ + +nsDocLoader* gDocLoader = nsnull; +void MyCallback (nsITimer *aTimer, void *aClosure) +{ + + if (gDocLoader != nsnull) + { + nsVoidArray* timers = gDocLoader->GetTimers(); + printf("Timer executed with delay %d\n", (int)aClosure); + if (timers->RemoveElement(aTimer) == PR_TRUE) + { + NS_RELEASE(aTimer); + gDocLoader->CallTest(); + } + } +} + + +void nsDocLoader::CreateOneShot(PRUint32 aDelay) +{ + nsITimer *timer; + gDocLoader = this; + NS_NewTimer(&timer); + timer->Init(MyCallback, (void *)aDelay, aDelay); + mTimers->AppendElement(timer); +} + + +void MyRepeatCallback (nsITimer *aTimer, void *aClosure) +{ + if (gDocLoader != nsnull) + { + nsVoidArray* timers = gDocLoader->GetTimers(); + + printf("Timer executed with delay %d\n", (int)aClosure); + if (timers->RemoveElement(aTimer) == PR_TRUE) + { + NS_RELEASE(aTimer); + gDocLoader->CallTest(); + } + gDocLoader->CreateRepeat((PRUint32)aClosure); + } +} + +void nsDocLoader::CreateRepeat(PRUint32 aDelay) +{ + if (mTimers) + { + gDocLoader = this; + nsITimer *timer; + + NS_NewTimer(&timer); + timer->Init(MyRepeatCallback, (void *)aDelay, aDelay); + + mTimers->AppendElement(timer); + } +} + + +void nsDocLoader::DoAction(PRInt32 aDocNum) +{ + if (aDocNum >= 0 && aDocNum < mURLList->Count()) + { + nsString* url = (nsString*)mURLList->ElementAt(aDocNum); + if (url) + mWebWidget->LoadURL(*url); + } +} + + +void nsDocLoader::CallTest() +{ + if (mDocNum < mURLList->Count()) + { + DoAction(mDocNum++); + } + else + { + CancelAll(); + + printf("Finished Running Tests"); + if (mPostExit) + { + printf("QUITTING APPLICATION \n"); + PostMessage(HWND_BROADCAST,(UINT)WM_COMMAND,(WPARAM)VIEWER_EXIT,0); + } + } +} + + + + +void nsDocLoader::CancelAll() +{ + int i, count = mTimers->Count(); + + for (i=0; i < count; i++) + { + nsITimer *timer = (nsITimer *)mTimers->ElementAt(i); + + if (timer != NULL) + { + timer->Cancel(); + NS_RELEASE(timer); + } + } + mTimers->Clear(); + delete mTimers; + mTimers = nsnull; + + if (mURLList) + { + PRInt32 count = mURLList->Count(); + for (int i = 0; i < count; i++) + { + nsString* s = (nsString*)mURLList->ElementAt(i); + delete s; + } + delete mURLList; + mURLList = nsnull; + } + +} diff --git a/webshell/tests/viewer/nsDocLoader.h b/webshell/tests/viewer/nsDocLoader.h new file mode 100644 index 000000000000..76b4b5f349d3 --- /dev/null +++ b/webshell/tests/viewer/nsDocLoader.h @@ -0,0 +1,94 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ +#ifndef nsDocLoader_h___ +#define nsDocLoader_h___ + +#include "nscore.h" +#include "nsISupports.h" + + +class nsIWebWidget; +class nsITimer; +class nsVoidArray; +class nsString; + +/* + This class loads creates and loads URLs until finished. + When the doc counter reaches zero, the DocLoader (optionally) + posts an exit application message +*/ + +class nsDocLoader +{ + public: + nsDocLoader(nsIWebWidget* aWebWidget, PRInt32 aSeconds=1, PRBool aPostExit=PR_TRUE); + virtual ~nsDocLoader(); + + // Add a URL to the doc loader + void AddURL(char* aURL); + void AddURL(nsString* aURL); + + // Get the timer and the url list + // needed to be available for the call back methods + nsVoidArray* GetTimers() + { return mTimers; } + nsVoidArray* GetURLList() + { return mURLList; } + + + void CreateOneShot(PRUint32 aDelay); + void CreateRepeat(PRUint32 aDelay); + void CallTest(); + + // Set the delay (by default, the timer is set to one second) + void SetDelay(PRInt32 aSeconds); + + // If set to TRUE the loader will post an exit message on + // exit + void SetExitOnDone(PRBool aPostExit); + + // Start Loading the list of documents and executing the + // Do Action Method on the documents + void StartTimedLoading(); + + + // By default this method loads a document from + // the list of documents added to the loader + // Override in subclasses to get different behavior + virtual void DoAction(PRInt32 aDocNum); + + + private: + + void CancelAll(); + + + + PRInt32 mDocNum; + PRBool mStart; + PRInt32 mDelay; + PRBool mPostExit; + nsIWebWidget* mWebWidget; + + nsVoidArray* mURLList; + nsVoidArray* mTimers; +}; + + + +#endif \ No newline at end of file diff --git a/webshell/tests/viewer/winmain.cpp b/webshell/tests/viewer/winmain.cpp index 4003a9675ce1..d80622817b16 100644 --- a/webshell/tests/viewer/winmain.cpp +++ b/webshell/tests/viewer/winmain.cpp @@ -47,6 +47,7 @@ #include "plevent.h" #include #include "nsIScriptContext.h" +#include "nsDocLoader.h" // JSConsole window JSConsole *gConsole = NULL; @@ -162,6 +163,50 @@ static DocObserver* NewObserver(nsIWebWidget* ww) //---------------------------------------------------------------------- + + +/* +* +* BEGIN PURIFY METHODS +* +*/ + + + +PRBool DoPurifyTest() +{ +#ifdef NS_WIN32 + char* name = "MOZ_PURIFY_TEST"; + char buffer[256]; + int result; + + result = GetEnvironmentVariable(name, buffer, 256); + return PRBool(result != 0); +#endif + return PR_FALSE; +} + + +void AddTestDocs(nsDocLoader* aDocLoader) +{ + char url[500]; + for (int docnum = 0; docnum < 9; docnum++) + { + PR_snprintf(url, 500, "%s/test%d.html", SAMPLES_BASE_URL, docnum); + aDocLoader->AddURL(url); + } +} + +/* +* +* END PURIFY METHODS +* +*/ + + + + + static nsresult ShowPrintPreview(nsIWebWidget* ww, PRIntn aColumns); void DestroyConsole() @@ -533,8 +578,25 @@ WinMain(HANDLE instance, HANDLE prevInstance, LPSTR cmdParam, int nCmdShow) wd->ww->Show(); wd->observer = NewObserver(wd->ww); - // Load the starting url if we have one - wd->ww->LoadURL(startURL ? startURL : START_URL); + + // Determine if we should run the purify test + PRBool purify = DoPurifyTest(); + nsDocLoader* dl = nsnull; + if (purify) + { + dl = new nsDocLoader(wd->ww); + + // Add the documents to the loader + AddTestDocs(dl); + + // Start the timer + dl->StartTimedLoading(); + } + else + { + // Load the starting url if we have one + wd->ww->LoadURL(startURL ? startURL : START_URL); + } // Process messages MSG msg;