text: /* Any copyright is dedicated to the Public Domain.
   * http://creativecommons.org/publicdomain/zero/1.0/
   */
  
  /* Application in use complete MAR file background patch apply failure test */
  
  const TEST_ID = "0161";
  
  // The files are listed in the same order as they are applied from the mar's
  // update.manifest. Complete updates have remove file and rmdir directory
  // operations located in the precomplete file performed first.
  const TEST_FILES = [
  {
    description      : "Should never change",
    fileName         : "channel-prefs.js",
    relPathDir       : "a/b/defaults/pref/",
    originalContents : "ShouldNotBeReplaced\n",
    compareContents  : "ShouldNotBeReplaced\n",
    originalFile     : null,
    compareFile      : null
  }, {
    description      : "Not added for failed update (add)",
    fileName         : "precomplete",
    relPathDir       : "",
    originalContents : null,
    compareContents  : null,
    originalFile     : "data/partial_precomplete",
    compareFile      : "data/partial_precomplete"
  }, {
    description      : "Not added for failed update (add)",
    fileName         : "searchpluginstext0",
    relPathDir       : "a/b/searchplugins/",
    originalContents : "ShouldNotBeReplaced\n",
    compareContents  : "ShouldNotBeReplaced\n",
    originalFile     : null,
    compareFile      : null
  }, {
    description      : "Not added for failed update (add)",
    fileName         : "searchpluginspng1.png",
    relPathDir       : "a/b/searchplugins/",
    originalContents : null,
    compareContents  : null,
    originalFile     : "data/partial.png",
    compareFile      : "data/partial.png"
  }, {
    description      : "Not added for failed update (add)",
    fileName         : "searchpluginspng0.png",
    relPathDir       : "a/b/searchplugins/",
    originalContents : null,
    compareContents  : null,
    originalFile     : "data/partial.png",
    compareFile      : "data/partial.png"
  }, {
    description      : "Not added for failed update (add)",
    fileName         : "removed-files",
    relPathDir       : "a/b/",
    originalContents : null,
    compareContents  : null,
    originalFile     : "data/partial_removed-files",
    compareFile      : "data/partial_removed-files"
  }, {
    description      : "Not added for failed update (add-if)",
    fileName         : "extensions1text0",
    relPathDir       : "a/b/extensions/extensions1/",
    originalContents : null,
    compareContents  : null,
    originalFile     : null,
    compareFile      : null
  }, {
    description      : "Not added for failed update (add-if)",
    fileName         : "extensions1png1.png",
    relPathDir       : "a/b/extensions/extensions1/",
    originalContents : null,
    compareContents  : null,
    originalFile     : null,
    compareFile      : null
  }, {
    description      : "Not added for failed update (add-if)",
    fileName         : "extensions1png0.png",
    relPathDir       : "a/b/extensions/extensions1/",
    originalContents : null,
    compareContents  : null,
    originalFile     : null,
    compareFile      : null
  }, {
    description      : "Not added for failed update (add-if)",
    fileName         : "extensions0text0",
    relPathDir       : "a/b/extensions/extensions0/",
    originalContents : "ShouldNotBeReplaced\n",
    compareContents  : "ShouldNotBeReplaced\n",
    originalFile     : null,
    compareFile      : null
  }, {
    description      : "Not added for failed update (add-if)",
    fileName         : "extensions0png1.png",
    relPathDir       : "a/b/extensions/extensions0/",
    originalContents : null,
    compareContents  : null,
    originalFile     : "data/partial.png",
    compareFile      : "data/partial.png"
  }, {
    description      : "Not added for failed update (add-if)",
    fileName         : "extensions0png0.png",
    relPathDir       : "a/b/extensions/extensions0/",
    originalContents : null,
    compareContents  : null,
    originalFile     : null,
    compareFile      : null
  }, {
    description      : "Not added for failed update (add)",
    fileName         : "exe0.exe",
    relPathDir       : "a/b/",
    originalContents : null,
    compareContents  : null,
    originalFile     : "data/partial.png",
    compareFile      : "data/partial.png"
  }, {
    description      : "Not added for failed update (add)",
    fileName         : "10text0",
    relPathDir       : "a/b/1/10/",
    originalContents : "ShouldNotBeReplaced\n",
    compareContents  : "ShouldNotBeReplaced\n",
    originalFile     : null,
    compareFile      : null
  }, {
    description      : "Not added for failed update (add)",
    fileName         : "0exe0.exe",
    relPathDir       : "a/b/0/",
    originalContents : null,
    compareContents  : null,
    originalFile     : null,
    compareFile      : null
  }, {
    description      : "Not added for failed update (add)",
    fileName         : "00text1",
    relPathDir       : "a/b/0/00/",
    originalContents : "ShouldNotBeReplaced\n",
    compareContents  : "ShouldNotBeReplaced\n",
    originalFile     : null,
    compareFile      : null
  }, {
    description      : "Not added for failed update (add)",
    fileName         : "00text0",
    relPathDir       : "a/b/0/00/",
    originalContents : "ShouldNotBeReplaced\n",
    compareContents  : "ShouldNotBeReplaced\n",
    originalFile     : null,
    compareFile      : null
  }, {
    description      : "Not added for failed update (add)",
    fileName         : "00png0.png",
    relPathDir       : "a/b/0/00/",
    originalContents : null,
    compareContents  : null,
    originalFile     : "data/partial.png",
    compareFile      : "data/partial.png"
  }, {
    description      : "Not removed for failed update (remove)",
    fileName         : "20text0",
    relPathDir       : "a/b/2/20/",
    originalContents : "ShouldNotBeDeleted\n",
    compareContents  : "ShouldNotBeDeleted\n",
    originalFile     : null,
    compareFile      : null
  }, {
    description      : "Not removed for failed update (remove)",
    fileName         : "20png0.png",
    relPathDir       : "a/b/2/20/",
    originalContents : "ShouldNotBeDeleted\n",
    compareContents  : "ShouldNotBeDeleted\n",
    originalFile     : null,
    compareFile      : null
  }];
  
  ADDITIONAL_TEST_DIRS = [
  {
    description  : "Not removed for failed update (rmdir)",
    relPathDir   : "a/b/2/20/",
    dirRemoved   : false
  }, {
    description  : "Not removed for failed update (rmdir)",
    relPathDir   : "a/b/2/",
    dirRemoved   : false
  }];
  
  function run_test() {
    do_test_pending();
    do_register_cleanup(cleanupUpdaterTest);
  
    gBackgroundUpdate = true;
    setupUpdaterTest(MAR_COMPLETE_FILE);
  
    // Launch the callback helper application so it is in use during the update
    let callbackApp = getApplyDirFile("a/b/" + gCallbackBinFile);
    let args = [getApplyDirPath() + "a/b/", "input", "output", "-s", "40"];
    let callbackAppProcess = AUS_Cc["@mozilla.org/process/util;1"].
                             createInstance(AUS_Ci.nsIProcess);
    callbackAppProcess.init(callbackApp);
    callbackAppProcess.run(false, args, args.length);
  
    do_timeout(TEST_HELPER_TIMEOUT, waitForHelperSleep);
  }
  
  function doUpdate() {
    // apply the complete mar
    let exitValue = runUpdate();
    logTestInfo("testing updater binary process exitValue for success when " +
                "applying a complete mar");
    do_check_eq(exitValue, 0);
  
    logTestInfo("testing update.status should be " + STATE_APPLIED);
    let updatesDir = do_get_file(TEST_ID + UPDATES_DIR_SUFFIX);
    do_check_eq(readStatusFile(updatesDir), STATE_APPLIED);
  
    // Now switch the application and its updated version
    gBackgroundUpdate = false;
    gSwitchApp = true;
    gDisableReplaceFallback = true;
    exitValue = runUpdate();
    logTestInfo("testing updater binary process exitValue for failure when " +
                "switching to the updated application");
    do_check_eq(exitValue, 1);
  
    setupHelperFinish();
  }
  
  function checkUpdate() {
    logTestInfo("testing update.status should be " + STATE_FAILED);
    let updatesDir = do_get_file(TEST_ID + UPDATES_DIR_SUFFIX);
    // The update status format for a failure is failed: # where # is the error
    // code for the failure.
    do_check_eq(readStatusFile(updatesDir).split(": ")[0], STATE_FAILED);
  
    checkFilesAfterUpdateFailure(getApplyDirFile);
    checkUpdateLogContains(ERR_RENAME_FILE);
  
    logTestInfo("testing tobedeleted directory doesn't exist");
    let toBeDeletedDir = getApplyDirFile("tobedeleted", true);
    do_check_false(toBeDeletedDir.exists());
  
    checkCallbackAppLog();
  }
  