Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/modules/libpref/test/unit/   (Browser von der Mozilla Stiftung Version 136.0.1©)  Datei vom 10.2.2025 mit Größe 4 kB image not shown  

Quelle  test_parsePrefs.js   Sprache: JAVA

 
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/licenses/publicdomain/  */


"use strict";

// // This undoes some of the configuration from
// Services.dirsvc
//   .QueryInterface(Ci.nsIDirectoryService)
//   .unregisterProvider(provider);

class PrefObserver {
  constructor() {
    this.events = [];
  }

  onStringPref(...args) {
    // // What happens with an exception here?
    // throw new Error(`foo ${args[1]}`);
    this.events.push(["string", ...args]);
  }

  onIntPref(...args) {
    this.events.push(["int", ...args]);
  }

  onBoolPref(...args) {
    this.events.push(["bool", ...args]);
  }

  onError(message) {
    this.events.push(["error", message]);
  }
}

const TESTS = {
  "data/testPrefSticky.js": [
    ["bool""Default""testPref.unsticky.bool"truefalsefalse],
    ["bool""Default""testPref.sticky.bool"falsetruefalse],
  ],
  "data/testPrefStickyUser.js": [
    ["bool""User""testPref.sticky.bool"falsefalsefalse],
  ],
  "data/testPrefLocked.js": [
    ["int""Default""testPref.unlocked.int", 333, falsefalse],
    ["int""Default""testPref.locked.int", 444, falsetrue],
  ],
  // data/testPrefLockedUser is ASCII.
  "data/testPrefLockedUser.js": [
    ["int""User""testPref.locked.int", 555, falsefalse],
  ],
  // data/testPref is ISO-8859.
  "data/testPref.js": [
    ["bool""User""testPref.bool1"truefalsefalse],
    ["bool""User""testPref.bool2"falsefalsefalse],
    ["int""User""testPref.int1", 23, falsefalse],
    ["int""User""testPref.int2", -1236, falsefalse],
    ["string""User""testPref.char1""_testPref"falsefalse],
    ["string""User""testPref.char2""älskar"falsefalse],
  ],
  // data/testParsePrefs is data/testPref.js as UTF-8.
  "data/testPrefUTF8.js": [
    ["bool""User""testPref.bool1"truefalsefalse],
    ["bool""User""testPref.bool2"falsefalsefalse],
    ["int""User""testPref.int1", 23, falsefalse],
    ["int""User""testPref.int2", -1236, falsefalse],
    ["string""User""testPref.char1""_testPref"falsefalse],
    // Observe that this is the ISO-8859/Latin-1 encoding of the UTF-8 bytes.
    // (Note that this source file is encoded as UTF-8.)  This appears to just
    // be how libpref handles this case.  This test serves as documentation of
    // this infelicity.
    ["string""User""testPref.char2""älskar"falsefalse],
  ],
};

add_task(async function test_success() {
  for (let [path, expected] of Object.entries(TESTS)) {
    let prefObserver = new PrefObserver();

    let prefsFile = do_get_file(path);
    let data = await IOUtils.read(prefsFile.path);

    Services.prefs.parsePrefsFromBuffer(data, prefObserver, path);
    Assert.deepEqual(
      prefObserver.events,
      expected,
      `Observations from ${path} are as expected`
    );
  }
});

add_task(async function test_exceptions() {
  const { AddonTestUtils } = ChromeUtils.importESModule(
    "resource://testing-common/AddonTestUtils.sys.mjs"
  );

  let s = `user_pref("testPref.bool1"true);
  user_pref("testPref.bool2"false);
  user_pref("testPref.int1", 23);
  user_pref("testPref.int2", -1236);
  `;

  let onErrorCount = 0;
  let addPrefCount = 0;

  let marker = "2fc599a1-433b-4de7-bd63-3e69c3bbad5b";
  let addPref = (...args) => {
    addPrefCount += 1;
    throw new Error(`${marker}${JSON.stringify(args)}${marker}`);
  };

  let { messages } = await AddonTestUtils.promiseConsoleOutput(() => {
    Services.prefs.parsePrefsFromBuffer(new TextEncoder().encode(s), {
      onStringPref: addPref,
      onIntPref: addPref,
      onBoolPref: addPref,
      onError(message) {
        onErrorCount += 1;
        console.error(message);
      },
    });
  });

  Assert.equal(addPrefCount, 4);
  Assert.equal(onErrorCount, 0);

  // This is fragile but mercifully simple.
  Assert.deepEqual(
    messages.map(m => JSON.parse(m.message.split(marker)[1])),
    [
      ["User""testPref.bool1"truefalsefalse],
      ["User""testPref.bool2"falsefalsefalse],
      ["User""testPref.int1", 23, falsefalse],
      ["User""testPref.int2", -1236, falsefalse],
    ]
  );
});

98%


¤ Dauer der Verarbeitung: 0.2 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung ist noch experimentell.