Back to index

lightning-sunbird  0.9+nobinonly
TestSpaceManager.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include <stdio.h>
00038 #include "nscore.h"
00039 #include "nsCRT.h"
00040 #include "nsSpaceManager.h"
00041 
00042 class MySpaceManager: public nsSpaceManager {
00043 public:
00044   MySpaceManager(nsIPresShell* aPresShell, nsIFrame* aFrame)
00045       : nsSpaceManager(aPresShell, aFrame) {}
00046 
00047   PRBool  TestAddBand();
00048   PRBool  TestAddBandOverlap();
00049   PRBool  TestAddRectToBand();
00050   PRBool  TestRemoveRegion();
00051   PRBool  TestGetBandData();
00052 
00053   struct BandInfo {
00054     nscoord   yOffset;
00055     nscoord   height;
00056     BandRect* firstRect;
00057     PRIntn    numRects;
00058   };
00059 
00060   struct BandsInfo {
00061     PRIntn   numBands;
00062     BandInfo bands[25];
00063   };
00064 
00065 protected:
00066   void  GetBandsInfo(BandsInfo&);
00067 };
00068 
00069 void MySpaceManager::GetBandsInfo(BandsInfo& aBandsInfo)
00070 {
00071   aBandsInfo.numBands = 0;
00072 
00073   if (!mBandList.IsEmpty()) {
00074     BandRect* band = mBandList.Head();
00075     while (nsnull != band) {
00076       BandInfo& info = aBandsInfo.bands[aBandsInfo.numBands];
00077 
00078       info.yOffset = band->mTop;
00079       info.height = band->mBottom - band->mTop;
00080       info.firstRect = band;
00081 
00082       aBandsInfo.numBands++;
00083 
00084       // Get the next band, and count the number of rects in this band
00085       info.numRects = 0;
00086       while (info.yOffset == band->mTop) {
00087         info.numRects++;
00088 
00089         band = band->Next();
00090         if (band == &mBandList) {
00091           // No bands left
00092           band = nsnull;
00093           break;
00094         }
00095       }
00096     }
00097   }
00098 }
00099 
00101 //
00102 
00103 // Test of adding a rect region that causes a new band to be created (no
00104 // overlap with an existing band)
00105 //
00106 // This tests the following:
00107 // 1. when there are no existing bands
00108 // 2. adding a new band above the topmost band
00109 // 3. inserting a new band between two existing bands
00110 // 4. adding a new band below the bottommost band
00111 PRBool MySpaceManager::TestAddBand()
00112 {
00113   BandsInfo bandsInfo;
00114   nsresult  status;
00115   
00116   // Clear any existing regions
00117   ClearRegions();
00118   NS_ASSERTION(mBandList.IsEmpty(), "clear regions failed");
00119 
00121   // #1. Add a rect region. Verify the return status, and that a band rect is
00122   // added
00123   status = AddRectRegion((nsIFrame*)0x01, nsRect(10, 100, 100, 100));
00124   if (NS_FAILED(status)) {
00125     printf("TestAddBand: add failed (#1)\n");
00126     return PR_FALSE;
00127   }
00128   GetBandsInfo(bandsInfo);
00129   if (bandsInfo.numBands != 1) {
00130     printf("TestAddBand: wrong number of bands (#1): %i\n", bandsInfo.numBands);
00131     return PR_FALSE;
00132   }
00133   if ((bandsInfo.bands[0].yOffset != 100) || (bandsInfo.bands[0].height != 100)) {
00134     printf("TestAddBand: wrong band size (#1)\n");
00135     return PR_FALSE;
00136   }
00137 
00139   // #2. Add another band rect completely above the first band rect
00140   status = AddRectRegion((nsIFrame*)0x02, nsRect(10, -10, 100, 20));
00141   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00142   GetBandsInfo(bandsInfo);
00143   if (bandsInfo.numBands != 2) {
00144     printf("TestAddBand: wrong number of bands (#2): %i\n", bandsInfo.numBands);
00145     return PR_FALSE;
00146   }
00147   if ((bandsInfo.bands[0].yOffset != -10) || (bandsInfo.bands[0].height != 20) ||
00148       (bandsInfo.bands[1].yOffset != 100) || (bandsInfo.bands[1].height != 100)) {
00149     printf("TestAddBand: wrong band sizes (#2)\n");
00150     return PR_FALSE;
00151   }
00152 
00154   // #3. Now insert a new band between the two existing bands
00155   status = AddRectRegion((nsIFrame*)0x03, nsRect(10, 40, 100, 30));
00156   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00157   GetBandsInfo(bandsInfo);
00158   if (bandsInfo.numBands != 3) {
00159     printf("TestAddBand: wrong number of bands (#3): %i\n", bandsInfo.numBands);
00160     return PR_FALSE;
00161   }
00162   if ((bandsInfo.bands[0].yOffset != -10) || (bandsInfo.bands[0].height != 20) ||
00163       (bandsInfo.bands[1].yOffset != 40) || (bandsInfo.bands[1].height != 30) ||
00164       (bandsInfo.bands[2].yOffset != 100) || (bandsInfo.bands[2].height != 100)) {
00165     printf("TestAddBand: wrong band sizes (#3)\n");
00166     return PR_FALSE;
00167   }
00168 
00170   // #4. Append a new bottommost band
00171   status = AddRectRegion((nsIFrame*)0x04, nsRect(10, 210, 100, 100));
00172   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00173   GetBandsInfo(bandsInfo);
00174   if (bandsInfo.numBands != 4) {
00175     printf("TestAddBand: wrong number of bands (#4): %i\n", bandsInfo.numBands);
00176     return PR_FALSE;
00177   }
00178   if ((bandsInfo.bands[0].yOffset != -10) || (bandsInfo.bands[0].height != 20) ||
00179       (bandsInfo.bands[1].yOffset != 40) || (bandsInfo.bands[1].height != 30) ||
00180       (bandsInfo.bands[2].yOffset != 100) || (bandsInfo.bands[2].height != 100) ||
00181       (bandsInfo.bands[3].yOffset != 210) || (bandsInfo.bands[3].height != 100)) {
00182     printf("TestAddBand: wrong band sizes (#4)\n");
00183     return PR_FALSE;
00184   }
00185 
00186   return PR_TRUE;
00187 }
00188 
00189 // Test of adding a rect region that overlaps an existing band
00190 //
00191 // This tests the following:
00192 // 1. Adding a rect that's above and partially overlaps an existing band
00193 // 2. Adding a rect that's completely contained by an existing band
00194 // 3. Adding a rect that overlaps and is below an existing band
00195 // 3. Adding a rect that contains an existing band
00196 PRBool MySpaceManager::TestAddBandOverlap()
00197 {
00198   BandsInfo bandsInfo;
00199   nsresult  status;
00200   
00201   // Clear any existing regions
00202   ClearRegions();
00203   NS_ASSERTION(mBandList.IsEmpty(), "clear regions failed");
00204 
00205   // Add a new band
00206   status = AddRectRegion((nsIFrame*)0x01, nsRect(100, 25, 100, 100));
00207   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00208 
00210   // #1. Add a rect region that's above and partially overlaps an existing band
00211   status = AddRectRegion((nsIFrame*)0x02, nsRect(10, -25, 50, 100));
00212   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00213   GetBandsInfo(bandsInfo);
00214   if (bandsInfo.numBands != 3) {
00215     printf("TestAddBandOverlap: wrong number of bands (#1): %i\n", bandsInfo.numBands);
00216     return PR_FALSE;
00217   }
00218   if ((bandsInfo.bands[0].yOffset != -25) || (bandsInfo.bands[0].height != 50) ||
00219       (bandsInfo.bands[1].yOffset != 25) || (bandsInfo.bands[1].height != 50) ||
00220       (bandsInfo.bands[2].yOffset != 75) || (bandsInfo.bands[2].height != 50)) {
00221     printf("TestAddBandOverlap: wrong band sizes (#1)\n");
00222     return PR_FALSE;
00223   }
00224   if ((bandsInfo.bands[0].numRects != 1) ||
00225       (bandsInfo.bands[1].numRects != 2) ||
00226       (bandsInfo.bands[2].numRects != 1)) {
00227     printf("TestAddBandOverlap: wrong number of rects (#1)\n");
00228     return PR_FALSE;
00229   }
00230 
00232   // #2. Add a rect region that's contained by the first band
00233   status = AddRectRegion((nsIFrame*)0x03, nsRect(200, -15, 50, 10));
00234   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00235   GetBandsInfo(bandsInfo);
00236   if (bandsInfo.numBands != 5) {
00237     printf("TestAddBandOverlap: wrong number of bands (#2): %i\n", bandsInfo.numBands);
00238     return PR_FALSE;
00239   }
00240   if ((bandsInfo.bands[0].yOffset != -25) || (bandsInfo.bands[0].height != 10) ||
00241       (bandsInfo.bands[1].yOffset != -15) || (bandsInfo.bands[1].height != 10) ||
00242       (bandsInfo.bands[2].yOffset != -5) || (bandsInfo.bands[2].height != 30) ||
00243       (bandsInfo.bands[3].yOffset != 25) || (bandsInfo.bands[3].height != 50) ||
00244       (bandsInfo.bands[4].yOffset != 75) || (bandsInfo.bands[4].height != 50)) {
00245     printf("TestAddBandOverlap: wrong band sizes (#2)\n");
00246     return PR_FALSE;
00247   }
00248   if ((bandsInfo.bands[0].numRects != 1) ||
00249       (bandsInfo.bands[1].numRects != 2) ||
00250       (bandsInfo.bands[2].numRects != 1) ||
00251       (bandsInfo.bands[3].numRects != 2) ||
00252       (bandsInfo.bands[4].numRects != 1)) {
00253     printf("TestAddBandOverlap: wrong number of rects (#2)\n");
00254     return PR_FALSE;
00255   }
00256 
00258   // #3. Add a rect that overlaps and is below an existing band
00259   status = AddRectRegion((nsIFrame*)0x04, nsRect(200, 100, 50, 50));
00260   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00261   GetBandsInfo(bandsInfo);
00262   if (bandsInfo.numBands != 7) {
00263     printf("TestAddBandOverlap: wrong number of bands (#3): %i\n", bandsInfo.numBands);
00264     return PR_FALSE;
00265   }
00266   if ((bandsInfo.bands[0].yOffset != -25) || (bandsInfo.bands[0].height != 10) ||
00267       (bandsInfo.bands[1].yOffset != -15) || (bandsInfo.bands[1].height != 10) ||
00268       (bandsInfo.bands[2].yOffset != -5) || (bandsInfo.bands[2].height != 30) ||
00269       (bandsInfo.bands[3].yOffset != 25) || (bandsInfo.bands[3].height != 50) ||
00270       (bandsInfo.bands[4].yOffset != 75) || (bandsInfo.bands[4].height != 25) ||
00271       (bandsInfo.bands[5].yOffset != 100) || (bandsInfo.bands[5].height != 25) ||
00272       (bandsInfo.bands[6].yOffset != 125) || (bandsInfo.bands[6].height != 25)) {
00273     printf("TestAddBandOverlap: wrong band sizes (#3)\n");
00274     return PR_FALSE;
00275   }
00276   if ((bandsInfo.bands[0].numRects != 1) ||
00277       (bandsInfo.bands[1].numRects != 2) ||
00278       (bandsInfo.bands[2].numRects != 1) ||
00279       (bandsInfo.bands[3].numRects != 2) ||
00280       (bandsInfo.bands[4].numRects != 1) ||
00281       (bandsInfo.bands[5].numRects != 2) ||
00282       (bandsInfo.bands[6].numRects != 1)) {
00283     printf("TestAddBandOverlap: wrong number of rects (#3)\n");
00284     return PR_FALSE;
00285   }
00286 
00288   // #4. Now test adding a rect that contains an existing band
00289   ClearRegions();
00290   status = AddRectRegion((nsIFrame*)0x01, nsRect(100, 100, 100, 100));
00291   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00292 
00293   // Now add a rect that contains the existing band vertically
00294   status = AddRectRegion((nsIFrame*)0x02, nsRect(200, 50, 100, 200));
00295   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00296 
00297   GetBandsInfo(bandsInfo);
00298   if (bandsInfo.numBands != 3) {
00299     printf("TestAddBandOverlap: wrong number of bands (#4): %i\n", bandsInfo.numBands);
00300     return PR_FALSE;
00301   }
00302   if ((bandsInfo.bands[0].yOffset != 50) || (bandsInfo.bands[0].height != 50) ||
00303       (bandsInfo.bands[1].yOffset != 100) || (bandsInfo.bands[1].height != 100) ||
00304       (bandsInfo.bands[2].yOffset != 200) || (bandsInfo.bands[2].height != 50)) {
00305     printf("TestAddBandOverlap: wrong band sizes (#4)\n");
00306     return PR_FALSE;
00307   }
00308   if ((bandsInfo.bands[0].numRects != 1) ||
00309       (bandsInfo.bands[1].numRects != 2) ||
00310       (bandsInfo.bands[2].numRects != 1)) {
00311     printf("TestAddBandOverlap: wrong number of rects (#4)\n");
00312     return PR_FALSE;
00313   }
00314 
00315   return PR_TRUE;
00316 }
00317 
00318 // Test of adding rects within an existing band
00319 //
00320 // This tests the following:
00321 // 1. Add a rect to the left of an existing rect
00322 // 2. Add a rect to the right of the rightmost rect
00323 // 3. Add a rect that's to the left of an existing rect and that overlaps it
00324 // 4. Add a rect that's to the right of an existing rect and that overlaps it
00325 // 5. Add a rect over top of an existing rect (existing rect contains new rect)
00326 // 6. Add a new rect that completely contains an existing rect
00327 PRBool MySpaceManager::TestAddRectToBand()
00328 {
00329   BandsInfo bandsInfo;
00330   BandRect* bandRect;
00331   nsresult  status;
00332 
00333   // Clear any existing regions
00334   ClearRegions();
00335   NS_ASSERTION(mBandList.IsEmpty(), "clear regions failed");
00336 
00337   // Add a new band
00338   status = AddRectRegion((nsIFrame*)0x01, nsRect(100, 100, 100, 100));
00339   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00340 
00342   // #1. Add a rect region that's to the left of the existing rect
00343   status = AddRectRegion((nsIFrame*)0x02, nsRect(10, 100, 50, 100));
00344   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00345   GetBandsInfo(bandsInfo);
00346   if (bandsInfo.numBands != 1) {
00347     printf("TestAddRectToBand: wrong number of bands (#1): %i\n", bandsInfo.numBands);
00348     return PR_FALSE;
00349   }
00350   if (bandsInfo.bands[0].numRects != 2) {
00351     printf("TestAddRectToBand: wrong number of rects (#1): %i\n", bandsInfo.bands[0].numRects);
00352     return PR_FALSE;
00353   }
00354   bandRect = bandsInfo.bands[0].firstRect;
00355   if ((bandRect->mLeft != 10) || (bandRect->mRight != 60)) {
00356     printf("TestAddRectToBand: wrong first rect (#1)\n");
00357     return PR_FALSE;
00358   }
00359   bandRect = bandRect->Next();
00360   if ((bandRect->mLeft != 100) || (bandRect->mRight != 200)) {
00361     printf("TestAddRectToBand: wrong second rect (#1)\n");
00362     return PR_FALSE;
00363   }
00364 
00366   // #2. Add a rect region that's to the right of the rightmost rect
00367   status = AddRectRegion((nsIFrame*)0x03, nsRect(250, 100, 100, 100));
00368   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00369   GetBandsInfo(bandsInfo);
00370   NS_ASSERTION(bandsInfo.numBands == 1, "wrong number of bands");
00371   if (bandsInfo.bands[0].numRects != 3) {
00372     printf("TestAddRectToBand: wrong number of rects (#2): %i\n", bandsInfo.bands[0].numRects);
00373     return PR_FALSE;
00374   }
00375   bandRect = bandsInfo.bands[0].firstRect;
00376   if ((bandRect->mLeft != 10) || (bandRect->mRight != 60)) {
00377     printf("TestAddRectToBand: wrong first rect (#2)\n");
00378     return PR_FALSE;
00379   }
00380   bandRect = bandRect->Next();
00381   if ((bandRect->mLeft != 100) || (bandRect->mRight != 200)) {
00382     printf("TestAddRectToBand: wrong second rect (#2)\n");
00383     return PR_FALSE;
00384   }
00385   bandRect = bandRect->Next();
00386   if ((bandRect->mLeft != 250) || (bandRect->mRight != 350)) {
00387     printf("TestAddRectToBand: wrong third rect (#2)\n");
00388     return PR_FALSE;
00389   }
00390 
00392   // #3. Add a rect region that's to the left of an existing rect and that
00393   // overlaps the rect
00394   status = AddRectRegion((nsIFrame*)0x04, nsRect(80, 100, 40, 100));
00395   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00396   GetBandsInfo(bandsInfo);
00397   NS_ASSERTION(bandsInfo.numBands == 1, "wrong number of bands");
00398   if (bandsInfo.bands[0].numRects != 5) {
00399     printf("TestAddRectToBand: wrong number of rects (#3): %i\n", bandsInfo.bands[0].numRects);
00400     return PR_FALSE;
00401   }
00402   bandRect = bandsInfo.bands[0].firstRect;
00403   if ((bandRect->mLeft != 10) || (bandRect->mRight != 60)) {
00404     printf("TestAddRectToBand: wrong first rect (#3)\n");
00405     return PR_FALSE;
00406   }
00407   bandRect = bandRect->Next();
00408   NS_ASSERTION(1 == bandRect->mNumFrames, "unexpected shared rect");
00409   if ((bandRect->mLeft != 80) || (bandRect->mRight != 100)) {
00410     printf("TestAddRectToBand: wrong second rect (#3)\n");
00411     return PR_FALSE;
00412   }
00413   bandRect = bandRect->Next();
00414   if ((bandRect->mLeft != 100) || (bandRect->mRight != 120) ||
00415       (bandRect->mNumFrames != 2) || !bandRect->IsOccupiedBy((nsIFrame*)0x04)) {
00416     printf("TestAddRectToBand: wrong third rect (#3)\n");
00417     return PR_FALSE;
00418   }
00419   bandRect = bandRect->Next();
00420   NS_ASSERTION(1 == bandRect->mNumFrames, "unexpected shared rect");
00421   if ((bandRect->mLeft != 120) || (bandRect->mRight != 200)) {
00422     printf("TestAddRectToBand: wrong fourth rect (#3)\n");
00423     return PR_FALSE;
00424   }
00425   bandRect = bandRect->Next();
00426   if ((bandRect->mLeft != 250) || (bandRect->mRight != 350)) {
00427     printf("TestAddRectToBand: wrong fifth rect (#3)\n");
00428     return PR_FALSE;
00429   }
00430 
00432   // #4. Add a rect region that's to the right of an existing rect and that
00433   // overlaps the rect
00434   status = AddRectRegion((nsIFrame*)0x05, nsRect(50, 100, 20, 100));
00435   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00436   GetBandsInfo(bandsInfo);
00437   NS_ASSERTION(bandsInfo.numBands == 1, "wrong number of bands");
00438   if (bandsInfo.bands[0].numRects != 7) {
00439     printf("TestAddRectToBand: wrong number of rects (#4): %i\n", bandsInfo.bands[0].numRects);
00440     return PR_FALSE;
00441   }
00442   bandRect = bandsInfo.bands[0].firstRect;
00443   NS_ASSERTION(1 == bandRect->mNumFrames, "unexpected shared rect");
00444   if ((bandRect->mLeft != 10) || (bandRect->mRight != 50)) {
00445     printf("TestAddRectToBand: wrong first rect (#4)\n");
00446     return PR_FALSE;
00447   }
00448   bandRect = bandRect->Next();
00449   if ((bandRect->mLeft != 50) || (bandRect->mRight != 60) ||
00450       (bandRect->mNumFrames != 2) || !bandRect->IsOccupiedBy((nsIFrame*)0x05)) {
00451     printf("TestAddRectToBand: wrong second rect (#4)\n");
00452     return PR_FALSE;
00453   }
00454   bandRect = bandRect->Next();
00455   NS_ASSERTION(1 == bandRect->mNumFrames, "unexpected shared rect");
00456   if ((bandRect->mLeft != 60) || (bandRect->mRight != 70)) {
00457     printf("TestAddRectToBand: wrong third rect (#4)\n");
00458     return PR_FALSE;
00459   }
00460   bandRect = bandRect->Next();
00461   NS_ASSERTION(1 == bandRect->mNumFrames, "unexpected shared rect");
00462   if ((bandRect->mLeft != 80) || (bandRect->mRight != 100)) {
00463     printf("TestAddRectToBand: wrong fourth rect (#4)\n");
00464     return PR_FALSE;
00465   }
00466 
00468   // #5. Add a new rect over top of an existing rect (existing rect contains
00469   // the new rect)
00470   status = AddRectRegion((nsIFrame*)0x06, nsRect(20, 100, 20, 100));
00471   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00472   GetBandsInfo(bandsInfo);
00473   NS_ASSERTION(bandsInfo.numBands == 1, "wrong number of bands");
00474   if (bandsInfo.bands[0].numRects != 9) {
00475     printf("TestAddRectToBand: wrong number of rects (#5): %i\n", bandsInfo.bands[0].numRects);
00476     return PR_FALSE;
00477   }
00478   bandRect = bandsInfo.bands[0].firstRect;
00479   NS_ASSERTION(1 == bandRect->mNumFrames, "unexpected shared rect");
00480   if ((bandRect->mLeft != 10) || (bandRect->mRight != 20)) {
00481     printf("TestAddRectToBand: wrong first rect (#5)\n");
00482     return PR_FALSE;
00483   }
00484   bandRect = bandRect->Next();
00485   if ((bandRect->mLeft != 20) || (bandRect->mRight != 40) ||
00486       (bandRect->mNumFrames != 2) || !bandRect->IsOccupiedBy((nsIFrame*)0x06)) {
00487     printf("TestAddRectToBand: wrong second rect (#5)\n");
00488     return PR_FALSE;
00489   }
00490   bandRect = bandRect->Next();
00491   NS_ASSERTION(1 == bandRect->mNumFrames, "unexpected shared rect");
00492   if ((bandRect->mLeft != 40) || (bandRect->mRight != 50)) {
00493     printf("TestAddRectToBand: wrong third rect (#5)\n");
00494     return PR_FALSE;
00495   }
00496   bandRect = bandRect->Next();
00497   if ((bandRect->mLeft != 50) || (bandRect->mRight != 60) || (bandRect->mNumFrames != 2)) {
00498     printf("TestAddRectToBand: wrong fourth rect (#5)\n");
00499     return PR_FALSE;
00500   }
00501 
00503   // #6. Add a new rect that completely contains an existing rect
00504   status = AddRectRegion((nsIFrame*)0x07, nsRect(0, 100, 30, 100));
00505   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00506   GetBandsInfo(bandsInfo);
00507   NS_ASSERTION(bandsInfo.numBands == 1, "wrong number of bands");
00508   if (bandsInfo.bands[0].numRects != 11) {
00509     printf("TestAddRectToBand: wrong number of rects (#6): %i\n", bandsInfo.bands[0].numRects);
00510     return PR_FALSE;
00511   }
00512   bandRect = bandsInfo.bands[0].firstRect;
00513   NS_ASSERTION(1 == bandRect->mNumFrames, "unexpected shared rect");
00514   if ((bandRect->mLeft != 0) || (bandRect->mRight != 10)) {
00515     printf("TestAddRectToBand: wrong first rect (#6)\n");
00516     return PR_FALSE;
00517   }
00518   bandRect = bandRect->Next();
00519   if ((bandRect->mLeft != 10) || (bandRect->mRight != 20) ||
00520       (bandRect->mNumFrames != 2) || !bandRect->IsOccupiedBy((nsIFrame*)0x07)) {
00521     printf("TestAddRectToBand: wrong second rect (#6)\n");
00522     return PR_FALSE;
00523   }
00524   bandRect = bandRect->Next();
00525   if ((bandRect->mLeft != 20) || (bandRect->mRight != 30) ||
00526       (bandRect->mNumFrames != 3) || !bandRect->IsOccupiedBy((nsIFrame*)0x07)) {
00527     printf("TestAddRectToBand: wrong third rect (#6)\n");
00528     return PR_FALSE;
00529   }
00530   bandRect = bandRect->Next();
00531   if ((bandRect->mLeft != 30) || (bandRect->mRight != 40) || (bandRect->mNumFrames != 2)) {
00532     printf("TestAddRectToBand: wrong fourth rect (#6)\n");
00533     return PR_FALSE;
00534   }
00535   bandRect = bandRect->Next();
00536   NS_ASSERTION(1 == bandRect->mNumFrames, "unexpected shared rect");
00537   if ((bandRect->mLeft != 40) || (bandRect->mRight != 50)) {
00538     printf("TestAddRectToBand: wrong fifth rect (#6)\n");
00539     return PR_FALSE;
00540   }
00541 
00542   return PR_TRUE;
00543 }
00544 
00545 // Test of removing regions. We especially need to test that we correctly
00546 // coalesce adjacent rects and bands
00547 //
00548 // This tests the following:
00549 // 1. simple test of removing the one and only band rect
00550 // 2. removing a shared rect and verifying adjacent rects are coalesced
00551 // 3. removing a band rect and making sure adjacent bands are combined
00552 PRBool MySpaceManager::TestRemoveRegion()
00553 {
00554   BandsInfo bandsInfo;
00555   BandRect* bandRect;
00556   nsresult  status;
00557 
00558   // Clear any existing regions
00559   ClearRegions();
00560   NS_ASSERTION(mBandList.IsEmpty(), "clear regions failed");
00561 
00563   // #1. A simple test of removing the one and only band rect
00564   status = AddRectRegion((nsIFrame*)0x01, nsRect(10, 100, 100, 100));
00565   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00566   status = RemoveRegion((nsIFrame*)0x01);
00567   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00568   GetBandsInfo(bandsInfo);
00569   if (bandsInfo.numBands != 0) {
00570     printf("TestRemoveRegion: wrong number of bands (#1): %i\n", bandsInfo.numBands);
00571     return PR_FALSE;
00572   }
00573 
00575   // #2. Test removing a rect that's shared. Make sure adjacent rects are
00576   // coalesced
00577   status = AddRectRegion((nsIFrame*)0x01, nsRect(10, 100, 100, 100));
00578   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00579   status = AddRectRegion((nsIFrame*)0x02, nsRect(40, 100, 20, 100));
00580   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00581 
00582   // Verify there are three rects in the band
00583   GetBandsInfo(bandsInfo);
00584   if (bandsInfo.bands[0].numRects != 3) {
00585     printf("TestRemoveRegion: wrong number of rects (#2): %i\n", bandsInfo.bands[0].numRects);
00586     return PR_FALSE;
00587   }
00588 
00589   // Remove the region associated with the second frame
00590   status = RemoveRegion((nsIFrame*)0x02);
00591   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00592   GetBandsInfo(bandsInfo);
00593   if (bandsInfo.bands[0].numRects != 1) {
00594     printf("TestRemoveRegion: failed to coalesce adjacent rects (#2)\n");
00595     return PR_FALSE;
00596   }
00597   bandRect = bandsInfo.bands[0].firstRect;
00598   if ((bandRect->mLeft != 10) || (bandRect->mRight != 110)) {
00599     printf("TestRemoveRegion: wrong size rect (#2)\n");
00600     return PR_FALSE;
00601   }
00602 
00604   // #3. Test removing a band rect and making sure adjacent bands are combined
00605   status = AddRectRegion((nsIFrame*)0x02, nsRect(10, 140, 20, 20));
00606   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00607 
00608   // Verify there are three bands and that each band has three rects
00609   GetBandsInfo(bandsInfo);
00610   if (bandsInfo.numBands != 3) {
00611     printf("TestRemoveRegion: wrong number of bands (#3): %i\n", bandsInfo.numBands);
00612     return PR_FALSE;
00613   }
00614   if (bandsInfo.bands[0].numRects != 1) {
00615     printf("TestRemoveRegion: band #1 wrong number of rects (#3): %i\n", bandsInfo.bands[0].numRects);
00616     return PR_FALSE;
00617   }
00618   if (bandsInfo.bands[1].numRects != 2) {
00619     printf("TestRemoveRegion: band #2 wrong number of rects (#3): %i\n", bandsInfo.bands[1].numRects);
00620     return PR_FALSE;
00621   }
00622   if (bandsInfo.bands[2].numRects != 1) {
00623     printf("TestRemoveRegion: band #3 wrong number of rects (#3): %i\n", bandsInfo.bands[2].numRects);
00624     return PR_FALSE;
00625   }
00626 
00627   // Remove the region associated with the second frame
00628   status = RemoveRegion((nsIFrame*)0x02);
00629   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00630   GetBandsInfo(bandsInfo);
00631   if (bandsInfo.bands[0].numRects != 1) {
00632     printf("TestRemoveRegion: failed to coalesce adjacent rects (#3)\n");
00633     return PR_FALSE;
00634   }
00635   bandRect = bandsInfo.bands[0].firstRect;
00636   if ((bandRect->mLeft != 10) || (bandRect->mRight != 110)) {
00637     printf("TestRemoveRegion: wrong size rect (#3)\n");
00638     return PR_FALSE;
00639   }
00640 
00641   return PR_TRUE;
00642 }
00643 
00644 // Test of getting the band data
00645 PRBool MySpaceManager::TestGetBandData()
00646 {
00647   nsresult  status;
00648   nscoord   yMost;
00649 
00650   // Clear any existing regions
00651   ClearRegions();
00652   NS_ASSERTION(mBandList.IsEmpty(), "clear regions failed");
00653 
00654   // Make sure YMost() returns the correct result
00655   if (NS_ERROR_ABORT != YMost(yMost)) {
00656     printf("TestGetBandData: YMost() returned wrong result (#1)\n");
00657     return PR_FALSE;
00658   }
00659 
00660   // Make a band with three rects
00661   status = AddRectRegion((nsIFrame*)0x01, nsRect(100, 100, 100, 100));
00662   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00663 
00664   status = AddRectRegion((nsIFrame*)0x02, nsRect(300, 100, 100, 100));
00665   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00666 
00667   status = AddRectRegion((nsIFrame*)0x03, nsRect(500, 100, 100, 100));
00668   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00669 
00670   // Verify that YMost() is correct
00671   if ((NS_OK != YMost(yMost)) || (yMost != 200)) {
00672     printf("TestGetBandData: YMost() returned wrong value (#2)\n");
00673     return PR_FALSE;
00674   }
00675 
00676   // Get the band data using a very large clip rect and a band data struct
00677   // that's large enough
00678   nsBandData      bandData;
00679   nsBandTrapezoid trapezoids[16];
00680   bandData.mSize = 16;
00681   bandData.mTrapezoids = trapezoids;
00682   status = GetBandData(100, nsSize(10000,10000), bandData);
00683   NS_ASSERTION(NS_SUCCEEDED(status), "unexpected status");
00684 
00685   // Verify that there are seven trapezoids
00686   if (bandData.mCount != 7) {
00687     printf("TestGetBandData: wrong trapezoid count (#3)\n");
00688     return PR_FALSE;
00689   }
00690   
00691   // Get the band data using a very large clip rect and a band data struct
00692   // that's too small
00693   bandData.mSize = 3;
00694   status = GetBandData(100, nsSize(10000,10000), bandData);
00695   if (NS_SUCCEEDED(status)) {
00696     printf("TestGetBandData: ignored band data count (#4)\n");
00697     return PR_FALSE;
00698   }
00699 
00700   // Make sure the count has been updated to reflect the number of trapezoids
00701   // required
00702   if (bandData.mCount <= bandData.mSize) {
00703     printf("TestGetBandData: bad band data count (#5)\n");
00704     return PR_FALSE;
00705   }
00706 
00707   // XXX We need lots more tests here...
00708 
00709   return PR_TRUE;
00710 }
00711 
00713 //
00714 
00715 int main(int argc, char** argv)
00716 {
00717   // Create a space manager
00718   MySpaceManager* spaceMgr = new MySpaceManager(nsnull, nsnull);
00719   
00720   // Test adding rect regions
00721   if (!spaceMgr->TestAddBand()) {
00722     delete spaceMgr;
00723     return -1;
00724   }
00725 
00726   // Test adding rect regions that overlap existing bands
00727   if (!spaceMgr->TestAddBandOverlap()) {
00728     delete spaceMgr;
00729     return -1;
00730   }
00731 
00732   // Test adding rects within an existing band
00733   if (!spaceMgr->TestAddRectToBand()) {
00734     delete spaceMgr;
00735     return -1;
00736   }
00737 
00738   // Test removing regions
00739   if (!spaceMgr->TestRemoveRegion()) {
00740     return -1;
00741   }
00742 
00743   // Test getting the band data
00744   if (!spaceMgr->TestGetBandData()) {
00745     return -1;
00746   }
00747 
00748   delete spaceMgr;
00749   return 0;
00750 }