tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

translator-locale.https.window.js (4089B)


      1 // META: title=Translator locale tests
      2 // META: global=window
      3 // META: timeout=long
      4 // META: script=resources/util.js
      5 // META: script=/resources/testdriver.js
      6 // META: script=../resources/util.js
      7 // META: script=../resources/locale-util.js
      8 
      9 'use strict';
     10 
     11 function getAvailability(sourceLanguage, targetLanguage) {
     12  return Translator.availability({sourceLanguage, targetLanguage});
     13 }
     14 
     15 promise_test(async t => {
     16  for (const [sourceLanguageSubtag, sourceVariations] of Object.entries(
     17           valid_language_tags)) {
     18    for (const [targetLanguageSubtag, targetVariations] of Object.entries(
     19             valid_language_tags)) {
     20      const languageSubtagAvailability =
     21          await getAvailability(sourceLanguageSubtag, targetLanguageSubtag);
     22 
     23      // All variations should be consistent with the language subtag.
     24      for (const sourceVariation of sourceVariations) {
     25        for (const targetVariation of targetVariations) {
     26          assert_availability_consistent(
     27              await getAvailability(sourceVariation, targetVariation),
     28              languageSubtagAvailability);
     29        }
     30      }
     31    }
     32  }
     33 }, 'Translator.availability() is consistent between language tag variations');
     34 
     35 async function assert_valid_languages(inSourceLanguage, inTargetLanguage) {
     36  if (['downloading', 'downloadable'].includes(
     37          await getAvailability(inSourceLanguage, inTargetLanguage))) {
     38    await test_driver.bless();
     39  }
     40 
     41  const {sourceLanguage: outSourceLanguage, targetLanguage: outTargetLanguage} =
     42      await Translator.create(
     43          {sourceLanguage: inSourceLanguage, targetLanguage: inTargetLanguage});
     44 
     45  assert_is_variation(inSourceLanguage, outSourceLanguage);
     46  assert_is_canonical(outSourceLanguage);
     47  assert_is_variation(inTargetLanguage, outTargetLanguage);
     48  assert_is_canonical(outTargetLanguage);
     49 }
     50 
     51 promise_test(async t => {
     52  for (const [sourceLanguageSubtag, sourceVariations] of Object.entries(
     53           valid_language_tags)) {
     54    for (const [targetLanguageSubtag, targetVariations] of Object.entries(
     55             valid_language_tags)) {
     56      if (await getAvailability(sourceLanguageSubtag, targetLanguageSubtag) ===
     57          'unavailable') {
     58        continue;
     59      }
     60 
     61      await assert_valid_languages(sourceLanguageSubtag, targetLanguageSubtag);
     62 
     63      for (const sourceVariation of sourceVariations) {
     64        for (const targetVariation of targetVariations) {
     65          await assert_valid_languages(sourceVariation, targetVariation);
     66        }
     67      }
     68    }
     69  }
     70 }, 'Translator has valid source and target languages');
     71 
     72 function assert_rejects_invalid_languages(
     73    t, method, sourceLanguage, targetLanguage) {
     74  return promise_rejects_js(
     75      t, RangeError, method({sourceLanguage, targetLanguage}));
     76 }
     77 
     78 function testInvalidLanguagePairs(t, method) {
     79  const allValidLanguageTags = Object.values(valid_language_tags).flat();
     80  // Invalid source language.
     81  for (const sourceLanguage of invalid_language_tags) {
     82    for (const targetLanguage of allValidLanguageTags) {
     83      assert_rejects_invalid_languages(
     84          t, method, sourceLanguage, targetLanguage);
     85    }
     86  }
     87  // Invalid target language.
     88  for (const sourceLanguage of allValidLanguageTags) {
     89    for (const targetLanguage of invalid_language_tags) {
     90      assert_rejects_invalid_languages(
     91          t, method, sourceLanguage, targetLanguage);
     92    }
     93  }
     94  // Invalid source and target language
     95  for (const sourceLanguage of invalid_language_tags) {
     96    for (const targetLanguage of invalid_language_tags) {
     97      assert_rejects_invalid_languages(
     98          t, method, sourceLanguage, targetLanguage);
     99    }
    100  }
    101 }
    102 
    103 promise_test(async t => {
    104  // We don't need to consume user activation since it should throw a RangeError
    105  // before it even can check if it needs to consume user activation.
    106  testInvalidLanguagePairs(t, Translator.create);
    107 }, 'Translator.create() throws RangeError for invalid language tags');
    108 
    109 promise_test(async t => {
    110  testInvalidLanguagePairs(t, Translator.availability);
    111 }, 'Translator.availability() throws RangeError for invalid language tags');