/work/auto_tests/scenarios/scenario_user_status_test.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "framework/framework.h" |
2 | | #include <stdio.h> |
3 | | #include <stdlib.h> |
4 | | #include <string.h> |
5 | | |
6 | | typedef struct { |
7 | | Tox_User_Status last_status; |
8 | | bool status_changed; |
9 | | } BobState; |
10 | | |
11 | | static void on_friend_status(const Tox_Event_Friend_Status *event, void *user_data) |
12 | 4 | { |
13 | 4 | ToxNode *self = (ToxNode *)user_data; |
14 | 4 | BobState *state = (BobState *)tox_node_get_script_ctx(self); |
15 | | |
16 | 4 | if (tox_event_friend_status_get_friend_number(event) == 0) { |
17 | 4 | state->last_status = tox_event_friend_status_get_status(event); |
18 | 4 | state->status_changed = true; |
19 | 4 | tox_node_log(self, "Alice changed status to %s", tox_user_status_to_string(state->last_status)); |
20 | 4 | } |
21 | 4 | } |
22 | | |
23 | | static void alice_script(ToxNode *self, void *ctx) |
24 | 1 | { |
25 | 1 | tox_node_wait_for_self_connected(self); |
26 | 1 | tox_node_wait_for_friend_connected(self, 0); |
27 | | |
28 | 1 | Tox *tox = tox_node_get_tox(self); |
29 | | |
30 | 1 | tox_node_log(self, "Setting status to AWAY"); |
31 | 1 | tox_self_set_status(tox, TOX_USER_STATUS_AWAY); |
32 | 1 | ck_assert(tox_self_get_status(tox) == TOX_USER_STATUS_AWAY); |
33 | | |
34 | | // Yield to let the status propagate |
35 | 21 | for (int i = 0; i < 20; ++i) { |
36 | 20 | tox_scenario_yield(self); |
37 | 20 | } |
38 | | |
39 | 1 | tox_node_log(self, "Setting status to BUSY"); |
40 | 1 | tox_self_set_status(tox, TOX_USER_STATUS_BUSY); |
41 | 1 | ck_assert(tox_self_get_status(tox) == TOX_USER_STATUS_BUSY); |
42 | | |
43 | 21 | for (int i = 0; i < 20; ++i) { |
44 | 20 | tox_scenario_yield(self); |
45 | 20 | } |
46 | | |
47 | 1 | tox_node_log(self, "Setting status back to NONE"); |
48 | 1 | tox_self_set_status(tox, TOX_USER_STATUS_NONE); |
49 | 1 | ck_assert(tox_self_get_status(tox) == TOX_USER_STATUS_NONE); |
50 | | |
51 | 1 | ToxNode *bob = tox_scenario_get_node(tox_node_get_scenario(self), 1); |
52 | 1 | WAIT_UNTIL(tox_node_is_finished(bob)); |
53 | 1 | } |
54 | | |
55 | | static void bob_script(ToxNode *self, void *ctx) |
56 | 1 | { |
57 | 1 | BobState *state = (BobState *)ctx; |
58 | 1 | tox_events_callback_friend_status(tox_node_get_dispatch(self), on_friend_status); |
59 | | |
60 | 1 | tox_node_wait_for_self_connected(self); |
61 | 1 | tox_node_wait_for_friend_connected(self, 0); |
62 | | |
63 | 1 | tox_node_log(self, "Waiting for Alice to become AWAY..."); |
64 | 1 | WAIT_UNTIL(state->status_changed && state->last_status == TOX_USER_STATUS_AWAY); |
65 | 1 | state->status_changed = false; |
66 | | |
67 | 1 | tox_node_log(self, "Waiting for Alice to become BUSY..."); |
68 | 1 | WAIT_UNTIL(state->status_changed && state->last_status == TOX_USER_STATUS_BUSY); |
69 | 1 | state->status_changed = false; |
70 | | |
71 | 1 | tox_node_log(self, "Waiting for Alice to become NONE..."); |
72 | 1 | WAIT_UNTIL(state->status_changed && state->last_status == TOX_USER_STATUS_NONE); |
73 | | |
74 | 1 | tox_node_log(self, "Status propagation verified!"); |
75 | 1 | } |
76 | | |
77 | | int main(int argc, char *argv[]) |
78 | 31 | { |
79 | 31 | ToxScenario *s = tox_scenario_new(argc, argv, 60000); |
80 | | |
81 | 31 | BobState bob_state = {TOX_USER_STATUS_NONE, false}; |
82 | | |
83 | 31 | tox_scenario_add_node(s, "Alice", alice_script, nullptr, 0); |
84 | 31 | tox_scenario_add_node(s, "Bob", bob_script, &bob_state, sizeof(BobState)); |
85 | | |
86 | 31 | ToxNode *alice = tox_scenario_get_node(s, 0); |
87 | 31 | ToxNode *bob = tox_scenario_get_node(s, 1); |
88 | | |
89 | 31 | tox_node_bootstrap(alice, bob); |
90 | 31 | tox_node_friend_add(alice, bob); |
91 | 31 | tox_node_friend_add(bob, alice); |
92 | | |
93 | 31 | ToxScenarioStatus res = tox_scenario_run(s); |
94 | 31 | if (res != TOX_SCENARIO_DONE) { |
95 | 0 | fprintf(stderr, "Scenario failed with status %u\n", res); |
96 | 0 | return 1; |
97 | 0 | } |
98 | | |
99 | 31 | tox_scenario_free(s); |
100 | 31 | return 0; |
101 | 31 | } |
102 | | |