File: duration_test.cpp

package info (click to toggle)
wf-config 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 424 kB
  • sloc: cpp: 5,743; xml: 37; makefile: 4
file content (164 lines) | stat: -rw-r--r-- 5,235 bytes parent folder | download | duplicates (2)
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
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

#include <wayfire/config/option.hpp>
#include <wayfire/util/duration.hpp>
#include <unistd.h>

using namespace wf;
using namespace wf::config;
using namespace wf::animation;

TEST_CASE("wf::animation::duration_t")
{
    duration_t duration;

    auto check_lifetime = [&] ()
    {
        CHECK(duration.running() == false);
        CHECK(duration.progress() == doctest::Approx{1.0});

        duration.start();
        CHECK(duration.running());
        CHECK(duration.progress() == doctest::Approx{0.0});

        usleep(50000);
        CHECK(duration.progress() == doctest::Approx{0.5}.epsilon(0.1));
        CHECK(duration.running());
        CHECK(duration.running());
        usleep(60000);

        /* At this point, duration must be finished */
        CHECK(duration.progress() == doctest::Approx{1.0}.epsilon(0.01));
        CHECK(duration.running()); // one last time
        CHECK(duration.running() == false);
        CHECK(duration.running() == false);
    };

    SUBCASE("Int option")
    {
        auto length = std::make_shared<option_t<int>>("length", 100);
        duration = duration_t{length, smoothing::linear};
    }

    SUBCASE("Animation option")
    {
        auto length = std::make_shared<option_t<wf::animation_description_t>>("length",
            wf::animation_description_t{
            .length_ms = 100,
            .easing    = smoothing::linear,
            .easing_name = "linear",
        });
        duration = duration_t{length};
    }

    /* Check twice, so that we can test restarting */
    check_lifetime();
    check_lifetime();

    auto check_reverse_duration = [&] ()
    {
        auto direction = duration.get_direction();
        CHECK(duration.running() == false);
        CHECK(duration.progress() == doctest::Approx{direction ? 1.0 : 0.0});

        duration.start();
        CHECK(duration.running());
        CHECK(duration.progress() == doctest::Approx{direction ? 0.0 : 1.0});

        usleep(75000);
        CHECK(duration.progress() == doctest::Approx{direction ? 0.75 : 0.25}.epsilon(
            0.1));
        duration.reverse();
        usleep(50000);
        CHECK(duration.progress() == doctest::Approx{direction ? 0.25 : 0.75}.epsilon(
            0.1));
        CHECK(duration.running());
        CHECK(duration.running());
        usleep(35000);

        /* At this point, duration must be finished */
        CHECK(duration.progress() ==
            doctest::Approx{direction ? 0.0 : 1.0}.epsilon(0.01));
        CHECK(duration.running()); // one last time
        CHECK(duration.running() == false);
        CHECK(duration.running() == false);
    };

    /* Check twice, so that we can test direction */
    check_reverse_duration();
    check_reverse_duration();
}

TEST_CASE("wf::animation::timed_transition_t")
{
    const double start   = 1.0;
    const double end     = 2.0;
    const double overend = 3.0;
    const double middle  = (start + end) / 2.0;

    auto length = std::make_shared<option_t<int>>("length", 100);
    duration_t duration{length, smoothing::linear};
    timed_transition_t transition{duration};
    timed_transition_t transition2{duration, start, end};
    transition.set(start, end);
    CHECK(transition.start == doctest::Approx(start));
    CHECK(transition.end == doctest::Approx(end));

    duration.start();
    CHECK((double)transition == doctest::Approx(start));
    usleep(50000);
    CHECK((double)transition == doctest::Approx(middle).epsilon(0.1));
    CHECK((double)transition2 == doctest::Approx(middle).epsilon(0.1));
    transition.restart_with_end(overend);
    transition2.restart_same_end();
    CHECK(transition.start == doctest::Approx(middle).epsilon(0.1));
    CHECK(transition2.start == doctest::Approx(middle).epsilon(0.1));
    CHECK(transition.end == doctest::Approx(overend));
    CHECK(transition2.end == doctest::Approx(end));
    usleep(60000);
    CHECK((double)transition == doctest::Approx(overend).epsilon(0.1));

    transition.flip();
    CHECK(transition.start == doctest::Approx(3.0).epsilon(0.1));
    CHECK(transition.end == doctest::Approx(middle).epsilon(0.1));
}

TEST_CASE("wf::animation::simple_animation_t")
{
    auto length = std::make_shared<option_t<int>>("length", 10);
    simple_animation_t anim{length, smoothing::linear};

    auto cycle_through = [&] (double s, double e, bool x1, bool x2)
    {
        if (!x1 && !x2)
        {
            anim.animate(s, e);
        } else if (!x2)
        {
            anim.animate(e);
        } else if (!x1)
        {
            anim.animate();
        }

        CHECK(anim.running());
        CHECK((double)anim == doctest::Approx(s));
        usleep(5000);
        CHECK((double)anim == doctest::Approx((s + e) / 2).epsilon(0.1));
        CHECK(anim.running());
        usleep(5500);
        CHECK((double)anim == doctest::Approx(e));
        CHECK(anim.running());
        CHECK(!anim.running());
    };

    cycle_through(1, 2, false, false);
    cycle_through(2, 3, true, false);
    cycle_through(3, 3, false, true);

    simple_animation_t sa;
    sa = simple_animation_t{length};
    sa.animate(1, 2);
    CHECK((double)sa == doctest::Approx(1.0));
}