tor-browser

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

test_storage_function.js (2259B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // This file tests the custom functions
      6 
      7 var testNums = [1, 2, 3, 4];
      8 
      9 function setup() {
     10  getOpenedDatabase().createTable("function_tests", "id INTEGER PRIMARY KEY");
     11 
     12  var stmt = createStatement("INSERT INTO function_tests (id) VALUES(?1)");
     13  for (let i = 0; i < testNums.length; ++i) {
     14    stmt.bindByIndex(0, testNums[i]);
     15    stmt.execute();
     16  }
     17  stmt.reset();
     18  stmt.finalize();
     19 }
     20 
     21 var testSquareFunction = {
     22  calls: 0,
     23 
     24  onFunctionCall(val) {
     25    ++this.calls;
     26    return val.getInt32(0) * val.getInt32(0);
     27  },
     28 };
     29 
     30 function test_function_registration() {
     31  var msc = getOpenedDatabase();
     32  msc.createFunction("test_square", 1, testSquareFunction);
     33 }
     34 
     35 function test_function_no_double_registration() {
     36  var msc = getOpenedDatabase();
     37  try {
     38    msc.createFunction("test_square", 2, testSquareFunction);
     39    do_throw("We shouldn't get here!");
     40  } catch (e) {
     41    Assert.equal(Cr.NS_ERROR_FAILURE, e.result);
     42  }
     43 }
     44 
     45 function test_function_removal() {
     46  var msc = getOpenedDatabase();
     47  msc.removeFunction("test_square");
     48  // Should be Ok now
     49  msc.createFunction("test_square", 1, testSquareFunction);
     50 }
     51 
     52 function test_function_aliases() {
     53  var msc = getOpenedDatabase();
     54  msc.createFunction("test_square2", 1, testSquareFunction);
     55 }
     56 
     57 function test_function_call() {
     58  var stmt = createStatement("SELECT test_square(id) FROM function_tests");
     59  while (stmt.executeStep()) {
     60    // Do nothing.
     61  }
     62  Assert.equal(testNums.length, testSquareFunction.calls);
     63  testSquareFunction.calls = 0;
     64  stmt.finalize();
     65 }
     66 
     67 function test_function_result() {
     68  var stmt = createStatement("SELECT test_square(42) FROM function_tests");
     69  stmt.executeStep();
     70  Assert.equal(42 * 42, stmt.getInt32(0));
     71  testSquareFunction.calls = 0;
     72  stmt.finalize();
     73 }
     74 
     75 var tests = [
     76  test_function_registration,
     77  test_function_no_double_registration,
     78  test_function_removal,
     79  test_function_aliases,
     80  test_function_call,
     81  test_function_result,
     82 ];
     83 
     84 function run_test() {
     85  setup();
     86 
     87  for (var i = 0; i < tests.length; i++) {
     88    tests[i]();
     89  }
     90 
     91  cleanup();
     92 }