tor-browser

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

animated_properties.rs (6652B)


      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 https://mozilla.org/MPL/2.0/. */
      4 
      5 use cssparser::RGBA;
      6 use style::values::animated::{Animate, Procedure, ToAnimatedValue};
      7 use style::values::computed::Percentage;
      8 use style::values::generics::transform::{Transform, TransformOperation};
      9 
     10 fn interpolate_rgba(from: RGBA, to: RGBA, progress: f64) -> RGBA {
     11    let from = from.to_animated_value();
     12    let to = to.to_animated_value();
     13    RGBA::from_animated_value(
     14        from.animate(&to, Procedure::Interpolate { progress })
     15            .unwrap(),
     16    )
     17 }
     18 
     19 // Color
     20 #[test]
     21 fn test_rgba_color_interepolation_preserves_transparent() {
     22    assert_eq!(
     23        interpolate_rgba(RGBA::transparent(), RGBA::transparent(), 0.5),
     24        RGBA::transparent()
     25    );
     26 }
     27 
     28 #[test]
     29 fn test_rgba_color_interepolation_alpha() {
     30    assert_eq!(
     31        interpolate_rgba(RGBA::new(200, 0, 0, 100), RGBA::new(0, 200, 0, 200), 0.5),
     32        RGBA::new(67, 133, 0, 150)
     33    );
     34 }
     35 
     36 #[test]
     37 fn test_rgba_color_interepolation_out_of_range_1() {
     38    // Some cubic-bezier functions produce values that are out of range [0, 1].
     39    // Unclamped cases.
     40    assert_eq!(
     41        interpolate_rgba(
     42            RGBA::from_floats(0.3, 0.0, 0.0, 0.4),
     43            RGBA::from_floats(0.0, 1.0, 0.0, 0.6),
     44            -0.5
     45        ),
     46        RGBA::new(154, 0, 0, 77)
     47    );
     48 }
     49 
     50 #[test]
     51 fn test_rgba_color_interepolation_out_of_range_2() {
     52    assert_eq!(
     53        interpolate_rgba(
     54            RGBA::from_floats(1.0, 0.0, 0.0, 0.6),
     55            RGBA::from_floats(0.0, 0.3, 0.0, 0.4),
     56            1.5
     57        ),
     58        RGBA::new(0, 154, 0, 77)
     59    );
     60 }
     61 
     62 #[test]
     63 fn test_rgba_color_interepolation_out_of_range_clamped_1() {
     64    assert_eq!(
     65        interpolate_rgba(
     66            RGBA::from_floats(1.0, 0.0, 0.0, 0.8),
     67            RGBA::from_floats(0.0, 1.0, 0.0, 0.2),
     68            -0.5
     69        ),
     70        RGBA::from_floats(1.0, 0.0, 0.0, 1.0)
     71    );
     72 }
     73 
     74 #[test]
     75 fn test_rgba_color_interepolation_out_of_range_clamped_2() {
     76    assert_eq!(
     77        interpolate_rgba(
     78            RGBA::from_floats(1.0, 0.0, 0.0, 0.8),
     79            RGBA::from_floats(0.0, 1.0, 0.0, 0.2),
     80            1.5
     81        ),
     82        RGBA::from_floats(0.0, 0.0, 0.0, 0.0)
     83    );
     84 }
     85 
     86 // Transform
     87 #[test]
     88 fn test_transform_interpolation_on_translate() {
     89    use style::values::computed::{CalcLengthPercentage, Length, LengthPercentage};
     90 
     91    let from = Transform(vec![TransformOperation::Translate3D(
     92        LengthPercentage::Length(Length::new(0.)),
     93        LengthPercentage::Length(Length::new(100.)),
     94        Length::new(25.),
     95    )]);
     96    let to = Transform(vec![TransformOperation::Translate3D(
     97        LengthPercentage::Length(Length::new(100.)),
     98        LengthPercentage::Length(Length::new(0.)),
     99        Length::new(75.),
    100    )]);
    101    assert_eq!(
    102        from.animate(&to, Procedure::Interpolate { progress: 0.5 })
    103            .unwrap(),
    104        Transform(vec![TransformOperation::Translate3D(
    105            LengthPercentage::Length(Length::new(50.)),
    106            LengthPercentage::Length(Length::new(50.)),
    107            Length::new(50.),
    108        )])
    109    );
    110 
    111    let from = Transform(vec![TransformOperation::Translate3D(
    112        LengthPercentage::Percentage(Percentage(0.5)),
    113        LengthPercentage::Percentage(Percentage(1.0)),
    114        Length::new(25.),
    115    )]);
    116    let to = Transform(vec![TransformOperation::Translate3D(
    117        LengthPercentage::Length(Length::new(100.)),
    118        LengthPercentage::Length(Length::new(50.)),
    119        Length::new(75.),
    120    )]);
    121    assert_eq!(
    122        from.animate(&to, Procedure::Interpolate { progress: 0.5 })
    123            .unwrap(),
    124        Transform(vec![TransformOperation::Translate3D(
    125            // calc(50px + 25%)
    126            LengthPercentage::Calc(CalcLengthPercentage::new(
    127                Length::new(50.),
    128                Some(Percentage(0.25))
    129            )),
    130            // calc(25px + 50%)
    131            LengthPercentage::Calc(CalcLengthPercentage::new(
    132                Length::new(25.),
    133                Some(Percentage(0.5))
    134            )),
    135            Length::new(50.),
    136        )])
    137    );
    138 }
    139 
    140 #[test]
    141 fn test_transform_interpolation_on_scale() {
    142    let from = Transform(vec![TransformOperation::Scale3D(1.0, 2.0, 1.0)]);
    143    let to = Transform(vec![TransformOperation::Scale3D(2.0, 4.0, 2.0)]);
    144    assert_eq!(
    145        from.animate(&to, Procedure::Interpolate { progress: 0.5 })
    146            .unwrap(),
    147        Transform(vec![TransformOperation::Scale3D(1.5, 3.0, 1.5)])
    148    );
    149 }
    150 
    151 #[test]
    152 fn test_transform_interpolation_on_rotate() {
    153    use style::values::computed::Angle;
    154 
    155    let from = Transform(vec![TransformOperation::Rotate3D(
    156        0.0,
    157        0.0,
    158        1.0,
    159        Angle::from_radians(0.0),
    160    )]);
    161    let to = Transform(vec![TransformOperation::Rotate3D(
    162        0.0,
    163        0.0,
    164        1.0,
    165        Angle::from_radians(100.0),
    166    )]);
    167    assert_eq!(
    168        from.animate(&to, Procedure::Interpolate { progress: 0.5 })
    169            .unwrap(),
    170        Transform(vec![TransformOperation::Rotate3D(
    171            0.0,
    172            0.0,
    173            1.0,
    174            Angle::from_radians(50.0)
    175        ),])
    176    );
    177 }
    178 
    179 #[test]
    180 fn test_transform_interpolation_on_skew() {
    181    use style::values::computed::Angle;
    182 
    183    let from = Transform(vec![TransformOperation::Skew(
    184        Angle::from_radians(0.0),
    185        Some(Angle::from_radians(100.0)),
    186    )]);
    187    let to = Transform(vec![TransformOperation::Skew(
    188        Angle::from_radians(100.0),
    189        Some(Angle::from_radians(0.0)),
    190    )]);
    191    assert_eq!(
    192        from.animate(&to, Procedure::Interpolate { progress: 0.5 })
    193            .unwrap(),
    194        Transform(vec![TransformOperation::Skew(
    195            Angle::from_radians(50.0),
    196            Some(Angle::from_radians(50.0)),
    197        )])
    198    );
    199 }
    200 
    201 #[test]
    202 fn test_transform_interpolation_on_mismatched_lists() {
    203    use style::values::computed::{Angle, Length, LengthPercentage};
    204 
    205    let from = Transform(vec![TransformOperation::Rotate3D(
    206        0.0,
    207        0.0,
    208        1.0,
    209        Angle::from_radians(100.0),
    210    )]);
    211    let to = Transform(vec![TransformOperation::Translate3D(
    212        LengthPercentage::Length(Length::new(100.)),
    213        LengthPercentage::Length(Length::new(0.)),
    214        Length::new(0.),
    215    )]);
    216    assert_eq!(
    217        from.animate(&to, Procedure::Interpolate { progress: 0.5 })
    218            .unwrap(),
    219        Transform(vec![TransformOperation::InterpolateMatrix {
    220            from_list: from.clone(),
    221            to_list: to.clone(),
    222            progress: Percentage(0.5),
    223        }])
    224    );
    225 }