tor-browser

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

test_UrlbarResult_getDisplayableValueAndHighlights.js (5428B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 add_task(function type_typed() {
      7  let queryContext = createContext("test");
      8  let result = new UrlbarResult({
      9    type: UrlbarUtils.RESULT_TYPE.URL,
     10    source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
     11    payload: {
     12      url: "https://test.example.com/",
     13    },
     14    highlights: {
     15      url: UrlbarUtils.HIGHLIGHT.TYPED,
     16    },
     17  });
     18 
     19  doTest({
     20    result,
     21    target: "url",
     22    options: { tokens: queryContext.tokens },
     23    expected: {
     24      value: "https://test.example.com/",
     25      highlights: [[8, 4]],
     26    },
     27  });
     28 });
     29 
     30 add_task(function type_suggested() {
     31  let queryContext = createContext("test");
     32  let result = new UrlbarResult({
     33    type: UrlbarUtils.RESULT_TYPE.SEARCH,
     34    source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
     35    payload: {
     36      suggestion: "test search test",
     37    },
     38    highlights: {
     39      suggestion: UrlbarUtils.HIGHLIGHT.SUGGESTED,
     40    },
     41  });
     42 
     43  doTest({
     44    result,
     45    target: "suggestion",
     46    options: { tokens: queryContext.tokens },
     47    expected: {
     48      value: "test search test",
     49      highlights: [[4, 8]],
     50    },
     51  });
     52 });
     53 
     54 add_task(function type_all() {
     55  let queryContext = createContext("test");
     56  let result = new UrlbarResult({
     57    type: UrlbarUtils.RESULT_TYPE.URL,
     58    source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
     59    payload: {
     60      url: "https://test.example.com/",
     61    },
     62    highlights: {
     63      url: UrlbarUtils.HIGHLIGHT.ALL,
     64    },
     65  });
     66 
     67  doTest({
     68    result,
     69    target: "url",
     70    options: { tokens: queryContext.tokens },
     71    expected: {
     72      value: "https://test.example.com/",
     73      highlights: [[0, 25]],
     74    },
     75  });
     76 });
     77 
     78 add_task(function option_isURL() {
     79  let queryContext = createContext("test");
     80  let result = new UrlbarResult({
     81    type: UrlbarUtils.RESULT_TYPE.URL,
     82    source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
     83    payload: {
     84      url: "https://test.example.com/",
     85    },
     86    highlights: {
     87      url: UrlbarUtils.HIGHLIGHT.TYPED,
     88    },
     89  });
     90 
     91  doTest({
     92    result,
     93    target: "url",
     94    options: { tokens: queryContext.tokens, isURL: true },
     95    expected: {
     96      value: "test.example.com",
     97      highlights: [[0, 4]],
     98    },
     99  });
    100 });
    101 
    102 add_task(function option_no_tokens() {
    103  let queryContext = createContext("");
    104  let result = new UrlbarResult({
    105    type: UrlbarUtils.RESULT_TYPE.URL,
    106    source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
    107    payload: {
    108      url: "https://test.example.com/",
    109    },
    110    highlights: {
    111      url: UrlbarUtils.HIGHLIGHT.TYPED,
    112    },
    113  });
    114 
    115  doTest({
    116    result,
    117    target: "url",
    118    options: { tokens: queryContext.tokens },
    119    expected: {
    120      value: "https://test.example.com/",
    121      highlights: undefined,
    122    },
    123  });
    124 });
    125 
    126 add_task(function option_nothing() {
    127  let result = new UrlbarResult({
    128    type: UrlbarUtils.RESULT_TYPE.URL,
    129    source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
    130    payload: {
    131      url: "https://test.example.com/",
    132    },
    133    highlights: {
    134      url: UrlbarUtils.HIGHLIGHT.TYPED,
    135    },
    136  });
    137 
    138  doTest({
    139    result,
    140    target: "url",
    141    expected: {
    142      value: "https://test.example.com/",
    143      highlights: undefined,
    144    },
    145  });
    146 });
    147 
    148 add_task(function invalid_target() {
    149  let queryContext = createContext("test");
    150  let result = new UrlbarResult({
    151    type: UrlbarUtils.RESULT_TYPE.URL,
    152    source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
    153    payload: {
    154      url: "https://test.example.com/",
    155    },
    156    highlights: {
    157      url: UrlbarUtils.HIGHLIGHT.TYPED,
    158    },
    159  });
    160 
    161  doTest({
    162    result,
    163    target: "invalid",
    164    options: { tokens: queryContext.tokens },
    165    expected: {
    166      value: undefined,
    167      highlights: undefined,
    168    },
    169  });
    170 });
    171 
    172 add_task(function cache() {
    173  let queryContext = createContext("test");
    174  let result = new UrlbarResult({
    175    type: UrlbarUtils.RESULT_TYPE.URL,
    176    source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
    177    payload: {
    178      url: "https://test.example.com/",
    179    },
    180    highlights: {
    181      url: UrlbarUtils.HIGHLIGHT.TYPED,
    182    },
    183  });
    184 
    185  info("Get without any options");
    186  doTest({
    187    result,
    188    target: "url",
    189    expected: {
    190      value: "https://test.example.com/",
    191      highlights: undefined,
    192    },
    193  });
    194 
    195  info("Get with tokens");
    196  doTest({
    197    result,
    198    target: "url",
    199    options: { tokens: queryContext.tokens },
    200    expected: {
    201      value: "https://test.example.com/",
    202      highlights: [[8, 4]],
    203    },
    204  });
    205 
    206  info("Get with different isURL");
    207  doTest({
    208    result,
    209    target: "url",
    210    options: { tokens: queryContext.tokens, isURL: true },
    211    expected: {
    212      value: "test.example.com",
    213      highlights: [[0, 4]],
    214    },
    215  });
    216 
    217  info("Get without tokens");
    218  doTest({
    219    result,
    220    target: "url",
    221    options: { isURL: true },
    222    expected: {
    223      value: "test.example.com",
    224      highlights: [[0, 4]],
    225    },
    226  });
    227 
    228  info("Get without different tokens");
    229  let anotherQueryContext = createContext("example");
    230  doTest({
    231    result,
    232    target: "url",
    233    options: { tokens: anotherQueryContext.tokens },
    234    expected: {
    235      value: "https://test.example.com/",
    236      highlights: [[13, 7]],
    237    },
    238  });
    239 });
    240 
    241 function doTest({ result, target, options, expected }) {
    242  let { value, highlights } = result.getDisplayableValueAndHighlights(
    243    target,
    244    options
    245  );
    246  Assert.equal(value, expected.value);
    247  Assert.deepEqual(highlights, expected.highlights);
    248 }