aboutsummaryrefslogtreecommitdiffstats
path: root/components/config/tests/prefs.rs
blob: e4ff276ebb57883bd3e06b21129f73d99dff43e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use servo_config::basedir;
use servo_config::prefs::{read_prefs, PrefValue, PREFS};
use std::fs::{self, File};
use std::io::{Read, Write};

#[test]
fn test_create_pref() {
    let json_str = "{\
                    \"layout.writing-mode.enabled\": true,\
                    \"network.mime.sniff\": false,\
                    \"shell.homepage\": \"https://servo.org\"\
                    }";

    let prefs = read_prefs(json_str);
    assert!(prefs.is_ok());
    let prefs = prefs.unwrap();

    assert_eq!(prefs.len(), 3);
}

#[test]
fn test_get_set_reset_extend() {
    let json_str = "{\
                    \"layout.writing-mode.enabled\": true,\
                    \"extra.stuff\": false,\
                    \"shell.homepage\": \"https://google.com\"\
                    }";

    assert_eq!(*PREFS.get("test"), PrefValue::Missing);
    PREFS.set("test", PrefValue::String("hi".to_owned()));
    assert_eq!(*PREFS.get("test"), PrefValue::String("hi".to_owned()));
    assert_eq!(
        *PREFS.get("shell.homepage"),
        PrefValue::String("https://servo.org".to_owned())
    );
    PREFS.set("shell.homepage", PrefValue::Boolean(true));
    assert_eq!(*PREFS.get("shell.homepage"), PrefValue::Boolean(true));
    PREFS.reset("shell.homepage");
    assert_eq!(
        *PREFS.get("shell.homepage"),
        PrefValue::String("https://servo.org".to_owned())
    );

    let extension = read_prefs(json_str).unwrap();
    PREFS.extend(extension);
    assert_eq!(
        *PREFS.get("shell.homepage"),
        PrefValue::String("https://google.com".to_owned())
    );
    assert_eq!(
        *PREFS.get("layout.writing-mode.enabled"),
        PrefValue::Boolean(true)
    );
    assert_eq!(*PREFS.get("extra.stuff"), PrefValue::Boolean(false));
}

#[cfg(not(target_os = "android"))]
#[test]
fn test_default_config_dir_create_read_write() {
    let json_str = "{\
                    \"layout.writing-mode.enabled\": true,\
                    \"extra.stuff\": false,\
                    \"shell.homepage\": \"https://google.com\"\
                    }";
    let mut expected_json = String::new();
    let config_path = basedir::default_config_dir().unwrap();

    if !config_path.exists() {
        fs::create_dir_all(&config_path).unwrap();
    }

    let json_path = config_path.join("test_config.json");

    let mut fd = File::create(&json_path).unwrap();
    assert_eq!(json_path.exists(), true);

    fd.write_all(json_str.as_bytes()).unwrap();
    let mut fd = File::open(&json_path).unwrap();
    fd.read_to_string(&mut expected_json).unwrap();

    assert_eq!(json_str, expected_json);

    fs::remove_file(&json_path).unwrap();
}