<?xml version="1.0"?>

<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<!-- Bug 466422:
   - Check that we replace the left pane with a correct one if it gets corrupted
   - and we end up having more than one. -->

<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
                 type="text/css"?>

<?xml-stylesheet href="chrome://communicator/content/places/places.css"?>
<?xml-stylesheet href="chrome://communicator/skin/places/places.css"?>

<?xul-overlay href="chrome://communicator/content/bookmarks/placesOverlay.xul"?>

<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        title="Test handling of multiple left pane folders"
        onload="runTest();">

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

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

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

    function runTest() {
      // Sanity checks.
      ok(PlacesUtils, "PlacesUtils is running in chrome context");
      ok(PlacesUIUtils, "PlacesUIUtils is running in chrome context");
      ok(PlacesUIUtils.ORGANIZER_LEFTPANE_VERSION > 0,
         "Left pane version in chrome context, " +
           "current version is: " + PlacesUIUtils.ORGANIZER_LEFTPANE_VERSION );

      var fakeLeftPanes = [];
      var as = PlacesUtils.annotations;
      var bs = PlacesUtils.bookmarks;

      // We need 2 left pane folders to simulate a corrupt profile.
      do {
        let leftPaneItems = as.getItemsWithAnnotation(PlacesUIUtils.ORGANIZER_FOLDER_ANNO);
        // Create a fake left pane folder.
        let fakeLeftPaneRoot = bs.createFolder(PlacesUtils.placesRootId, "",
                                               bs.DEFAULT_INDEX);
        as.setItemAnnotation(fakeLeftPaneRoot, PlacesUIUtils.ORGANIZER_FOLDER_ANNO,
                             PlacesUIUtils.ORGANIZER_LEFTPANE_VERSION, 0,
                             as.EXPIRE_NEVER);
        fakeLeftPanes.push(fakeLeftPaneRoot);
      } while (fakeLeftPanes.length < 2);

      // Initialize the left pane queries.
      PlacesUIUtils.leftPaneFolderId;

      // Check left pane.
      ok(PlacesUIUtils.leftPaneFolderId > 0,
         "Left pane folder correctly created");
      var leftPaneItems = as.getItemsWithAnnotation(PlacesUIUtils.ORGANIZER_FOLDER_ANNO);
      is(leftPaneItems.length, 1,
         "We correctly have only 1 left pane folder");

      // Check that all old left pane items have been removed.
      fakeLeftPanes.forEach(function(aItemId) {
        try {
          bs.getItemTitle(aItemId);
          throw("This folder should have been removed");
        } catch (ex) {}
      });

    }
  ]]>
  </script>

</window>
