View Javadoc

1   /***
2    *     Ambient - A music player for the Android platform
3    Copyright (C) 2007 Martin Vysny
4    
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9    
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14  
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  package sk.baka.ambient.playlist;
19  
20  import java.util.Arrays;
21  
22  import sk.baka.ambient.collection.IDynamicPlaylistTrackProvider;
23  import sk.baka.ambient.collection.TrackMetadataBean;
24  import sk.baka.ambient.commons.Interval;
25  
26  /***
27   * Tests the {@link DynamicPlaylistStrategy}.
28   * 
29   * @author mvy
30   */
31  public class DynamicPlaylistStrategyTest extends AbstractPlaylistStrategyTest {
32  
33  	@Override
34  	protected IPlaylistStrategy getStrategy() {
35  		return new DynamicPlaylistStrategy(new CyclicProvider(), null, 0, 0);
36  	}
37  
38  	private final class CyclicProvider implements IDynamicPlaylistTrackProvider {
39  		/***
40  		 * 
41  		 */
42  		private static final long serialVersionUID = 787560300757471755L;
43  		private int i = 0;
44  
45  		public void removeFromHistory(int trackCount) {
46  			// do nothing
47  		}
48  
49  		public void setRandom(Random random, TrackMetadataBean track) {
50  			// do nothing
51  		}
52  
53  		public boolean hasNext() {
54  			return true;
55  		}
56  
57  		public TrackMetadataBean next() {
58  			final TrackMetadataBean result = tracks.get(i++);
59  			if (i >= tracks.size())
60  				i = 0;
61  			return result;
62  		}
63  
64  		public void remove() {
65  			throw new UnsupportedOperationException();
66  		}
67  
68  		public void reset() {
69  			// do nothing
70  		}
71  
72  		public void close() {
73  			// do nothing
74  		}
75  	}
76  
77  	/***
78  	 * 
79  	 */
80  	public void testRequeueTracks() {
81  		strategy.add(0, tracks);
82  		Interval queueInterval = new Interval(5, 5);
83  		strategy.queue(queueInterval);
84  		verifyQueueProperties(5);
85  		assertEquals(new Object[] { 0, 1, 2, 3, 4 }, strategy.getQueue());
86  		strategy.queue(queueInterval);
87  		verifyQueueProperties(10);
88  		assertEquals(new Object[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, strategy
89  				.getQueue());
90  		strategy.queue(queueInterval);
91  		verifyQueueProperties(10);
92  		assertEquals(new Object[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, strategy
93  				.getQueue());
94  	}
95  
96  	/***
97  	 * When the currently played track is removed from the playlist, current
98  	 * track must be set to -1.
99  	 */
100 	public void testHistoryLength() {
101 		DynamicPlaylistStrategy strategy = new DynamicPlaylistStrategy(
102 				new CyclicProvider(), null, 10, 5);
103 		strategy.add(0, tracks);
104 		assertEquals(0, strategy.getCurrentHistoryLength());
105 		for (int i = 0; i < 5; i++)
106 			strategy.next();
107 		assertEquals(4, strategy.getCurrentHistoryLength());
108 		for (int i = 0; i < 15; i++)
109 			strategy.next();
110 		assertEquals(10, strategy.getCurrentHistoryLength());
111 		strategy = new DynamicPlaylistStrategy(new CyclicProvider(), null, 0, 5);
112 		strategy.add(0, tracks);
113 		assertEquals(0, strategy.getCurrentHistoryLength());
114 		for (int i = 0; i < 5; i++)
115 			strategy.next();
116 		assertEquals(0, strategy.getCurrentHistoryLength());
117 		for (int i = 0; i < 15; i++)
118 			strategy.next();
119 		assertEquals(0, strategy.getCurrentHistoryLength());
120 	}
121 
122 	/***
123 	 * When the currently played track is removed from the playlist, current
124 	 * track must be set to -1.
125 	 */
126 	public void testRemovingPlayedTrack() {
127 		DynamicPlaylistStrategy strategy = new DynamicPlaylistStrategy(
128 				new CyclicProvider(), null, 10, 5);
129 		strategy.add(0, tracks);
130 		for (int i = 0; i < 15; i++)
131 			strategy.next();
132 		strategy.remove(new Interval(5, 10));
133 		assertEquals(-1, strategy.getCurrentlyPlaying());
134 	}
135 
136 	/***
137 	 * 
138 	 */
139 	public void testMoveTracksBefore() {
140 		// try simple move
141 		strategy.add(0, tracks);
142 		Interval result = strategy.move(new Interval(5, 5), 1);
143 		assertEquals(new Interval(1, 5), result);
144 		assertTracks(Arrays.asList(0, 5, 6, 7, 8, 9, 1, 2, 3, 4, 10, 11, 12,
145 				13, 14, 15, 16), 0);
146 		// reinit strategy and try moving over currently played and queued
147 		// tracks
148 		strategy = new DynamicPlaylistStrategy(new CyclicProvider(), null, 10,
149 				5);
150 		assertNextTracks(tracks.subList(0, 11), false);
151 		assertEquals(10, strategy.getCurrentlyPlaying());
152 		strategy.queue(new Interval(11, 2));
153 		result = strategy.move(new Interval(9, 6), 2);
154 		assertEquals(new Interval(2, 3), result);
155 		assertTracks(Arrays.asList(0, 1, 9, 13, 14, 2, 3, 4, 5, 6, 7, 8, 10,
156 				11, 12, 15), 0);
157 	}
158 
159 	/***
160 	 * 
161 	 */
162 	public void testMoveTracksBeforeStart() {
163 		// try simple move
164 		strategy.add(0, tracks);
165 		Interval result = strategy.moveBy(new Interval(0, 3), -1);
166 		assertEquals(new Interval(0, 3), result);
167 		assertEquals(tracks, strategy.getPlayItems());
168 		// try move over played/queued items
169 		strategy = new DynamicPlaylistStrategy(new CyclicProvider(), null, 10,
170 				6);
171 		assertEquals(0, strategy.next());
172 		strategy.queue(new Interval(1, 3));
173 		result = strategy.moveBy(new Interval(2, 4), -1);
174 		assertEquals(new Interval(0, 2), result);
175 		assertTracks(Arrays.asList(4, 5, 0, 1, 2, 3, 6), 0);
176 	}
177 
178 	/***
179 	 * 
180 	 */
181 	public void testMoveTracksDown() {
182 		strategy = new DynamicPlaylistStrategy(new CyclicProvider(), null, 10,
183 				5);
184 		Interval result = strategy.moveBy(new Interval(0, 1), 1);
185 		assertEquals(new Interval(1, 1), result);
186 		assertEquals(tracks.get(0), strategy.getPlayItems().get(1).track);
187 		// try simple move
188 		strategy = new DynamicPlaylistStrategy(new CyclicProvider(), null, 10,
189 				5);
190 		strategy.next();
191 		assertEquals(1, strategy.next());
192 		result = strategy.moveBy(new Interval(0, 1), 1);
193 		assertEquals(0, strategy.getCurrentlyPlaying());
194 		assertEquals(new Interval(1, 1), result);
195 		assertTracks(Arrays.asList(1, 0, 2, 3, 4, 5, 6), 0);
196 		// move selection started in a queued track
197 		strategy = new DynamicPlaylistStrategy(new CyclicProvider(), null, 10,
198 				5);
199 		strategy.next();
200 		assertEquals(1, strategy.next());
201 		strategy.queue(Interval.fromItem(2));
202 		result = strategy.moveBy(new Interval(2, 2), 1);
203 		assertEquals(new Interval(4, 1), result);
204 		assertTracks(Arrays.asList(0, 1, 2, 4, 3, 5, 6), 0);
205 	}
206 
207 	/***
208 	 * 
209 	 */
210 	public void testMoveTracksUp() {
211 		// try simple move
212 		strategy = new DynamicPlaylistStrategy(new CyclicProvider(), null, 10,
213 				5);
214 		strategy.next();
215 		assertEquals(1, strategy.next());
216 		Interval result = strategy.moveBy(new Interval(2, 2), -1);
217 		assertEquals(new Interval(1, 2), result);
218 		assertTracks(Arrays.asList(0, 2, 3, 1, 4, 5, 6), 0);
219 	}
220 }