aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/script/htmlimageelement.rs
blob: 1b0612b14a10f04bfb506c8d184299de040f742d (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* 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 https://mozilla.org/MPL/2.0/. */

use script::test::srcset::{parse_a_srcset_attribute, Descriptor, ImageSource};

#[test]
fn no_value() {
    let new_vec = Vec::new();
    assert_eq!(parse_a_srcset_attribute(" "), new_vec);
}

#[test]
fn width_one_value() {
    let first_descriptor = Descriptor {
        wid: Some(320),
        den: None,
    };
    let first_imagesource = ImageSource {
        url: "small-image.jpg".to_string(),
        descriptor: first_descriptor,
    };
    let sources = &[first_imagesource];
    assert_eq!(parse_a_srcset_attribute("small-image.jpg, 320w"), sources);
}

#[test]
fn width_two_value() {
    let first_descriptor = Descriptor {
        wid: Some(320),
        den: None,
    };
    let first_imagesource = ImageSource {
        url: "small-image.jpg".to_string(),
        descriptor: first_descriptor,
    };
    let second_descriptor = Descriptor {
        wid: Some(480),
        den: None,
    };
    let second_imagesource = ImageSource {
        url: "medium-image.jpg".to_string(),
        descriptor: second_descriptor,
    };
    let sources = &[first_imagesource, second_imagesource];
    assert_eq!(
        parse_a_srcset_attribute("small-image.jpg 320w, medium-image.jpg 480w"),
        sources
    );
}

#[test]
fn width_three_value() {
    let first_descriptor = Descriptor {
        wid: Some(320),
        den: None,
    };
    let first_imagesource = ImageSource {
        url: "smallImage.jpg".to_string(),
        descriptor: first_descriptor,
    };
    let second_descriptor = Descriptor {
        wid: Some(480),
        den: None,
    };
    let second_imagesource = ImageSource {
        url: "mediumImage.jpg".to_string(),
        descriptor: second_descriptor,
    };
    let third_descriptor = Descriptor {
        wid: Some(800),
        den: None,
    };
    let third_imagesource = ImageSource {
        url: "largeImage.jpg".to_string(),
        descriptor: third_descriptor,
    };
    let sources = &[first_imagesource, second_imagesource, third_imagesource];
    assert_eq!(
        parse_a_srcset_attribute(
            "smallImage.jpg 320w,
                                        mediumImage.jpg 480w,
                                        largeImage.jpg 800w"
        ),
        sources
    );
}

#[test]
fn density_value() {
    let first_descriptor = Descriptor {
        wid: None,
        den: Some(1.0),
    };
    let first_imagesource = ImageSource {
        url: "small-image.jpg".to_string(),
        descriptor: first_descriptor,
    };
    let sources = &[first_imagesource];
    assert_eq!(parse_a_srcset_attribute("small-image.jpg 1x"), sources);
}

#[test]
fn without_descriptor() {
    let first_descriptor = Descriptor {
        wid: None,
        den: None,
    };
    let first_imagesource = ImageSource {
        url: "small-image.jpg".to_string(),
        descriptor: first_descriptor,
    };
    let sources = &[first_imagesource];
    assert_eq!(parse_a_srcset_attribute("small-image.jpg"), sources);
}

//Does not parse an ImageSource when both width and density descriptor present
#[test]
fn two_descriptor() {
    let empty_vec = Vec::new();
    assert_eq!(
        parse_a_srcset_attribute("small-image.jpg 320w 1.1x"),
        empty_vec
    );
}

#[test]
fn decimal_descriptor() {
    let first_descriptor = Descriptor {
        wid: None,
        den: Some(2.2),
    };
    let first_imagesource = ImageSource {
        url: "small-image.jpg".to_string(),
        descriptor: first_descriptor,
    };
    let sources = &[first_imagesource];
    assert_eq!(parse_a_srcset_attribute("small-image.jpg 2.2x"), sources);
}

#[test]
fn different_descriptor() {
    let first_descriptor = Descriptor {
        wid: Some(320),
        den: None,
    };
    let first_imagesource = ImageSource {
        url: "small-image.jpg".to_string(),
        descriptor: first_descriptor,
    };
    let second_descriptor = Descriptor {
        wid: None,
        den: Some(2.2),
    };
    let second_imagesource = ImageSource {
        url: "medium-image.jpg".to_string(),
        descriptor: second_descriptor,
    };
    let sources = &[first_imagesource, second_imagesource];
    assert_eq!(
        parse_a_srcset_attribute("small-image.jpg 320w, medium-image.jpg 2.2x"),
        sources
    );
}