tor-browser

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

test.rs (2250B)


      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 use l10nregistry_ffi::load::{load_async, load_sync};
      6 use std::borrow::Borrow;
      7 
      8 // We want to test a file that ships in every platform configuration, so we take
      9 // something from `toolkit/`.  But we don't want to depend on the specifics of
     10 // the text, or the packaging of that text, since those can change.  It would be
     11 // best to register an untranslated `.ftl` for this test, but that's difficult.
     12 // Second best is to ship an untranslated `.ftl`, but that is not well-supported
     13 // by existing processes either.  So we settle for depending on the form of
     14 // specific identifiers, whose names will appear in future searches, while
     15 // depending on the specific messages or the file packaging.
     16 fn assert_about_about_correct<T: Borrow<[u8]>> (res: T) {
     17    assert!(res.borrow().len() > 0);
     18 
     19    // `windows` is a convenient, if inefficient, way to look for a subslice.
     20    let needle = b"about-about-title";
     21    assert!(res.borrow().windows(needle.len()).position(|window| window == needle).is_some());
     22 
     23    let needle = b"about-about-note";
     24    assert!(res.borrow().windows(needle.len()).position(|window| window == needle).is_some());
     25 }
     26 
     27 #[no_mangle]
     28 pub extern "C" fn Rust_L10NLoadAsync(it_worked: *mut bool) {
     29    let future = async move {
     30        match load_async("resource://gre/localization/en-US/toolkit/about/aboutAbout.ftl").await {
     31            Ok(res) => {
     32                assert_about_about_correct(res);
     33                unsafe {
     34                    *it_worked = true;
     35                }
     36            }
     37            Err(err) => panic!("{:?}", err),
     38        }
     39    };
     40 
     41    unsafe {
     42        moz_task::gtest_only::spin_event_loop_until("Rust_L10NLoadAsync", future).unwrap();
     43    }
     44 }
     45 
     46 #[no_mangle]
     47 pub extern "C" fn Rust_L10NLoadSync(it_worked: *mut bool) {
     48    match load_sync("resource://gre/localization/en-US/toolkit/about/aboutAbout.ftl") {
     49        Ok(res) => {
     50            assert_about_about_correct(res);
     51            unsafe {
     52                *it_worked = true;
     53            }
     54        }
     55        Err(err) => panic!("{:?}", err),
     56    }
     57 }