diff --git a/core/audio.cpp b/core/audio.cpp index c63ed3dd..29a47238 100644 --- a/core/audio.cpp +++ b/core/audio.cpp @@ -145,7 +145,7 @@ void audio :: load_track(struct track *track) return; _load_track(track, cur_driver->is_playing()); - queue_add(history_get_queue(), cur_track); + history_add(cur_track); } struct track *audio :: current_track() diff --git a/core/deck.cpp b/core/deck.cpp index 78bbad81..018eeb1d 100644 --- a/core/deck.cpp +++ b/core/deck.cpp @@ -217,12 +217,8 @@ struct track *deck :: next() if (!track) track = queue_next(collection_get_queue()); - if (track) { - queue_remove_all(history_get_queue(), track); - queue_add(history_get_queue(), track); - _q_iter_set(&history_get_queue()->q_tracks, - &history_get_queue()->q_cur, 0); - } + if (track) + history_add(track); return track; } diff --git a/core/history.c b/core/history.c index 8ce54345..4e92a093 100644 --- a/core/history.c +++ b/core/history.c @@ -17,6 +17,12 @@ void history_deinit() queue_deinit(&history_queue); } +void history_add(struct track *track) +{ + queue_add(&history_queue, track); + _q_iter_set(&history_queue.q_tracks, &history_queue.q_cur, 0); +} + struct queue *history_get_queue() { return &history_queue; diff --git a/include/core/history.h b/include/core/history.h index 4eb5e730..5a837389 100644 --- a/include/core/history.h +++ b/include/core/history.h @@ -13,6 +13,10 @@ void history_init(struct queue_ops *); void history_deinit(); +/* Called to add a track to the history queue. */ +void history_add(struct track *); + + /* Called to access the queue of recent tracks. */ struct queue *history_get_queue(); diff --git a/tests/core/deck.cpp b/tests/core/deck.cpp index 2a62e64d..f2d65114 100644 --- a/tests/core/deck.cpp +++ b/tests/core/deck.cpp @@ -123,7 +123,7 @@ static void test_next_prev() test_equal(deck :: next()->tr_dbe.dbe_index, (unsigned)1); test_equal(deck :: next()->tr_dbe.dbe_index, (unsigned)2); test_equal(deck :: next()->tr_dbe.dbe_index, (unsigned)3); - test_equal(queue_size(q), (unsigned)4); + test_equal(queue_size(q), 4 * (i + 1)); } for (unsigned int i = 0; i < 2; i++) { diff --git a/tests/core/history.c b/tests/core/history.c index 8c97f35f..0d9a7016 100644 --- a/tests/core/history.c +++ b/tests/core/history.c @@ -2,12 +2,22 @@ * Copyright 2015 (c) Anna Schumaker. */ #include +#include +#include +#include +#include +#include +#include #include static void test_init() { struct queue *q = history_get_queue(); + filter_init(); + tags_init(); + playlist_init(NULL); + collection_init(NULL); history_init(NULL); test_not_equal((void *)q, NULL); @@ -20,9 +30,46 @@ static void test_init() test_equal((void *)q->q_sort, NULL); test_equal(queue_size(q), 0); + collection_add("tests/Music/Hyrule Symphony"); + while (idle_run_task()) {}; +} + +static void test_history() +{ + const struct database *track_db = track_db_get(); + struct queue *q = history_get_queue(); + struct db_entry *track, *next; + unsigned int i = 0; + + /* Add tracks once */ + db_for_each(track, next, track_db) { + history_add(TRACK(track)); + test_loop_equal(queue_size(q), i + 1, i); + test_loop_equal((void *)queue_at(q, 0), (void *)TRACK(track), i); + i++; + } test_loop_passed(); + test_equal(queue_size(q), track_db->db_size); + + i = 0; + /* Add tracks again, old tracks should remain */ + db_for_each(track, next, track_db) { + history_add(TRACK(track)); + test_loop_equal(queue_size(q), track_db->db_size + i + 1, i); + test_loop_equal((void *)queue_at(q, 0), (void *)TRACK(track), i); + test_loop_equal((void *)queue_at(q, track_db->db_size), + (void *)TRACK(track), i); + i++; + } test_loop_passed(); + test_equal(queue_size(q), 2 * track_db->db_size); + history_deinit(); + collection_deinit(); + playlist_deinit(); + tags_deinit(); + filter_deinit(); } DECLARE_UNIT_TESTS( UNIT_TEST("History Initialization", test_init), + UNIT_TEST("History Queue", test_history), );