summaryrefslogtreecommitdiff
path: root/tests/dbus/test-entity.c
blob: a0645fbb04673bfe4c8fe0e582a6eb338b6d143c (plain)
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
165
166
167
168
169
170
171
#include "config.h"

#include <glib.h>
#include <glib/gprintf.h>
#include <telepathy-logger/entity.h>
#include <telepathy-logger/entity-internal.h>

#include "lib/util.h"
#include "lib/contacts-conn.h"

static void
test_entity_instantiation (void)
{
  TplEntity *entity;

  entity = tpl_entity_new ("my-identifier", TPL_ENTITY_CONTACT,
      "my-alias", "my-token");

  g_assert_cmpstr (tpl_entity_get_identifier (entity), ==, "my-identifier");
  g_assert (tpl_entity_get_entity_type (entity) == TPL_ENTITY_CONTACT);
  g_assert_cmpstr (tpl_entity_get_alias (entity), ==, "my-alias");
  g_assert_cmpstr (tpl_entity_get_avatar_token (entity), ==, "my-token");

  g_object_unref (entity);

  /* Check that identifier is copied in absence of ID */
  entity = tpl_entity_new ("my-identifier", TPL_ENTITY_CONTACT,
      NULL, NULL);

  g_assert_cmpstr (tpl_entity_get_alias (entity), ==, "my-identifier");
  g_assert_cmpstr (tpl_entity_get_avatar_token (entity), ==, "");

  g_object_unref (entity);
}

static void
test_entity_instantiation_from_room_id (void)
{
  TplEntity *entity;

  entity = tpl_entity_new_from_room_id ("my-room-id");

  g_assert_cmpstr (tpl_entity_get_identifier (entity), ==, "my-room-id");
  g_assert (tpl_entity_get_entity_type (entity) == TPL_ENTITY_ROOM);
  g_assert_cmpstr (tpl_entity_get_alias (entity), ==, "my-room-id");
  g_assert_cmpstr (tpl_entity_get_avatar_token (entity), ==, "");

  g_object_unref (entity);
}

typedef struct {
  TpContact *contact;
  GMainLoop *loop;
} Result;

static void
ensure_contact_cb (GObject *source,
    GAsyncResult *op_result,
    gpointer user_data)
{
  Result *result = user_data;
  GError *error = NULL;

  result->contact = tp_simple_client_factory_ensure_contact_by_id_finish (
      TP_SIMPLE_CLIENT_FACTORY (source), op_result, &error);

  g_assert_no_error (error);
  g_assert (TP_IS_CONTACT (result->contact));

  g_main_loop_quit (result->loop);
}

static void
test_entity_instantiation_from_tp_contact (void)
{
  TpBaseConnection *base_connection;
  TpConnection *client_connection;
  TpTestsContactsConnection *connection;
  TpHandleRepoIface *repo;
  TpHandle handles[2];
  const char *alias[] = {"Alice in Wonderland", "Bob the builder"};
  const char *avatar_tokens[] = {"alice-token", NULL};
  Result result;
  TplEntity *entity;
  TpContact *alice, *bob;
  TpSimpleClientFactory *factory;

  tp_tests_create_and_connect_conn (TP_TESTS_TYPE_CONTACTS_CONNECTION,
      "me@test.com", &base_connection, &client_connection);

  connection = TP_TESTS_CONTACTS_CONNECTION (base_connection);

  repo = tp_base_connection_get_handles (base_connection,
      TP_HANDLE_TYPE_CONTACT);

  handles[0] = tp_handle_ensure (repo, "alice", NULL, NULL);
  g_assert (handles[0] != 0);

  handles[1] = tp_handle_ensure (repo, "bob", NULL, NULL);
  g_assert (handles[1] != 0);

  tp_tests_contacts_connection_change_aliases (connection, 2, handles,
      alias);
  tp_tests_contacts_connection_change_avatar_tokens (connection, 2, handles,
      avatar_tokens);

  factory = tp_proxy_get_factory (client_connection);
  tp_simple_client_factory_add_contact_features_varargs (factory,
      TP_CONTACT_FEATURE_ALIAS,
      TP_CONTACT_FEATURE_AVATAR_TOKEN,
      TP_CONTACT_FEATURE_INVALID);

  result.loop = g_main_loop_new (NULL, FALSE);

  tp_simple_client_factory_ensure_contact_by_id_async (factory,
      client_connection, "alice", ensure_contact_cb, &result);
  g_main_loop_run (result.loop);
  alice = result.contact;

  tp_simple_client_factory_ensure_contact_by_id_async (factory,
      client_connection, "bob", ensure_contact_cb, &result);
  g_main_loop_run (result.loop);
  bob = result.contact;

  entity = tpl_entity_new_from_tp_contact (alice, TPL_ENTITY_SELF);

  g_assert_cmpstr (tpl_entity_get_identifier (entity), ==, "alice");
  g_assert (tpl_entity_get_entity_type (entity) == TPL_ENTITY_SELF);
  g_assert_cmpstr (tpl_entity_get_alias (entity), ==, alias[0]);
  g_assert_cmpstr (tpl_entity_get_avatar_token (entity), ==, avatar_tokens[0]);
  g_object_unref (entity);

  entity = tpl_entity_new_from_tp_contact (bob, TPL_ENTITY_CONTACT);

  g_assert_cmpstr (tpl_entity_get_identifier (entity), ==, "bob");
  g_assert (tpl_entity_get_entity_type (entity) == TPL_ENTITY_CONTACT);
  g_assert_cmpstr (tpl_entity_get_alias (entity), ==, alias[1]);
  g_assert_cmpstr (tpl_entity_get_avatar_token (entity), ==, "");
  g_object_unref (entity);

  g_object_unref (alice);
  g_object_unref (bob);
  g_main_loop_unref (result.loop);

  tp_base_connection_change_status (base_connection,
      TP_CONNECTION_STATUS_DISCONNECTED,
      TP_CONNECTION_STATUS_REASON_REQUESTED);
  tp_base_connection_finish_shutdown (base_connection);

  g_object_unref (base_connection);
  g_object_unref (client_connection);
}

int main (int argc,
    char **argv)
{
  g_test_init (&argc, &argv, NULL);

  g_type_init ();

  g_test_add_func ("/entity/instantiation",
      test_entity_instantiation);

  g_test_add_func ("/entity/instantiation-from-room-id",
      test_entity_instantiation_from_room_id);

  g_test_add_func ("/entity/instantiation-from-tp-contact",
      test_entity_instantiation_from_tp_contact);

  return g_test_run ();
}