WorkSpecDaoTest.java revision df55ec7f797caa24839ed110d7a53135b7d17b0e
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
17package androidx.work;
18
19import static android.support.test.espresso.matcher.ViewMatchers.assertThat;
20
21import static org.hamcrest.CoreMatchers.equalTo;
22
23import static androidx.work.State.SUCCEEDED;
24
25import android.support.test.filters.SmallTest;
26import android.support.test.runner.AndroidJUnit4;
27
28import org.junit.Test;
29import org.junit.runner.RunWith;
30
31import java.util.List;
32import java.util.concurrent.TimeUnit;
33
34import androidx.work.impl.model.WorkSpec;
35import androidx.work.impl.model.WorkSpecDao;
36import androidx.work.worker.TestWorker;
37
38@RunWith(AndroidJUnit4.class)
39public class WorkSpecDaoTest extends DatabaseTest {
40
41    @Test
42    @SmallTest
43    public void testSystemAlarmEligibleWorkSpecs() {
44        long startTime = System.currentTimeMillis();
45        Work work = new Work.Builder(TestWorker.class)
46                .withPeriodStartTime(startTime + TimeUnit.HOURS.toMillis(1))
47                .build();
48        Work succeeded = new Work.Builder(TestWorker.class)
49                .withPeriodStartTime(startTime)
50                .withInitialState(SUCCEEDED)
51                .build();
52        Work enqueued = new Work.Builder(TestWorker.class)
53                .withPeriodStartTime(startTime)
54                .build();
55
56        insertWork(work);
57        insertWork(succeeded);
58        insertWork(enqueued);
59
60        WorkSpecDao workSpecDao = mDatabase.workSpecDao();
61        List<WorkSpec> eligibleWorkSpecs = workSpecDao.getSystemAlarmEligibleWorkSpecs(startTime);
62        assertThat(eligibleWorkSpecs.size(), equalTo(1));
63        assertThat(eligibleWorkSpecs.get(0).getId(), equalTo(enqueued.getId()));
64    }
65}
66