1/* 2 * Copyright 2017 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 org.junit.Assert.assertEquals 20import org.junit.Test 21import org.junit.runner.RunWith 22import org.junit.runners.JUnit4 23import org.mockito.ArgumentCaptor 24import org.mockito.ArgumentMatchers.eq 25import org.mockito.Mockito.mock 26import org.mockito.Mockito.verify 27import org.mockito.Mockito.verifyNoMoreInteractions 28import java.util.Collections 29 30@Suppress("DEPRECATION") 31@RunWith(JUnit4::class) 32class TiledDataSourceTest { 33 34 fun TiledDataSource<String>.loadInitial( 35 startPosition: Int, count: Int, pageSize: Int): List<String> { 36 @Suppress("UNCHECKED_CAST") 37 val receiver = mock(PageResult.Receiver::class.java) as PageResult.Receiver<String> 38 39 this.dispatchLoadInitial(true, startPosition, count, pageSize, FailExecutor(), receiver) 40 41 @Suppress("UNCHECKED_CAST") 42 val argument = ArgumentCaptor.forClass(PageResult::class.java) 43 as ArgumentCaptor<PageResult<String>> 44 verify(receiver).onPageResult(eq(PageResult.INIT), argument.capture()) 45 verifyNoMoreInteractions(receiver) 46 47 val observed = argument.value 48 49 return observed.page 50 } 51 52 @Test 53 fun loadInitialEmpty() { 54 class EmptyDataSource : TiledDataSource<String>() { 55 override fun countItems(): Int { 56 return 0 57 } 58 59 override fun loadRange(startPosition: Int, count: Int): List<String> { 60 return emptyList() 61 } 62 } 63 64 assertEquals(Collections.EMPTY_LIST, EmptyDataSource().loadInitial(0, 1, 5)) 65 } 66 67 @Test 68 fun loadInitialTooLong() { 69 val list = List(26) { "" + 'a' + it } 70 class AlphabetDataSource : TiledDataSource<String>() { 71 override fun countItems(): Int { 72 return list.size 73 } 74 75 override fun loadRange(startPosition: Int, count: Int): List<String> { 76 return list.subList(startPosition, startPosition + count) 77 } 78 } 79 // baseline behavior 80 assertEquals(list, AlphabetDataSource().loadInitial(0, 26, 10)) 81 assertEquals(list, AlphabetDataSource().loadInitial(50, 26, 10)) 82 } 83} 84