1/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.paging
18
19import android.support.test.filters.SmallTest
20import io.reactivex.Observable
21import io.reactivex.observers.TestObserver
22import io.reactivex.schedulers.TestScheduler
23import org.junit.Assert.assertEquals
24import org.junit.Test
25import org.junit.runner.RunWith
26import org.junit.runners.JUnit4
27
28@SmallTest
29@RunWith(JUnit4::class)
30class RxPagedListBuilderTest {
31
32    /**
33     * Creates a data source that will sequentially supply the passed lists
34     */
35    private fun testDataSourceSequence(data: List<List<String>>): DataSource.Factory<Int, String> {
36        return object : DataSource.Factory<Int, String>() {
37            var localData = data
38            override fun create(): DataSource<Int, String> {
39                val currentList = localData.first()
40                localData = localData.drop(1)
41                return ListDataSource<String>(currentList)
42            }
43        }
44    }
45
46    @Test
47    fun basic() {
48        val factory = testDataSourceSequence(listOf(listOf("a", "b"), listOf("c", "d")))
49        val scheduler = TestScheduler()
50        val observable = RxPagedListBuilder(factory, 10)
51                .setFetchScheduler(scheduler)
52                .setNotifyScheduler(scheduler)
53                .buildObservable()
54        val observer = TestObserver<PagedList<String>>()
55
56        observable.subscribe(observer)
57
58        // initial state
59        observer.assertNotComplete()
60        observer.assertValueCount(0)
61
62        // load first item
63        scheduler.triggerActions()
64        observer.assertValueCount(1)
65        assertEquals(listOf("a", "b"), observer.values().first())
66
67        // invalidate triggers second load
68        observer.values().first().dataSource.invalidate()
69        scheduler.triggerActions()
70        observer.assertValueCount(2)
71        assertEquals(listOf("c", "d"), observer.values().last())
72    }
73
74    @Test
75    fun checkSchedulers() {
76        val factory = testDataSourceSequence(listOf(listOf("a", "b"), listOf("c", "d")))
77        val notifyScheduler = TestScheduler()
78        val fetchScheduler = TestScheduler()
79        val observable: Observable<PagedList<String>> = RxPagedListBuilder(
80                factory, 10)
81                .setFetchScheduler(fetchScheduler)
82                .setNotifyScheduler(notifyScheduler)
83                .buildObservable()
84        val observer = TestObserver<PagedList<String>>()
85        observable.subscribe(observer)
86
87        // notify has nothing to do
88        notifyScheduler.triggerActions()
89        observer.assertValueCount(0)
90
91        // fetch creates list, but observer doesn't see
92        fetchScheduler.triggerActions()
93        observer.assertValueCount(0)
94
95        // now notify reveals item
96        notifyScheduler.triggerActions()
97        observer.assertValueCount(1)
98    }
99}
100