12e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar/* 22e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar * Copyright (C) 2017 The Android Open Source Project 32e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar * 42e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License"); 52e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar * you may not use this file except in compliance with the License. 62e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar * You may obtain a copy of the License at 72e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar * 82e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar * http://www.apache.org/licenses/LICENSE-2.0 92e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar * 102e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar * Unless required by applicable law or agreed to in writing, software 112e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS, 122e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar * See the License for the specific language governing permissions and 142e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar * limitations under the License. 152e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar */ 162e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar 17bdc4c86d3dff74f6634a38e2f7b316b0e823a2c8Alan Viverettepackage androidx.room.integration.testapp.test; 182e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar 192e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport static org.hamcrest.CoreMatchers.instanceOf; 202e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport static org.hamcrest.CoreMatchers.notNullValue; 212e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport static org.hamcrest.CoreMatchers.nullValue; 222e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport static org.hamcrest.MatcherAssert.assertThat; 232e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar 242e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport android.content.Context; 252e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport android.support.test.InstrumentationRegistry; 262e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport android.support.test.filters.SmallTest; 272e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport android.support.test.runner.AndroidJUnit4; 282e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar 29bdc4c86d3dff74f6634a38e2f7b316b0e823a2c8Alan Viveretteimport androidx.arch.core.util.Function; 30bdc4c86d3dff74f6634a38e2f7b316b0e823a2c8Alan Viveretteimport androidx.room.Room; 31bdc4c86d3dff74f6634a38e2f7b316b0e823a2c8Alan Viveretteimport androidx.room.RoomDatabase; 32bdc4c86d3dff74f6634a38e2f7b316b0e823a2c8Alan Viveretteimport androidx.room.integration.testapp.TestDatabase; 33bdc4c86d3dff74f6634a38e2f7b316b0e823a2c8Alan Viverette 342e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport org.junit.Test; 352e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport org.junit.runner.RunWith; 362e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar 372e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport java.util.concurrent.atomic.AtomicReference; 382e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar 392e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar@RunWith(AndroidJUnit4.class) 402e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar@SmallTest 412e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarpublic class MainThreadCheckTest { 422e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar 432e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar @Test 442e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar public void testMainThread() { 452e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar final Throwable error = test(false, new Function<TestDatabase, Void>() { 462e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar @Override 472e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar public Void apply(TestDatabase db) { 482e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar db.getUserDao().load(3); 492e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar return null; 502e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar } 512e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar }); 522e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar assertThat(error, notNullValue()); 532e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar assertThat(error, instanceOf(IllegalStateException.class)); 542e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar } 552e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar 562e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar @Test 572e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar public void testFlowableOnMainThread() { 582e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar final Throwable error = test(false, new Function<TestDatabase, Void>() { 592e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar @Override 602e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar public Void apply(TestDatabase db) { 612e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar db.getUserDao().flowableUserById(3); 622e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar return null; 632e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar } 642e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar }); 652e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar assertThat(error, nullValue()); 662e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar } 672e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar 682e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar @Test 692e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar public void testLiveDataOnMainThread() { 702e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar final Throwable error = test(false, new Function<TestDatabase, Void>() { 712e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar @Override 722e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar public Void apply(TestDatabase db) { 732e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar db.getUserDao().liveUserById(3); 742e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar return null; 752e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar } 762e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar }); 772e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar assertThat(error, nullValue()); 782e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar } 792e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar 802e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar @Test 812e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar public void testAllowMainThread() { 822e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar final Throwable error = test(true, new Function<TestDatabase, Void>() { 832e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar @Override 842e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar public Void apply(TestDatabase db) { 852e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar db.getUserDao().load(3); 862e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar return null; 872e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar } 882e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar }); 892e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar assertThat(error, nullValue()); 902e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar } 912e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar 922e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar private Throwable test(boolean allowMainThread, final Function<TestDatabase, Void> fun) { 932e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar Context context = InstrumentationRegistry.getTargetContext(); 942e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar final RoomDatabase.Builder<TestDatabase> builder = Room.inMemoryDatabaseBuilder( 952e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar context, TestDatabase.class); 962e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar if (allowMainThread) { 972e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar builder.allowMainThreadQueries(); 982e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar } 992e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar final TestDatabase db = builder.build(); 1002e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar final AtomicReference<Throwable> error = new AtomicReference<>(); 1012e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar try { 1022e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 1032e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar @Override 1042e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar public void run() { 1052e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar try { 1062e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar fun.apply(db); 1072e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar } catch (Throwable t) { 1082e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar error.set(t); 1092e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar } 1102e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar } 1112e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar }); 1122e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar } finally { 1132e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar db.close(); 1142e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar } 1152e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar return error.get(); 1162e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar } 1172e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar} 118