aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/src/com/android/tv/data/ChannelDataManagerTest.java
blob: 7a4a4982da0f3495d3d4da6ae99ed8d646fb476e (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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.data;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.InstrumentationRegistry.getTargetContext;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.media.tv.TvContract;
import android.media.tv.TvContract.Channels;
import android.net.Uri;
import android.support.test.filters.SmallTest;
import android.test.MoreAsserts;
import android.test.mock.MockContentProvider;
import android.test.mock.MockContentResolver;
import android.test.mock.MockCursor;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;

import com.android.tv.testing.ChannelInfo;
import com.android.tv.testing.Constants;
import com.android.tv.util.TvInputManagerHelper;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * Test for {@link ChannelDataManager}
 *
 * A test method may include tests for multiple methods to minimize the DB access.
 * Note that all the methods of {@link ChannelDataManager} should be called from the UI thread.
 */
@SmallTest
public class ChannelDataManagerTest {
    private static final boolean DEBUG = false;
    private static final String TAG = "ChannelDataManagerTest";

    // Wait time for expected success.
    private static final long WAIT_TIME_OUT_MS = 1000L;
    private static final String DUMMY_INPUT_ID = "dummy";
    private static final String COLUMN_BROWSABLE = "browsable";
    private static final String COLUMN_LOCKED = "locked";

    private ChannelDataManager mChannelDataManager;
    private TestChannelDataManagerListener mListener;
    private FakeContentResolver mContentResolver;
    private FakeContentProvider mContentProvider;

    @Before
    public void setUp() {
        assertTrue("More than 2 channels to test", Constants.UNIT_TEST_CHANNEL_COUNT > 2);

        mContentProvider = new FakeContentProvider(getTargetContext());
        mContentResolver = new FakeContentResolver();
        mContentResolver.addProvider(TvContract.AUTHORITY, mContentProvider);
        mListener = new TestChannelDataManagerListener();
        getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                TvInputManagerHelper mockHelper = Mockito.mock(TvInputManagerHelper.class);
                Mockito.when(mockHelper.hasTvInputInfo(Matchers.anyString())).thenReturn(true);
                mChannelDataManager = new ChannelDataManager(getTargetContext(), mockHelper,
                        mContentResolver);
                mChannelDataManager.addListener(mListener);
            }
        });
    }

    @After
    public void tearDown() {
        getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mChannelDataManager.stop();
            }
        });
    }

    private void startAndWaitForComplete() throws InterruptedException {
        getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mChannelDataManager.start();
            }
        });
        assertTrue(mListener.loadFinishedLatch.await(WAIT_TIME_OUT_MS, TimeUnit.MILLISECONDS));
    }

    private void restart() throws InterruptedException {
        getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mChannelDataManager.stop();
                mListener.reset();
            }
        });
        startAndWaitForComplete();
    }

    @Test
    public void testIsDbLoadFinished() throws InterruptedException {
        startAndWaitForComplete();
        assertTrue(mChannelDataManager.isDbLoadFinished());
    }

    /**
     * Test for following methods
     *   - {@link ChannelDataManager#getChannelCount}
     *   - {@link ChannelDataManager#getChannelList}
     *   - {@link ChannelDataManager#getChannel}
     */
    @Test
    public void testGetChannels() throws InterruptedException {
        startAndWaitForComplete();

        // Test {@link ChannelDataManager#getChannelCount}
        assertEquals(Constants.UNIT_TEST_CHANNEL_COUNT, mChannelDataManager.getChannelCount());

        // Test {@link ChannelDataManager#getChannelList}
        List<ChannelInfo> channelInfoList = new ArrayList<>();
        for (int i = 1; i <= Constants.UNIT_TEST_CHANNEL_COUNT; i++) {
            channelInfoList.add(ChannelInfo.create(getTargetContext(), i));
        }
        List<Channel> channelList = mChannelDataManager.getChannelList();
        for (Channel channel : channelList) {
            boolean found = false;
            for (ChannelInfo channelInfo : channelInfoList) {
                if (TextUtils.equals(channelInfo.name, channel.getDisplayName())
                        && TextUtils.equals(channelInfo.name, channel.getDisplayName())) {
                    found = true;
                    channelInfoList.remove(channelInfo);
                    break;
                }
            }
            assertTrue("Cannot find (" + channel + ")", found);
        }

        // Test {@link ChannelDataManager#getChannelIndex()}
        for (Channel channel : channelList) {
            assertEquals(channel, mChannelDataManager.getChannel(channel.getId()));
        }
    }

    /**
     * Test for {@link ChannelDataManager#getChannelCount} when no channel is available.
     */
    @Test
    public void testGetChannels_noChannels() throws InterruptedException {
        mContentProvider.clear();
        startAndWaitForComplete();
        assertEquals(0, mChannelDataManager.getChannelCount());
    }

    /**
     * Test for following methods and channel listener with notifying change.
     *   - {@link ChannelDataManager#updateBrowsable}
     *   - {@link ChannelDataManager#applyUpdatedValuesToDb}
     */
    @Test
    public void testBrowsable() throws InterruptedException {
        startAndWaitForComplete();

        // Test if all channels are browsable
        List<Channel> channelList = mChannelDataManager.getChannelList();
        List<Channel> browsableChannelList = mChannelDataManager.getBrowsableChannelList();
        for (Channel browsableChannel : browsableChannelList) {
            boolean found = channelList.remove(browsableChannel);
            assertTrue("Cannot find (" + browsableChannel + ")", found);
        }
        assertEquals(0, channelList.size());

        // Prepare for next tests.
        channelList = mChannelDataManager.getChannelList();
        TestChannelDataManagerChannelListener channelListener =
                new TestChannelDataManagerChannelListener();
        Channel channel1 = channelList.get(0);
        mChannelDataManager.addChannelListener(channel1.getId(), channelListener);

        // Test {@link ChannelDataManager#updateBrowsable} & notification.
        mChannelDataManager.updateBrowsable(channel1.getId(), false, false);
        assertTrue(mListener.channelBrowsableChangedCalled);
        assertFalse(mChannelDataManager.getBrowsableChannelList().contains(channel1));
        MoreAsserts.assertContentsInAnyOrder(channelListener.updatedChannels, channel1);
        channelListener.reset();

        // Test {@link ChannelDataManager#applyUpdatedValuesToDb}
        // Disable the update notification to avoid the unwanted call of "onLoadFinished".
        mContentResolver.mNotifyDisabled = true;
        mChannelDataManager.applyUpdatedValuesToDb();
        restart();
        browsableChannelList = mChannelDataManager.getBrowsableChannelList();
        assertEquals(Constants.UNIT_TEST_CHANNEL_COUNT - 1, browsableChannelList.size());
        assertFalse(browsableChannelList.contains(channel1));
    }

    /**
     * Test for following methods and channel listener without notifying change.
     *   - {@link ChannelDataManager#updateBrowsable}
     *   - {@link ChannelDataManager#applyUpdatedValuesToDb}
     */
    @Test
    public void testBrowsable_skipNotification() throws InterruptedException {
        startAndWaitForComplete();

        List<Channel> channels = mChannelDataManager.getChannelList();
        // Prepare for next tests.
        TestChannelDataManagerChannelListener channelListener =
                new TestChannelDataManagerChannelListener();
        Channel channel1 = channels.get(0);
        Channel channel2 = channels.get(1);
        mChannelDataManager.addChannelListener(channel1.getId(), channelListener);
        mChannelDataManager.addChannelListener(channel2.getId(), channelListener);

        // Test {@link ChannelDataManager#updateBrowsable} & skip notification.
        mChannelDataManager.updateBrowsable(channel1.getId(), false, true);
        mChannelDataManager.updateBrowsable(channel2.getId(), false, true);
        mChannelDataManager.updateBrowsable(channel1.getId(), true, true);
        assertFalse(mListener.channelBrowsableChangedCalled);
        List<Channel> browsableChannelList = mChannelDataManager.getBrowsableChannelList();
        assertTrue(browsableChannelList.contains(channel1));
        assertFalse(browsableChannelList.contains(channel2));

        // Test {@link ChannelDataManager#applyUpdatedValuesToDb}
        // Disable the update notification to avoid the unwanted call of "onLoadFinished".
        mContentResolver.mNotifyDisabled = true;
        mChannelDataManager.applyUpdatedValuesToDb();
        restart();
        browsableChannelList = mChannelDataManager.getBrowsableChannelList();
        assertEquals(Constants.UNIT_TEST_CHANNEL_COUNT - 1, browsableChannelList.size());
        assertFalse(browsableChannelList.contains(channel2));
    }

    /**
     * Test for following methods and channel listener.
     *   - {@link ChannelDataManager#updateLocked}
     *   - {@link ChannelDataManager#applyUpdatedValuesToDb}
     */
    @Test
    public void testLocked() throws InterruptedException {
        startAndWaitForComplete();

        // Test if all channels aren't locked at the first time.
        List<Channel> channelList = mChannelDataManager.getChannelList();
        for (Channel channel : channelList) {
            assertFalse(channel + " is locked", channel.isLocked());
        }

        // Prepare for next tests.
        Channel channel = mChannelDataManager.getChannelList().get(0);

        // Test {@link ChannelDataManager#updateLocked}
        mChannelDataManager.updateLocked(channel.getId(), true);
        assertTrue(mChannelDataManager.getChannel(channel.getId()).isLocked());

        // Test {@link ChannelDataManager#applyUpdatedValuesToDb}.
        // Disable the update notification to avoid the unwanted call of "onLoadFinished".
        mContentResolver.mNotifyDisabled = true;
        mChannelDataManager.applyUpdatedValuesToDb();
        restart();
        assertTrue(mChannelDataManager.getChannel(channel.getId()).isLocked());

        // Cleanup
        mChannelDataManager.updateLocked(channel.getId(), false);
    }

    /**
     * Test ChannelDataManager when channels in TvContract are updated, removed, or added.
     */
    @Test
    public void testChannelListChanged() throws InterruptedException {
        startAndWaitForComplete();

        // Test channel add.
        mListener.reset();
        long testChannelId = Constants.UNIT_TEST_CHANNEL_COUNT + 1;
        ChannelInfo testChannelInfo = ChannelInfo.create(getTargetContext(), (int) testChannelId);
        testChannelId = Constants.UNIT_TEST_CHANNEL_COUNT + 1;
        mContentProvider.simulateInsert(testChannelInfo);
        assertTrue(
                mListener.channelListUpdatedLatch.await(WAIT_TIME_OUT_MS, TimeUnit.MILLISECONDS));
        assertEquals(Constants.UNIT_TEST_CHANNEL_COUNT + 1, mChannelDataManager.getChannelCount());

        // Test channel update
        mListener.reset();
        TestChannelDataManagerChannelListener channelListener =
                new TestChannelDataManagerChannelListener();
        mChannelDataManager.addChannelListener(testChannelId, channelListener);
        String newName = testChannelInfo.name + "_test";
        mContentProvider.simulateUpdate(testChannelId, newName);
        assertTrue(
                mListener.channelListUpdatedLatch.await(WAIT_TIME_OUT_MS, TimeUnit.MILLISECONDS));
        assertTrue(
                channelListener.channelChangedLatch.await(WAIT_TIME_OUT_MS, TimeUnit.MILLISECONDS));
        assertEquals(0, channelListener.removedChannels.size());
        assertEquals(1, channelListener.updatedChannels.size());
        Channel updatedChannel = channelListener.updatedChannels.get(0);
        assertEquals(testChannelId, updatedChannel.getId());
        assertEquals(testChannelInfo.number, updatedChannel.getDisplayNumber());
        assertEquals(newName, updatedChannel.getDisplayName());
        assertEquals(Constants.UNIT_TEST_CHANNEL_COUNT + 1,
                mChannelDataManager.getChannelCount());

        // Test channel remove.
        mListener.reset();
        channelListener.reset();
        mContentProvider.simulateDelete(testChannelId);
        assertTrue(
                mListener.channelListUpdatedLatch.await(WAIT_TIME_OUT_MS, TimeUnit.MILLISECONDS));
        assertTrue(
                channelListener.channelChangedLatch.await(WAIT_TIME_OUT_MS, TimeUnit.MILLISECONDS));
        assertEquals(1, channelListener.removedChannels.size());
        assertEquals(0, channelListener.updatedChannels.size());
        Channel removedChannel = channelListener.removedChannels.get(0);
        assertEquals(newName, removedChannel.getDisplayName());
        assertEquals(testChannelInfo.number, removedChannel.getDisplayNumber());
        assertEquals(Constants.UNIT_TEST_CHANNEL_COUNT, mChannelDataManager.getChannelCount());
    }

    private class ChannelInfoWrapper {
        public ChannelInfo channelInfo;
        public boolean browsable;
        public boolean locked;
        public ChannelInfoWrapper(ChannelInfo channelInfo) {
            this.channelInfo = channelInfo;
            browsable = true;
            locked = false;
        }
    }

    private class FakeContentResolver extends MockContentResolver {
        boolean mNotifyDisabled;

        @Override
        public void notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork) {
            super.notifyChange(uri, observer, syncToNetwork);
            if (DEBUG) {
                Log.d(TAG, "onChanged(uri=" + uri + ", observer=" + observer + ") - Notification "
                        + (mNotifyDisabled ? "disabled" : "enabled"));
            }
            if (mNotifyDisabled) {
                return;
            }
            // Do not call {@link ContentObserver#onChange} directly to run it on the correct
            // thread.
            if (observer != null) {
                observer.dispatchChange(false, uri);
            } else {
                mChannelDataManager.getContentObserver().dispatchChange(false, uri);
            }
        }
    }

    // This implements the minimal methods in content resolver
    // and detailed assumptions are written in each method.
    private class FakeContentProvider extends MockContentProvider {
        private final SparseArray<ChannelInfoWrapper> mChannelInfoList = new SparseArray<>();

        public FakeContentProvider(Context context) {
            super(context);
            for (int i = 1; i <= Constants.UNIT_TEST_CHANNEL_COUNT; i++) {
                mChannelInfoList.put(i,
                        new ChannelInfoWrapper(ChannelInfo.create(getTargetContext(), i)));
            }
        }

        /**
         * Implementation of {@link ContentProvider#query}.
         * This assumes that {@link ChannelDataManager} queries channels
         * with empty {@code selection}. (i.e. channels are always queries for all)
         */
        @Override
        public Cursor query(Uri uri, String[] projection, String selection, String[]
                selectionArgs, String sortOrder) {
            if (DEBUG) {
                Log.d(TAG, "dump query");
                Log.d(TAG, "  uri=" + uri);
                Log.d(TAG, "  projection=" + Arrays.toString(projection));
                Log.d(TAG, "  selection=" + selection);
            }
            assertChannelUri(uri);
            return new FakeCursor(projection);
        }

        /**
         * Implementation of {@link ContentProvider#update}.
         * This assumes that {@link ChannelDataManager} update channels
         * only for changing browsable and locked.
         */
        @Override
        public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
            if (DEBUG) Log.d(TAG, "update(uri=" + uri + ", selection=" + selection);
            assertChannelUri(uri);
            List<Long> channelIds = new ArrayList<>();
            try {
                long channelId = ContentUris.parseId(uri);
                channelIds.add(channelId);
            } catch (NumberFormatException e) {
                // Update for multiple channels.
                if (TextUtils.isEmpty(selection)) {
                    for (int i = 0; i < mChannelInfoList.size(); i++) {
                        channelIds.add((long) mChannelInfoList.keyAt(i));
                    }
                } else {
                    // See {@link Utils#buildSelectionForIds} for the syntax.
                    String selectionForId = selection.substring(
                            selection.indexOf("(") + 1, selection.lastIndexOf(")"));
                    String[] ids = selectionForId.split(", ");
                    if (ids != null) {
                        for (String id : ids) {
                            channelIds.add(Long.parseLong(id));
                        }
                    }
                }
            }
            int updateCount = 0;
            for (long channelId : channelIds) {
                boolean updated = false;
                ChannelInfoWrapper channel = mChannelInfoList.get((int) channelId);
                if (channel == null) {
                    return 0;
                }
                if (values.containsKey(COLUMN_BROWSABLE)) {
                    updated = true;
                    channel.browsable = (values.getAsInteger(COLUMN_BROWSABLE) == 1);
                }
                if (values.containsKey(COLUMN_LOCKED)) {
                    updated = true;
                    channel.locked = (values.getAsInteger(COLUMN_LOCKED) == 1);
                }
                updateCount += updated ? 1 : 0;
            }
            if (updateCount > 0) {
                if (channelIds.size() == 1) {
                    mContentResolver.notifyChange(uri, null);
                } else {
                    mContentResolver.notifyChange(Channels.CONTENT_URI, null);
                }
            } else {
                if (DEBUG) {
                    Log.d(TAG, "Update to channel(uri=" + uri + ") is ignored for " + values);
                }
            }
            return updateCount;
        }

        /**
         * Simulates channel data insert.
         * This assigns original network ID (the same with channel number) to channel ID.
         */
        public void simulateInsert(ChannelInfo testChannelInfo) {
            long channelId = testChannelInfo.originalNetworkId;
            mChannelInfoList.put((int) channelId, new ChannelInfoWrapper(
                    ChannelInfo.create(getTargetContext(), (int) channelId)));
            mContentResolver.notifyChange(TvContract.buildChannelUri(channelId), null);
        }

        /**
         * Simulates channel data delete.
         */
        public void simulateDelete(long channelId) {
            mChannelInfoList.remove((int) channelId);
            mContentResolver.notifyChange(TvContract.buildChannelUri(channelId), null);
        }

        /**
         * Simulates channel data update.
         */
        public void simulateUpdate(long channelId, String newName) {
            ChannelInfoWrapper channel = mChannelInfoList.get((int) channelId);
            ChannelInfo.Builder builder = new ChannelInfo.Builder(channel.channelInfo);
            builder.setName(newName);
            channel.channelInfo = builder.build();
            mContentResolver.notifyChange(TvContract.buildChannelUri(channelId), null);
        }

        private void assertChannelUri(Uri uri) {
            assertTrue("Uri(" + uri + ") isn't channel uri",
                    uri.toString().startsWith(Channels.CONTENT_URI.toString()));
        }

        public void clear() {
            mChannelInfoList.clear();
        }

        public ChannelInfoWrapper get(int position) {
            return mChannelInfoList.get(mChannelInfoList.keyAt(position));
        }

        public int getCount() {
            return mChannelInfoList.size();
        }

        public long keyAt(int position) {
            return mChannelInfoList.keyAt(position);
        }
    }

    private class FakeCursor extends MockCursor {
        private final String[] ALL_COLUMNS =  {
                Channels._ID,
                Channels.COLUMN_DISPLAY_NAME,
                Channels.COLUMN_DISPLAY_NUMBER,
                Channels.COLUMN_INPUT_ID,
                Channels.COLUMN_VIDEO_FORMAT,
                Channels.COLUMN_ORIGINAL_NETWORK_ID,
                COLUMN_BROWSABLE,
                COLUMN_LOCKED};
        private final String[] mColumns;
        private int mPosition;

        public FakeCursor(String[] columns) {
            mColumns = (columns == null) ? ALL_COLUMNS : columns;
            mPosition = -1;
        }

        @Override
        public String getColumnName(int columnIndex) {
            return mColumns[columnIndex];
        }

        @Override
        public int getColumnIndex(String columnName) {
            for (int i = 0; i < mColumns.length; i++) {
                if (mColumns[i].equalsIgnoreCase(columnName)) {
                    return i;
                }
            }
            return -1;
        }

        @Override
        public long getLong(int columnIndex) {
            String columnName = getColumnName(columnIndex);
            switch (columnName) {
                case Channels._ID:
                    return mContentProvider.keyAt(mPosition);
            }
            if (DEBUG) {
                Log.d(TAG, "Column (" + columnName + ") is ignored in getLong()");
            }
            return 0;
        }

        @Override
        public String getString(int columnIndex) {
            String columnName = getColumnName(columnIndex);
            ChannelInfoWrapper channel = mContentProvider.get(mPosition);
            switch (columnName) {
                case Channels.COLUMN_DISPLAY_NAME:
                    return channel.channelInfo.name;
                case Channels.COLUMN_DISPLAY_NUMBER:
                    return channel.channelInfo.number;
                case Channels.COLUMN_INPUT_ID:
                    return DUMMY_INPUT_ID;
                case Channels.COLUMN_VIDEO_FORMAT:
                    return channel.channelInfo.getVideoFormat();
            }
            if (DEBUG) {
                Log.d(TAG, "Column (" + columnName + ") is ignored in getString()");
            }
            return null;
        }

        @Override
        public int getInt(int columnIndex) {
            String columnName = getColumnName(columnIndex);
            ChannelInfoWrapper channel = mContentProvider.get(mPosition);
            switch (columnName) {
                case Channels.COLUMN_ORIGINAL_NETWORK_ID:
                    return channel.channelInfo.originalNetworkId;
                case COLUMN_BROWSABLE:
                    return channel.browsable ? 1 : 0;
                case COLUMN_LOCKED:
                    return channel.locked ? 1 : 0;
            }
            if (DEBUG) {
                Log.d(TAG, "Column (" + columnName + ") is ignored in getInt()");
            }
            return 0;
        }

        @Override
        public int getCount() {
            return mContentProvider.getCount();
        }

        @Override
        public boolean moveToNext() {
            return ++mPosition < mContentProvider.getCount();
        }

        @Override
        public void close() {
            // No-op.
        }
    }

    private class TestChannelDataManagerListener implements ChannelDataManager.Listener {
        public CountDownLatch loadFinishedLatch = new CountDownLatch(1);
        public CountDownLatch channelListUpdatedLatch = new CountDownLatch(1);
        public boolean channelBrowsableChangedCalled;

        @Override
        public void onLoadFinished() {
            loadFinishedLatch.countDown();
        }

        @Override
        public void onChannelListUpdated() {
            channelListUpdatedLatch.countDown();
        }

        @Override
        public void onChannelBrowsableChanged() {
            channelBrowsableChangedCalled = true;
        }

        public void reset() {
            loadFinishedLatch = new CountDownLatch(1);
            channelListUpdatedLatch = new CountDownLatch(1);
            channelBrowsableChangedCalled = false;
        }
    }

    private class TestChannelDataManagerChannelListener
            implements ChannelDataManager.ChannelListener {
        public CountDownLatch channelChangedLatch = new CountDownLatch(1);
        public final List<Channel> removedChannels = new ArrayList<>();
        public final List<Channel> updatedChannels = new ArrayList<>();

        @Override
        public void onChannelRemoved(Channel channel) {
            removedChannels.add(channel);
            channelChangedLatch.countDown();
        }

        @Override
        public void onChannelUpdated(Channel channel) {
            updatedChannels.add(channel);
            channelChangedLatch.countDown();
        }

        public void reset() {
            channelChangedLatch = new CountDownLatch(1);
            removedChannels.clear();
            updatedChannels.clear();
        }
    }
}