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 parsing::parse;
6 use style::properties::longhands::{background_attachment, background_clip, background_color, background_image};
7 use style::properties::longhands::{background_origin, background_position_x, background_position_y, background_repeat};
8 use style::properties::longhands::background_size;
9 use style::properties::shorthands::background;
10 
11 #[test]
background_shorthand_should_parse_all_available_properties_when_specified()12 fn background_shorthand_should_parse_all_available_properties_when_specified() {
13     let input = "url(\"http://servo/test.png\") top center / 200px 200px repeat-x fixed padding-box content-box red";
14     let result = parse(background::parse_value, input).unwrap();
15 
16     assert_eq!(result.background_image, parse_longhand!(background_image, "url(\"http://servo/test.png\")"));
17     assert_eq!(result.background_position_x, parse_longhand!(background_position_x, "center"));
18     assert_eq!(result.background_position_y, parse_longhand!(background_position_y, "top"));
19     assert_eq!(result.background_size, parse_longhand!(background_size, "200px 200px"));
20     assert_eq!(result.background_repeat, parse_longhand!(background_repeat, "repeat-x"));
21     assert_eq!(result.background_attachment, parse_longhand!(background_attachment, "fixed"));
22     assert_eq!(result.background_origin, parse_longhand!(background_origin, "padding-box"));
23     assert_eq!(result.background_clip, parse_longhand!(background_clip, "content-box"));
24     assert_eq!(result.background_color, parse_longhand!(background_color, "red"));
25 }
26 
27 #[test]
background_shorthand_should_parse_when_some_fields_set()28 fn background_shorthand_should_parse_when_some_fields_set() {
29     let result = parse(background::parse_value, "14px 40px repeat-y").unwrap();
30 
31     assert_eq!(result.background_position_x, parse_longhand!(background_position_x, "14px"));
32     assert_eq!(result.background_position_y, parse_longhand!(background_position_y, "40px"));
33     assert_eq!(result.background_repeat, parse_longhand!(background_repeat, "repeat-y"));
34 
35     let result = parse(background::parse_value, "url(\"http://servo/test.png\") repeat blue").unwrap();
36 
37     assert_eq!(result.background_image, parse_longhand!(background_image, "url(\"http://servo/test.png\")"));
38     assert_eq!(result.background_repeat, parse_longhand!(background_repeat, "repeat"));
39     assert_eq!(result.background_color, parse_longhand!(background_color, "blue"));
40 
41     let result = parse(background::parse_value, "padding-box").unwrap();
42 
43     assert_eq!(result.background_origin, parse_longhand!(background_origin, "padding-box"));
44     assert_eq!(result.background_clip, parse_longhand!(background_clip, "padding-box"));
45 
46     let result = parse(background::parse_value, "url(\"http://servo/test.png\")").unwrap();
47 
48     assert_eq!(result.background_image, parse_longhand!(background_image, "url(\"http://servo/test.png\")"));
49 }
50 
51 #[test]
background_shorthand_should_parse_comma_separated_declarations()52 fn background_shorthand_should_parse_comma_separated_declarations() {
53     let input = "url(\"http://servo/test.png\") top left no-repeat, url(\"http://servo/test.png\") \
54         center / 100% 100% no-repeat, white";
55     let result = parse(background::parse_value, input).unwrap();
56 
57     assert_eq!(result.background_image, parse_longhand!(background_image, "url(\"http://servo/test.png\"), \
58         url(\"http://servo/test.png\"), none"));
59     assert_eq!(result.background_position_x, parse_longhand!(background_position_x, "left, center, 0%"));
60     assert_eq!(result.background_position_y, parse_longhand!(background_position_y, "top, center, 0%"));
61     assert_eq!(result.background_repeat, parse_longhand!(background_repeat, "no-repeat, no-repeat, repeat"));
62     assert_eq!(result.background_clip, parse_longhand!(background_clip, "border-box, border-box, border-box"));
63     assert_eq!(result.background_origin, parse_longhand!(background_origin, "padding-box, padding-box, \
64         padding-box"));
65     assert_eq!(result.background_size, parse_longhand!(background_size, "auto auto, 100% 100%, auto auto"));
66     assert_eq!(result.background_attachment, parse_longhand!(background_attachment, "scroll, scroll, scroll"));
67     assert_eq!(result.background_color, parse_longhand!(background_color, "white"));
68 }
69 
70 #[test]
background_shorthand_should_parse_position_and_size_correctly()71 fn background_shorthand_should_parse_position_and_size_correctly() {
72     let result = parse(background::parse_value, "7px 4px").unwrap();
73 
74     assert_eq!(result.background_position_x, parse_longhand!(background_position_x, "7px"));
75     assert_eq!(result.background_position_y, parse_longhand!(background_position_y, "4px"));
76 
77     let result = parse(background::parse_value, "7px 4px / 30px 20px").unwrap();
78 
79     assert_eq!(result.background_position_x, parse_longhand!(background_position_x, "7px"));
80     assert_eq!(result.background_position_y, parse_longhand!(background_position_y, "4px"));
81     assert_eq!(result.background_size, parse_longhand!(background_size, "30px 20px"));
82 
83     assert!(parse(background::parse_value, "/ 30px 20px").is_err());
84 
85     assert!(parse(background::parse_value, "repeat-x / 30px 20px").is_err());
86 }
87 
88 #[test]
background_shorthand_should_parse_origin_and_clip_correctly()89 fn background_shorthand_should_parse_origin_and_clip_correctly() {
90     let result = parse(background::parse_value, "padding-box content-box").unwrap();
91 
92     assert_eq!(result.background_origin, parse_longhand!(background_origin, "padding-box"));
93     assert_eq!(result.background_clip, parse_longhand!(background_clip, "content-box"));
94 
95     let result = parse(background::parse_value, "padding-box padding-box").unwrap();
96 
97     assert_eq!(result.background_origin, parse_longhand!(background_origin, "padding-box"));
98     assert_eq!(result.background_clip, parse_longhand!(background_clip, "padding-box"));
99 
100     let result = parse(background::parse_value, "padding-box").unwrap();
101 
102     assert_eq!(result.background_origin, parse_longhand!(background_origin, "padding-box"));
103     assert_eq!(result.background_clip, parse_longhand!(background_clip, "padding-box"));
104 }
105