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
172e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarpackage android.arch.persistence.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.arch.core.util.Function;
252e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport android.arch.persistence.room.Room;
262e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport android.arch.persistence.room.RoomDatabase;
272e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport android.arch.persistence.room.integration.testapp.TestDatabase;
282e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport android.content.Context;
292e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport android.support.test.InstrumentationRegistry;
302e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport android.support.test.filters.SmallTest;
312e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport android.support.test.runner.AndroidJUnit4;
322e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar
332e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport org.junit.Test;
342e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport org.junit.runner.RunWith;
352e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar
362e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarimport java.util.concurrent.atomic.AtomicReference;
372e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar
382e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar@RunWith(AndroidJUnit4.class)
392e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar@SmallTest
402e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyarpublic class MainThreadCheckTest {
412e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar
422e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar    @Test
432e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar    public void testMainThread() {
442e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        final Throwable error = test(false, new Function<TestDatabase, Void>() {
452e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            @Override
462e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            public Void apply(TestDatabase db) {
472e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                db.getUserDao().load(3);
482e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                return null;
492e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            }
502e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        });
512e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        assertThat(error, notNullValue());
522e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        assertThat(error, instanceOf(IllegalStateException.class));
532e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar    }
542e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar
552e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar    @Test
562e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar    public void testFlowableOnMainThread() {
572e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        final Throwable error = test(false, new Function<TestDatabase, Void>() {
582e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            @Override
592e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            public Void apply(TestDatabase db) {
602e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                db.getUserDao().flowableUserById(3);
612e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                return null;
622e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            }
632e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        });
642e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        assertThat(error, nullValue());
652e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar    }
662e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar
672e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar    @Test
682e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar    public void testLiveDataOnMainThread() {
692e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        final Throwable error = test(false, new Function<TestDatabase, Void>() {
702e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            @Override
712e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            public Void apply(TestDatabase db) {
722e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                db.getUserDao().liveUserById(3);
732e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                return null;
742e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            }
752e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        });
762e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        assertThat(error, nullValue());
772e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar    }
782e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar
792e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar    @Test
802e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar    public void testAllowMainThread() {
812e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        final Throwable error = test(true, new Function<TestDatabase, Void>() {
822e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            @Override
832e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            public Void apply(TestDatabase db) {
842e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                db.getUserDao().load(3);
852e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                return null;
862e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            }
872e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        });
882e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        assertThat(error, nullValue());
892e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar    }
902e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar
912e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar    private Throwable test(boolean allowMainThread, final Function<TestDatabase, Void> fun) {
922e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        Context context = InstrumentationRegistry.getTargetContext();
932e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        final RoomDatabase.Builder<TestDatabase> builder = Room.inMemoryDatabaseBuilder(
942e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                context, TestDatabase.class);
952e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        if (allowMainThread) {
962e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            builder.allowMainThreadQueries();
972e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        }
982e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        final TestDatabase db = builder.build();
992e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        final AtomicReference<Throwable> error = new AtomicReference<>();
1002e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        try {
1012e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
1022e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                @Override
1032e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                public void run() {
1042e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                    try {
1052e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                        fun.apply(db);
1062e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                    } catch (Throwable t) {
1072e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                        error.set(t);
1082e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                    }
1092e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar                }
1102e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            });
1112e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        } finally {
1122e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar            db.close();
1132e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        }
1142e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar        return error.get();
1152e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar    }
1162e8d5608ba4b3da0a05d6b6cc23d81fe10371970Yigit Boyar}
117