summaryrefslogtreecommitdiff
path: root/auto_tests/typing_test.c
diff options
context:
space:
mode:
authorRobin Lindén <dev@robinlinden.eu>2018-02-18 01:57:45 +0100
committeriphydf <iphydf@users.noreply.github.com>2018-02-18 14:50:18 +0000
commitc3515c49e012f76c5d2ca104476e372d924a79f9 (patch)
tree56512d94a79ad08d768ea51afa735c56833338b1 /auto_tests/typing_test.c
parent13706de14bce8f8bfd1cb7db226d12cbd927b1b4 (diff)
Split up tox_test into multiple smaller tests
Diffstat (limited to 'auto_tests/typing_test.c')
-rw-r--r--auto_tests/typing_test.c111
1 files changed, 111 insertions, 0 deletions
diff --git a/auto_tests/typing_test.c b/auto_tests/typing_test.c
new file mode 100644
index 00000000..837ed757
--- /dev/null
+++ b/auto_tests/typing_test.c
@@ -0,0 +1,111 @@
1/* Tests that our typing notifications work.
2 */
3
4#ifndef _XOPEN_SOURCE
5#define _XOPEN_SOURCE 600
6#endif
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "check_compat.h"
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <time.h>
17
18#include "../toxcore/ccompat.h"
19#include "../toxcore/tox.h"
20#include "../toxcore/util.h"
21
22#include "helpers.h"
23
24static void typing_callback(Tox *m, uint32_t friendnumber, bool typing, void *userdata)
25{
26 bool *is_typing = (bool *)userdata;
27 *is_typing = typing;
28}
29
30static void test_typing(void)
31{
32 printf("initialising 2 toxes\n");
33 uint32_t index[] = { 1, 2 };
34 const time_t cur_time = time(nullptr);
35 Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]);
36 Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]);
37
38 ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances");
39
40 printf("tox1 adds tox2 as friend, tox2 adds tox1\n");
41 uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
42 tox_self_get_public_key(tox2, public_key);
43 tox_friend_add_norequest(tox1, public_key, nullptr);
44 tox_self_get_public_key(tox1, public_key);
45 tox_friend_add_norequest(tox2, public_key, nullptr);
46
47 printf("bootstrapping tox2 off tox1\n");
48 uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
49 tox_self_get_dht_id(tox1, dht_key);
50 const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
51
52 tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);
53
54 while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
55 tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) {
56 tox_iterate(tox1, nullptr);
57 tox_iterate(tox2, nullptr);
58
59 c_sleep(200);
60 }
61
62 printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time);
63 const time_t con_time = time(nullptr);
64
65 while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
66 tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) {
67 tox_iterate(tox1, nullptr);
68 tox_iterate(tox2, nullptr);
69
70 c_sleep(200);
71 }
72
73 printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time);
74
75 tox_callback_friend_typing(tox2, &typing_callback);
76 tox_self_set_typing(tox1, 0, true, nullptr);
77
78 bool is_typing = false;
79
80 while (!is_typing) {
81 tox_iterate(tox1, nullptr);
82 tox_iterate(tox2, &is_typing);
83 c_sleep(200);
84 }
85
86 ck_assert_msg(tox_friend_get_typing(tox2, 0, nullptr) == 1, "Typing failure");
87 tox_self_set_typing(tox1, 0, false, nullptr);
88
89 while (is_typing) {
90 tox_iterate(tox1, nullptr);
91 tox_iterate(tox2, &is_typing);
92 c_sleep(200);
93 }
94
95 TOX_ERR_FRIEND_QUERY err_t;
96 ck_assert_msg(tox_friend_get_typing(tox2, 0, &err_t) == 0, "Typing failure");
97 ck_assert_msg(err_t == TOX_ERR_FRIEND_QUERY_OK, "Typing failure");
98
99 printf("test_typing succeeded, took %ld seconds\n", time(nullptr) - cur_time);
100
101 tox_kill(tox1);
102 tox_kill(tox2);
103}
104
105int main(int argc, char *argv[])
106{
107 setvbuf(stdout, nullptr, _IONBF, 0);
108
109 test_typing();
110 return 0;
111}