<?xml version="1.0"?>
<!--
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is
 * Mozilla Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2008
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Shawn Wilsher <me@shawnwilsher.com> (Original Author)
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

/**
 * This tests that the performance of deleting a large number of bookmarks.  See
 * bug 432706 for more details.
 *
 * To run this test, run it like you would a standard chrome test, with the test
 * path being:
 * ../chrome/browser/components/places/tests/perf/perf_large_delete.xul
 */
-->

<window title="Places Library Performance Test"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        onload="test();">

  <script type="application/javascript"
          src="chrome://mochikit/content/MochiKit/packed.js"/>
  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>

  <script type="application/javascript">
  <![CDATA[

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;

function test()
{
  let ios = Cc["@mozilla.org/network/io-service;1"].
            getService(Ci.nsIIOService);

  // First, add 250 bookmarks
  let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
           getService(Ci.nsINavBookmarksService);
  bs.runInBatchMode({
    runBatched: function(aUserData)
    {
      for (let i = 0; i < 250; i++) {
        let uri = ios.newURI("http://example.com/" + i, null, null);
        bs.insertBookmark(bs.unfiledBookmarksFolder, uri, bs.DEFAULT_INDEX,
                          "bookmark " + i);
      }
    }
  }, null);

  // Close the window if it's already open
  let wm = Cc["@mozilla.org/appshell/window-mediator;1"].
           getService(Ci.nsIWindowMediator);
  let win = wm.getMostRecentWindow("Places:Orgainzier");
  if (win)
    win.close();


  let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
           getService(Ci.nsIWindowWatcher);
  ww.registerNotification({
    observe: function(aSubject, aTopic, aData)
    {
      if (aTopic != "domwindowopened")
        return;

      let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);

      // We need to run after the window is loaded, and has run its startup code
      win.addEventListener("DOMContentLoaded", function() {
        let tm = Cc["@mozilla.org/thread-manager;1"].
                 getService(Ci.nsIThreadManager);
        tm.mainThread.dispatch({
          run: function() doTest(win.QueryInterface(Ci.nsIDOMWindow))
        }, Ci.nsIThread.DISPATCH_NORMAL);
      }, false);

      // and now to unregister ourself as a listener
      ww.unregisterNotification(this);
    }
  });

  function doTest(aWin)
  {
    // Select all the bookmarks
    synthesizeKey("a", {accelKey:true}, aWin);

    // If we can, start shark
    try {
      connectShark();
      startShark();
    }
    catch (e) { }

    // Delete the bookmarks
    let start = Date.now();
    synthesizeKey("VK_BACK_SPACE", {}, aWin);
    let end = Date.now();

    // Stop shark, if we can
    try {
      stopShark();
      disconnectionShark();
    }
    catch (e) { }

    // Close the window, and write the results out
    aWin.close();
    document.getElementById("test-result").value =
      "Duration was " + String(end - start) + "ms";

    SimpleTest.finish();
  }

  // And open the window
  wm.getMostRecentWindow("navigator:browser")
    .openDialog("chrome://browser/content/places/places.xul",
                "", "chrome,toolbar=yes,dialog=no,resizable",
                "UnfiledBookmarks");

  SimpleTest.waitForExplicitFinish();
}

  ]]>
  </script>

  <body xmlns="http://www.w3.org/1999/xhtml">
    <p id="display"></p>
    <div id="content" style="display:none;"></div>
    <pre id="test"></pre>
  </body>
  <label id="test-result"/>
</window>
