Tyson Henning | ee3fb4b | 2018-05-16 15:51:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
| 17 | package androidx.room.integration.testapp.test; |
| 18 | |
Aurimas Liutikas | b7eeda4 | 2018-07-10 11:57:16 -0700 | [diff] [blame^] | 19 | import static androidx.test.InstrumentationRegistry.getInstrumentation; |
Tyson Henning | ee3fb4b | 2018-05-16 15:51:37 -0700 | [diff] [blame] | 20 | |
| 21 | import static org.hamcrest.CoreMatchers.is; |
| 22 | import static org.hamcrest.MatcherAssert.assertThat; |
| 23 | import static org.hamcrest.Matchers.greaterThan; |
| 24 | |
Tyson Henning | ee3fb4b | 2018-05-16 15:51:37 -0700 | [diff] [blame] | 25 | import androidx.arch.core.executor.ArchTaskExecutor; |
| 26 | import androidx.arch.core.executor.DefaultTaskExecutor; |
| 27 | import androidx.lifecycle.LiveData; |
| 28 | import androidx.room.Room; |
| 29 | import androidx.room.integration.testapp.TestDatabase; |
| 30 | import androidx.room.integration.testapp.dao.PetDao; |
| 31 | import androidx.room.integration.testapp.vo.Pet; |
Aurimas Liutikas | b7eeda4 | 2018-07-10 11:57:16 -0700 | [diff] [blame^] | 32 | import androidx.test.InstrumentationRegistry; |
| 33 | import androidx.test.filters.SmallTest; |
| 34 | import androidx.test.runner.AndroidJUnit4; |
Tyson Henning | ee3fb4b | 2018-05-16 15:51:37 -0700 | [diff] [blame] | 35 | |
| 36 | import com.google.common.base.Optional; |
| 37 | import com.google.common.util.concurrent.ListenableFuture; |
| 38 | import com.google.common.util.concurrent.SettableFuture; |
| 39 | |
| 40 | import org.junit.After; |
| 41 | import org.junit.Before; |
| 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
| 44 | |
| 45 | import java.util.ArrayDeque; |
| 46 | import java.util.Queue; |
| 47 | import java.util.concurrent.ExecutionException; |
| 48 | import java.util.concurrent.Executor; |
| 49 | import java.util.concurrent.ExecutorService; |
| 50 | import java.util.concurrent.Executors; |
| 51 | |
| 52 | @SmallTest |
| 53 | @RunWith(AndroidJUnit4.class) |
| 54 | public class QueryExecutorTest { |
| 55 | |
| 56 | /** |
| 57 | * Blocks the calling thread while executing input commands on a background thread. Also records |
| 58 | * executed tasks into a queue. |
| 59 | */ |
| 60 | private static class RecordingExecutor implements Executor { |
| 61 | private final Queue<Runnable> mTaskQueue = new ArrayDeque<>(); |
| 62 | |
| 63 | private ExecutorService mExecutorService = Executors.newFixedThreadPool(3); |
| 64 | |
| 65 | @Override |
| 66 | public void execute(Runnable command) { |
| 67 | try { |
| 68 | mExecutorService.submit(command).get(); |
| 69 | } catch (Exception e) { |
| 70 | throw new RuntimeException(e); |
| 71 | } |
| 72 | mTaskQueue.add(command); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private RecordingExecutor mRecordingExecutor; |
| 77 | private TestDatabase mTestDatabase; |
| 78 | private PetDao mPetDao; |
| 79 | |
| 80 | @Before |
| 81 | public void setUp() { |
| 82 | // Set ArchTaskExecutor to crash. It should never be invoked. |
| 83 | ArchTaskExecutor.getInstance().setDelegate(new DefaultTaskExecutor() { |
| 84 | @Override |
| 85 | public void executeOnDiskIO(Runnable runnable) { |
| 86 | throw new IllegalStateException( |
| 87 | "ArchTestExecutor was used instead of the set Executor."); |
| 88 | } |
| 89 | }); |
| 90 | |
| 91 | mRecordingExecutor = new RecordingExecutor(); |
| 92 | mTestDatabase = Room.inMemoryDatabaseBuilder( |
| 93 | InstrumentationRegistry.getContext(), |
| 94 | TestDatabase.class) |
| 95 | .setQueryExecutor(mRecordingExecutor) |
| 96 | .build(); |
| 97 | mPetDao = mTestDatabase.getPetDao(); |
| 98 | } |
| 99 | |
| 100 | @After |
| 101 | public void tearDown() { |
| 102 | mTestDatabase.close(); |
| 103 | } |
| 104 | |
| 105 | @Test |
| 106 | public void testInitDoesNotUseExecutor() { |
| 107 | assertThat( |
| 108 | mRecordingExecutor.mTaskQueue.isEmpty(), |
| 109 | is(true)); |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void testGuavaListenableFutureUsesExecutorOnQuery() |
| 114 | throws ExecutionException, InterruptedException { |
| 115 | mPetDao.insertOrReplace(TestUtil.createPet(2)); |
| 116 | ListenableFuture<Optional<Pet>> petsNamedFuture = mPetDao.petWithIdFuture(2); |
| 117 | |
| 118 | assertThat( |
| 119 | mRecordingExecutor.mTaskQueue.size(), |
| 120 | greaterThan(0)); |
| 121 | assertThat( |
| 122 | petsNamedFuture.get().isPresent(), |
| 123 | is(true)); |
| 124 | assertThat( |
| 125 | petsNamedFuture.get().get().getPetId(), |
| 126 | is(2)); |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void testRxUsesExecutorOnConsume() { |
| 131 | mPetDao.insertOrReplace(TestUtil.createPet(2)); |
| 132 | Pet pet = mPetDao.petWithIdFlowable(2).blockingFirst(); |
| 133 | |
| 134 | assertThat( |
| 135 | mRecordingExecutor.mTaskQueue.size(), |
| 136 | greaterThan(0)); |
| 137 | assertThat( |
| 138 | pet.getPetId(), |
| 139 | is(2)); |
| 140 | } |
| 141 | |
| 142 | @Test |
| 143 | public void testLiveDataUsesExecutorOnObserve() |
| 144 | throws ExecutionException, InterruptedException { |
| 145 | mPetDao.insertOrReplace(TestUtil.createPet(2)); |
| 146 | LiveData<Pet> petLiveData = mPetDao.petWithIdLiveData(2); |
| 147 | |
| 148 | SettableFuture<Pet> observedPet = SettableFuture.create(); |
| 149 | getInstrumentation().runOnMainSync(() -> petLiveData.observeForever(pet -> { |
| 150 | observedPet.set(pet); |
| 151 | })); |
| 152 | getInstrumentation().waitForIdleSync(); |
| 153 | |
| 154 | assertThat( |
| 155 | mRecordingExecutor.mTaskQueue.size(), |
| 156 | greaterThan(0)); |
| 157 | assertThat(petLiveData.getValue().getPetId(), is(2)); |
| 158 | assertThat(observedPet.get().getPetId(), is(2)); |
| 159 | } |
| 160 | } |