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.impl.background.systemalarm;
18
19import static org.hamcrest.CoreMatchers.is;
20import static org.hamcrest.CoreMatchers.notNullValue;
21import static org.hamcrest.CoreMatchers.nullValue;
22import static org.hamcrest.MatcherAssert.assertThat;
23import static org.mockito.Mockito.mock;
24import static org.mockito.Mockito.when;
25
26import android.content.Context;
27import android.support.test.InstrumentationRegistry;
28import android.support.test.filters.SmallTest;
29import android.support.test.runner.AndroidJUnit4;
30
31import androidx.work.DatabaseTest;
32import androidx.work.OneTimeWorkRequest;
33import androidx.work.impl.WorkManagerImpl;
34import androidx.work.impl.model.SystemIdInfo;
35import androidx.work.worker.TestWorker;
36
37import org.junit.Before;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40
41import java.util.concurrent.TimeUnit;
42
43@RunWith(AndroidJUnit4.class)
44@SmallTest
45public class AlarmsTest extends DatabaseTest {
46
47    private Context mContext;
48    private WorkManagerImpl mWorkManager;
49    private long mTriggerAt;
50
51    @Before
52    public void setUp() {
53        mContext = InstrumentationRegistry.getTargetContext();
54        mWorkManager = mock(WorkManagerImpl.class);
55        // Set it to sometime in the future so as to avoid triggering real alarms.
56        mTriggerAt = System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1);
57        when(mWorkManager.getWorkDatabase()).thenReturn(mDatabase);
58    }
59
60    @Test
61    public void testSetAlarm_noPreExistingAlarms() {
62        OneTimeWorkRequest work = new OneTimeWorkRequest.Builder(TestWorker.class).build();
63        insertWork(work);
64        String workSpecId = work.getStringId();
65
66        Alarms.setAlarm(mContext, mWorkManager, workSpecId, mTriggerAt);
67        SystemIdInfo systemIdInfo = mDatabase.systemIdInfoDao().getSystemIdInfo(workSpecId);
68        assertThat(systemIdInfo, is(notNullValue()));
69    }
70
71    @Test
72    public void testSetAlarm_withPreExistingAlarms() {
73        OneTimeWorkRequest work = new OneTimeWorkRequest.Builder(TestWorker.class).build();
74        insertWork(work);
75        String workSpecId = work.getStringId();
76
77        SystemIdInfo systemIdInfo = new SystemIdInfo(workSpecId, 1);
78        mDatabase.systemIdInfoDao().insertSystemIdInfo(systemIdInfo);
79
80        Alarms.setAlarm(mContext, mWorkManager, workSpecId, mTriggerAt);
81        SystemIdInfo updatedSystemIdInfo = mDatabase.systemIdInfoDao().getSystemIdInfo(workSpecId);
82        assertThat(updatedSystemIdInfo, is(notNullValue()));
83        assertThat(updatedSystemIdInfo.systemId, is(systemIdInfo.systemId));
84    }
85
86    @Test
87    public void testCancelAlarm() {
88        OneTimeWorkRequest work = new OneTimeWorkRequest.Builder(TestWorker.class).build();
89        insertWork(work);
90        String workSpecId = work.getStringId();
91
92        SystemIdInfo systemIdInfo = new SystemIdInfo(workSpecId, 1);
93        mDatabase.systemIdInfoDao().insertSystemIdInfo(systemIdInfo);
94
95        Alarms.cancelAlarm(mContext, mWorkManager, workSpecId);
96        SystemIdInfo updatedSystemIdInfo = mDatabase.systemIdInfoDao().getSystemIdInfo(workSpecId);
97        assertThat(updatedSystemIdInfo, is(nullValue()));
98    }
99}
100