-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathtest_music_endpoint.c
More file actions
418 lines (341 loc) · 14.1 KB
/
test_music_endpoint.c
File metadata and controls
418 lines (341 loc) · 14.1 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* SPDX-FileCopyrightText: 2024 Google LLC */
/* SPDX-License-Identifier: Apache-2.0 */
#include "clar.h"
#include "pbl/services/music.h"
#include "pbl/services/music_endpoint.h"
#include "pbl/services/music_internal.h"
#include "pbl/services/comm_session/session.h"
#include "pbl/services/comm_session/session_remote_os.h"
#include "kernel/events.h"
#include "util/size.h"
// Stubs & Fakes
///////////////////////////////////////////////////////////
#include "fake_events.h"
#include "fake_rtc.h"
#include "fake_session.h"
#include "fake_system_task.h"
#include "stubs_app_manager.h"
#include "stubs_app_install_manager.h"
#include "stubs_passert.h"
#include "stubs_pbl_malloc.h"
#include "stubs_bt_lock.h"
#include "stubs_hexdump.h"
#include "stubs_logging.h"
#include "stubs_mutex.h"
#include "stubs_serial.h"
#include "stubs_tick.h"
extern void music_protocol_msg_callback(CommSession *session, const uint8_t* msg, size_t length);
// Helpers
///////////////////////////////////////////////////////////
static void prv_receive_app_info_event(bool is_android) {
const PebbleRemoteAppInfoEvent app_info_event = (const PebbleRemoteAppInfoEvent) {
.os = is_android ? RemoteOSAndroid : RemoteOSiOS,
};
music_endpoint_handle_mobile_app_info_event(&app_info_event);
}
static void prv_receive_app_event(bool is_open) {
const PebbleCommSessionEvent app_event = (const PebbleCommSessionEvent) {
.is_open = is_open,
.is_system = true
};
music_endpoint_handle_mobile_app_event(&app_event);
}
static void prv_receive_pp_data(const uint8_t *data, uint16_t length) {
fake_event_clear_last();
music_protocol_msg_callback(NULL, data, length);
}
static void prv_receive_and_assert_now_playing(bool expect_is_handled) {
uint8_t msg[] = { 0x10, 3, 'o', 'n', 'e', 3, 't', 'w', 'o', 5, 't', 'h', 'r', 'e', 'e', 0xAA,
0x00, 0x00, 0x00, 0xAA, 0x00, 0xAA, 0x00 };
prv_receive_pp_data(msg, sizeof(msg));
PebbleEvent e = fake_event_get_last();
if (expect_is_handled) {
cl_assert_equal_i(e.type, PEBBLE_MEDIA_EVENT);
cl_assert_equal_i(e.media.type, PebbleMediaEventTypeTrackPosChanged);
char artist[MUSIC_BUFFER_LENGTH];
char album[MUSIC_BUFFER_LENGTH];
char title[MUSIC_BUFFER_LENGTH];
music_get_now_playing(title, artist, album);
cl_assert_equal_s(artist, "one");
cl_assert_equal_s(album, "two");
cl_assert_equal_s(title, "three");
uint32_t track_position, track_duration;
music_get_pos(&track_position, &track_duration);
cl_assert_equal_i(track_duration, 0xAA);
} else {
cl_assert_equal_i(e.type, PEBBLE_NULL_EVENT);
}
}
static void prv_receive_and_assert_play_state(bool expect_is_handled) {
uint8_t msg[] = { 0x11, 0x01, 0xAA, 0x00, 0x00, 0x00, 0xAA, 0x00, 0x00, 0x00, 0x01, 0x01 };
prv_receive_pp_data(msg, sizeof(msg));
PebbleEvent e = fake_event_get_last();
if (expect_is_handled) {
cl_assert_equal_i(e.type, PEBBLE_MEDIA_EVENT);
cl_assert_equal_i(e.media.type, PebbleMediaEventTypeTrackPosChanged);
cl_assert_equal_i(music_get_playback_state(), 0x01);
uint32_t track_position, track_duration;
music_get_pos(&track_position, &track_duration);
cl_assert_equal_i(track_position, 0xAA);
cl_assert_equal_i(music_get_playback_rate_percent(), 0xAA);
} else {
cl_assert_equal_i(e.type, PEBBLE_NULL_EVENT);
}
}
static void prv_receive_and_assert_volume_info(bool expect_is_handled) {
uint8_t msg[] = { 0x12, 0x33 };
prv_receive_pp_data(msg, sizeof(msg));
PebbleEvent e = fake_event_get_last();
if (expect_is_handled) {
cl_assert_equal_i(e.type, PEBBLE_MEDIA_EVENT);
cl_assert_equal_i(e.media.type, PebbleMediaEventTypeVolumeChanged);
cl_assert_equal_i(music_service_get_volume_percent(), 0x33);
} else {
cl_assert_equal_i(e.type, PEBBLE_NULL_EVENT);
}
}
static void prv_receive_and_assert_player_info(bool expect_is_handled) {
uint8_t msg[] = { 0x13, 17, 'c', 'o', 'm', '.', 's', 'p', 'o', 't', 'i', 'f', 'y', '.', 'm',
'u', 's', 'i', 'c', 7, 'S', 'p', 'o', 't', 'i', 'f', 'y' };
prv_receive_pp_data(msg, sizeof(msg));
PebbleEvent e = fake_event_get_last();
if (expect_is_handled) {
cl_assert_equal_i(e.type, PEBBLE_MEDIA_EVENT);
cl_assert_equal_i(e.media.type, PebbleMediaEventTypeNowPlayingChanged);
char player_name[MUSIC_BUFFER_LENGTH];
music_get_player_name(player_name);
cl_assert_equal_s(player_name, "Spotify");
} else {
cl_assert_equal_i(e.type, PEBBLE_NULL_EVENT);
}
}
static void prv_receive_and_assert_all(bool expect_is_handled) {
prv_receive_and_assert_now_playing(expect_is_handled);
prv_receive_and_assert_play_state(expect_is_handled);
prv_receive_and_assert_volume_info(expect_is_handled);
prv_receive_and_assert_player_info(expect_is_handled);
}
static const MusicServerImplementation s_dummy_server_implementation = {};
static void prv_set_dummy_server_connected(bool connected) {
music_set_connected_server(&s_dummy_server_implementation,
connected /* connected */);
}
static void prv_assert_no_data_sent_cb(uint16_t endpoint_id,
const uint8_t* data, unsigned int data_length) {
cl_assert(false);
}
static bool s_now_playing_requested;
static void prv_assert_now_playing_requested_cb(uint16_t endpoint_id,
const uint8_t* data, unsigned int data_length) {
cl_assert_equal_i(data_length, 1);
cl_assert_equal_i(data[0], 0x08); // MusicEndpointCmdIDGetAllInfo
s_now_playing_requested = true;
}
static bool s_next_track_command_sent;
static void prv_assert_next_track_command_sent_cb(uint16_t endpoint_id,
const uint8_t* data, unsigned int data_length) {
cl_assert_equal_i(data_length, 1);
cl_assert_equal_i(data[0], 0x04); // MusicEndpointCmdIDNextTrack
s_next_track_command_sent = true;
}
static bool s_is_playback_cmd_sent;
static uint8_t s_playback_cmd_sent;
static void prv_assert_playback_command_sent_cb(uint16_t endpoint_id,
const uint8_t* data, unsigned int data_length) {
cl_assert_equal_i(data_length, 1);
if (!s_is_playback_cmd_sent) {
s_playback_cmd_sent = data[0];
s_is_playback_cmd_sent = true;
} else {
// Playback command is always followed by:
cl_assert_equal_i(data[0], 0x08); // MusicEndpointCmdIDGetAllInfo
}
}
// Tests
///////////////////////////////////////////////////////////
Transport *s_transport;
void test_music_endpoint__initialize(void) {
s_now_playing_requested = false;
s_next_track_command_sent = false;
s_is_playback_cmd_sent = false;
s_playback_cmd_sent = ~0;
fake_event_init();
fake_rtc_init(0, 0);
fake_comm_session_init();
music_init();
s_transport = fake_transport_create(TransportDestinationSystem, NULL, NULL);
fake_transport_set_connected(s_transport, true /* connected */);
// Simulate connecting Pebble mobile app:
prv_receive_app_event(true /* is_open */);
}
void test_music_endpoint__cleanup(void) {
fake_comm_session_process_send_next();
// Simulate disconnecting Pebble mobile app:
prv_receive_app_event(false /* is_open */);
fake_comm_session_cleanup();
}
void test_music_endpoint__ignore_now_playing_while_not_connected(void) {
// Don't connect app, but receive Now Playing info. Should be ignored:
prv_receive_and_assert_all(false /* expect_is_handled */);
}
void test_music_endpoint__ignore_now_playing_while_other_server_connected(void) {
// Another music server connects:
prv_set_dummy_server_connected(true /* connected */);
// Android app connects:
prv_receive_app_info_event(true /* is_android */);
// Receive Now Playing info. Should be ignored, because other server is connected:
prv_receive_and_assert_all(false /* expect_is_handled*/);
// Disconnect dummy server, to clean up after ourselves:
prv_set_dummy_server_connected(false /* connected */);
}
void test_music_endpoint__ignore_now_playing_from_ios_app(void) {
// iOS app connects:
prv_receive_app_info_event(false /* is_android */);
// iOS app is not supposed to use this endpoint:
prv_receive_and_assert_all(false /* expect_is_handled*/);
}
void test_music_endpoint__request_now_playing_upon_connect(void) {
fake_transport_set_sent_cb(s_transport, &prv_assert_now_playing_requested_cb);
// Android app connects:
prv_receive_app_info_event(true /* is_android */);
fake_comm_session_process_send_next();
cl_assert_equal_b(s_now_playing_requested, true);
}
void test_music_endpoint__receive_now_playing_while_connected(void) {
// Android app connects:
prv_receive_app_info_event(true /* is_android */);
prv_receive_and_assert_all(true /* expect_is_handled*/);
}
void test_music_endpoint__ignore_unknown_message(void) {
// Android app connects:
prv_receive_app_info_event(true /* is_android */);
uint8_t unknown_msg[] = { 0xff };
prv_receive_pp_data(unknown_msg, sizeof(unknown_msg));
PebbleEvent e = fake_event_get_last();
cl_assert_equal_i(e.type, PEBBLE_NULL_EVENT);
}
void test_music_endpoint__receive_zero_length_now_playing(void) {
// Android app connects:
prv_receive_app_info_event(true /* is_android */);
prv_receive_and_assert_all(true /* expect_is_handled*/);
cl_assert_equal_b(music_has_now_playing(), true);
uint8_t zero_length_now_playing[] = { 0x10, 0, 0, 0 };
prv_receive_pp_data(zero_length_now_playing, sizeof(zero_length_now_playing));
cl_assert_equal_b(music_has_now_playing(), false);
}
void test_music_endpoint__ignore_malformatted_messages(void) {
// Android app connects:
prv_receive_app_info_event(true /* is_android */);
const uint8_t malformatted_artist[] = {
0x10, 14, 'o', 'n', 'e', 3, 't', 'w', 'o', 5, 't', 'h', 'r', 'e', 'e'
};
const uint8_t malformatted_album[] = {
0x10, 3, 'o', 'n', 'e', 10, 't', 'w', 'o', 5, 't', 'h', 'r', 'e', 'e'
};
const uint8_t malformatted_title[] = {
0x10, 3, 'o', 'n', 'e', 3, 't', 'w', 'o', 6, 't', 'h', 'r', 'e', 'e'
};
const uint8_t malformatted_player[] = {
0x13, 17, 'c', 'o', 'm', '.', 's', 'p', 'o', 't', 'i', 'f', 'y', '.', 'm', 'u', 's', 'i', 'c',
9, 'S', 'p', 'o', 't', 'i', 'f', 'y'
};
struct {
const uint8_t *data;
uint16_t length;
} test_vectors[] = {
{ malformatted_artist, sizeof(malformatted_artist) },
{ malformatted_album, sizeof(malformatted_album) },
{ malformatted_title, sizeof(malformatted_title) },
{ malformatted_player, sizeof(malformatted_player) }
};
for (int i = 0; i < ARRAY_LENGTH(test_vectors); ++i) {
prv_receive_pp_data(test_vectors[i].data, test_vectors[i].length);
PebbleEvent e = fake_event_get_last();
cl_assert_equal_i(e.type, PEBBLE_NULL_EVENT);
}
}
void test_music_endpoint__supported_capabilities(void) {
// Android app connects:
prv_receive_app_info_event(true /* is_android */);
// music_is_progress_reporting_supported() relies on a valid track duration
prv_receive_and_assert_all(true /* expect_is_handled*/);
cl_assert_equal_b(music_is_playback_state_reporting_supported(), true);
cl_assert_equal_b(music_is_progress_reporting_supported(), true);
cl_assert_equal_b(music_is_volume_reporting_supported(), true);
cl_assert_equal_b(music_needs_user_to_start_playback_on_phone(), false);
for (MusicCommand cmd = 0; cmd < NumMusicCommand; ++cmd) {
bool expect_supported = true;
if (cmd == MusicCommandAdvanceRepeatMode ||
cmd == MusicCommandAdvanceShuffleMode ||
cmd == MusicCommandSkipForward ||
cmd == MusicCommandSkipBackward ||
cmd == MusicCommandLike ||
cmd == MusicCommandDislike ||
cmd == MusicCommandBookmark) {
expect_supported = false;
}
cl_assert_equal_b(music_is_command_supported(cmd), expect_supported);
}
}
void test_music_endpoint__reduced_latency(void) {
// Android app connects:
prv_receive_app_info_event(true /* is_android */);
cl_assert_equal_b(fake_comm_session_is_latency_reduced(), false);
music_request_reduced_latency(true);
cl_assert_equal_b(fake_comm_session_is_latency_reduced(), true);
music_request_reduced_latency(false);
cl_assert_equal_b(fake_comm_session_is_latency_reduced(), false);
}
void test_music_endpoint__low_latency_for_period(void) {
// Android app connects:
prv_receive_app_info_event(true /* is_android */);
cl_assert_equal_i(fake_comm_session_get_responsiveness_max_period(), 0);
music_request_low_latency_for_period(2000);
cl_assert_equal_i(fake_comm_session_get_responsiveness_max_period(), 2);
}
void test_music_endpoint__send_unsupported_command(void) {
// Android app connects:
prv_receive_app_info_event(true /* is_android */);
fake_comm_session_process_send_next(); // send out any pending data
// Attempting to send an unsupported command should not result in any data getting sent out:
fake_transport_set_sent_cb(s_transport, prv_assert_no_data_sent_cb);
music_command_send(MusicCommandAdvanceRepeatMode);
fake_comm_session_process_send_next();
}
void test_music_endpoint__send_next_track_command(void) {
// Android app connects:
prv_receive_app_info_event(true /* is_android */);
fake_comm_session_process_send_next(); // send out any pending data
fake_transport_set_sent_cb(s_transport, prv_assert_next_track_command_sent_cb);
music_command_send(MusicCommandNextTrack);
fake_comm_session_process_send_next();
cl_assert_equal_b(s_next_track_command_sent, true);
}
void test_music_endpoint__send_playback_command(void) {
// Android app connects:
prv_receive_app_info_event(true /* is_android */);
fake_comm_session_process_send_next(); // send out any pending data
MusicCommand cmds[] = {
MusicCommandTogglePlayPause,
MusicCommandPause,
MusicCommandPlay,
};
uint8_t opcodes[] = {
0x1,
0x2,
0x3,
};
fake_transport_set_sent_cb(s_transport, prv_assert_playback_command_sent_cb);
for (int i = 0; i < ARRAY_LENGTH(cmds); ++i) {
music_command_send(cmds[i]);
fake_comm_session_process_send_next();
cl_assert_equal_i(s_is_playback_cmd_sent, true);
cl_assert_equal_i(s_playback_cmd_sent, opcodes[i]);
s_is_playback_cmd_sent = false;
}
}
void test_music_endpoint__player_name_not_available(void) {
// Android app connects:
prv_receive_app_info_event(true /* is_android */);
cl_assert_equal_b(music_get_player_name(NULL), false);
}