1/*
2 * Copyright (C) 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.room.integration.testapp.test;
18
19import static org.hamcrest.CoreMatchers.is;
20import static org.hamcrest.MatcherAssert.assertThat;
21import static org.hamcrest.Matchers.empty;
22import static org.hamcrest.core.IsCollectionContaining.hasItem;
23import static org.junit.Assert.assertFalse;
24import static org.junit.Assert.assertTrue;
25
26import android.content.Context;
27import android.database.Cursor;
28import android.support.test.InstrumentationRegistry;
29import android.support.test.filters.MediumTest;
30import android.support.test.filters.SmallTest;
31import android.support.test.runner.AndroidJUnit4;
32
33import androidx.annotation.NonNull;
34import androidx.room.Room;
35import androidx.room.RoomDatabase;
36import androidx.room.integration.testapp.TestDatabase;
37import androidx.room.integration.testapp.vo.User;
38import androidx.sqlite.db.SupportSQLiteDatabase;
39
40import org.junit.Test;
41import org.junit.runner.RunWith;
42
43import java.util.ArrayList;
44import java.util.List;
45
46@RunWith(AndroidJUnit4.class)
47public class DatabaseCallbackTest {
48
49    @Test
50    @MediumTest
51    public void createAndOpen() {
52        Context context = InstrumentationRegistry.getTargetContext();
53        TestDatabaseCallback callback1 = new TestDatabaseCallback();
54        TestDatabase db1 = null;
55        TestDatabase db2 = null;
56        try {
57            db1 = Room.databaseBuilder(context, TestDatabase.class, "test")
58                    .addCallback(callback1)
59                    .build();
60            assertFalse(callback1.mCreated);
61            assertFalse(callback1.mOpened);
62            User user1 = TestUtil.createUser(3);
63            user1.setName("george");
64            db1.getUserDao().insert(user1);
65            assertTrue(callback1.mCreated);
66            assertTrue(callback1.mOpened);
67            TestDatabaseCallback callback2 = new TestDatabaseCallback();
68            db2 = Room.databaseBuilder(context, TestDatabase.class, "test")
69                    .addCallback(callback2)
70                    .build();
71            assertFalse(callback2.mCreated);
72            assertFalse(callback2.mOpened);
73            User user2 = db2.getUserDao().load(3);
74            assertThat(user2.getName(), is("george"));
75            assertFalse(callback2.mCreated); // Not called; already created by db1
76            assertTrue(callback2.mOpened);
77        } finally {
78            if (db1 != null) {
79                db1.close();
80            }
81            if (db2 != null) {
82                db2.close();
83            }
84            assertTrue(context.deleteDatabase("test"));
85        }
86    }
87
88    @Test
89    @SmallTest
90    public void writeOnCreate() {
91        Context context = InstrumentationRegistry.getTargetContext();
92        TestDatabase db = Room.inMemoryDatabaseBuilder(context, TestDatabase.class)
93                .addCallback(new RoomDatabase.Callback() {
94                    @Override
95                    public void onCreate(@NonNull SupportSQLiteDatabase db) {
96                        Cursor cursor = null;
97                        try {
98                            cursor = db.query(
99                                    "SELECT name FROM sqlite_master WHERE type = 'table'");
100                            ArrayList<String> names = new ArrayList<>();
101                            while (cursor.moveToNext()) {
102                                names.add(cursor.getString(0));
103                            }
104                            assertThat(names, hasItem("User"));
105                        } finally {
106                            if (cursor != null) {
107                                cursor.close();
108                            }
109                        }
110                    }
111                })
112                .build();
113        List<Integer> ids = db.getUserDao().loadIds();
114        assertThat(ids, is(empty()));
115    }
116
117    public static class TestDatabaseCallback extends RoomDatabase.Callback {
118
119        boolean mCreated;
120        boolean mOpened;
121
122        @Override
123        public void onCreate(@NonNull SupportSQLiteDatabase db) {
124            mCreated = true;
125        }
126
127        @Override
128        public void onOpen(@NonNull SupportSQLiteDatabase db) {
129            mOpened = true;
130        }
131    }
132}
133