/**
 * Basic teardown function for tests.
 *
 * @memberOf head
 * @param {object} aModule
 *        Test module to teardown
 * @param {Object} [aFlags={}]
 *        Flags for skipping portions of the teardown process. Use these for tests
 *        that leave state for subsequent tests. (restart tests, primarily)
 * @param {Boolean} [aFlags.skipResetTabs=false]
 *        Skip the resetting of tabs to about:blank
 * @param {Boolean} [aFlags.skipRestoreBookmarks=false]
 *        Skip restoring bookmarks to default
 * @param {Boolean} [aFlags.skipClearHistory=false]
 *        Skip clearing page history
 */
function teardown(aModule, aFlags) {
  aFlags = aFlags || {};

  // Remove all history after the test
  if (!aFlags.skipClearHistory)
    places.removeAllHistory();

  // Restore the default bookmarks after the test
  if (!aFlags.skipRestoreBookmarks)
    places.restoreDefaultBookmarks();

  // Reset tabs to known state after the test
  if (!aFlags.skipResetTabs)
    aModule.browser.resetTabs();

  // Clean-up the open browser window (i.e. modal observer)
  aModule.browser.destroy();
}
/**
 * @name module
 * @namespace Augmented features into the Mozmill module scope
 */


/**
 * Basic setup function for tests.
 *
 * @memberOf head
 * @param {object} aModule
 *        Test module to initialize
 * @param {Object} [aFlags={}]
 *        Flags for skipping portions of the setup process. Use these for tests
 *        that count on the Firefox default state, or which inherit state from
 *        previous tests (restart tests, primarily)
 * @param {Boolean} [aFlags.skipResetTabs=false]
 *        Skip the resetting of tabs to about:blank
 * @param {Boolean} [aFlags.skipRestoreBookmarks=false]
 *        Skip restoring bookmarks to default
 * @param {Boolean} [aFlags.skipClearHistory=false]
 *        Skip clearing page history
 */
function setup(aModule, aFlags) {
  aFlags = aFlags || {};

  /**
   * The Mozmill driver for handling global actions
   *
   * @name driver
   * @type driver
   * @memberOf module
   */
  aModule.driver = require("api/driver");

  /**
   * Instance of the Assert class to execute tests with fatal assertions
   *
   * @name assert
   * @type assertions.Assert
   * @memberOf module
   */
  aModule.assert = new assertions.Assert();

  /**
   * Instance of the Expect class to execute tests with non-fatal assertions
   *
   * @name expect
   * @type assertions.Expect
   * @memberOf module
   */
  aModule.expect = new assertions.Expect();

  /**
   * Helper method to get the top-most browser window.
   *
   * @name getBrowserWindow
   * @type browser.getBrowserWindow
   * @memberOf module
   */
  aModule.getBrowserWindow = require("ui/browser").getBrowserWindow;

  /**
   * Helper method to open a new browser window.
   *
   * @name openBrowserWindow
   * @type browser.openBrowserWindow
   * @memberOf module
   */
  aModule.openBrowserWindow = require("ui/browser").openBrowserWindow;

  /**
   * Get the default browser window or create a new one.
   */
  aModule.browser = aModule.getBrowserWindow();

  // Set tabs to known state before the test. This is necessary as the first
  // launch will launch with startup pages, the rest won't have them.
  if (!aFlags.skipResetTabs)
    aModule.browser.resetTabs();

  // Restore default bookmarks before the test in case last didn't tear down right.
  // This is documented in places.js as a longish operation, but seems very
  // short in practice. If this becomes too much a drag, it can be removed with some risk.
  if (!aFlags.skipRestoreBookmarks)
    places.restoreDefaultBookmarks();

  // Remove all history before the test in case last didn't tear down right
  if (!aFlags.skipRemoveHistory)
    places.removeAllHistory();
}