WorkTest.java revision b5728f4e1a4b3f4f1fabf033b1363ca6b1cffdef
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 org.hamcrest.CoreMatchers.is;
20import static org.hamcrest.MatcherAssert.assertThat;
21
22import android.support.test.filters.SdkSuppress;
23import android.support.test.filters.SmallTest;
24
25import androidx.work.worker.TestWorker;
26
27import org.junit.Before;
28import org.junit.Rule;
29import org.junit.Test;
30import org.junit.rules.ExpectedException;
31
32import java.util.concurrent.TimeUnit;
33
34@SmallTest
35public class WorkTest extends WorkManagerTest {
36    @Rule
37    public ExpectedException mThrown = ExpectedException.none();
38
39    private Work.Builder mBuilder;
40
41    @Before
42    public void setUp() {
43        mBuilder = new Work.Builder(TestWorker.class);
44    }
45
46    @Test
47    public void testBuild_withInitialDelay() {
48        final long expectedInitialDelay = 123L;
49        Work work = mBuilder.withInitialDelay(expectedInitialDelay, TimeUnit.MILLISECONDS).build();
50        assertThat(getWorkSpec(work).initialDelay, is(expectedInitialDelay));
51    }
52
53    @Test
54    public void testBuild_setBackoffCriteria_exceedMaxBackoffDuration() {
55        final long backoffDuration = BaseWork.MAX_BACKOFF_MILLIS + 123L;
56        Work work = mBuilder
57                .withBackoffCriteria(
58                        BackoffPolicy.EXPONENTIAL,
59                        backoffDuration,
60                        TimeUnit.MILLISECONDS)
61                .build();
62        assertThat(getWorkSpec(work).backoffDelayDuration, is(BaseWork.MAX_BACKOFF_MILLIS));
63    }
64
65    @Test
66    public void testBuild_setBackoffCriteria_lessThanMinBackoffDuration() {
67        final long backoffDuration = BaseWork.MIN_BACKOFF_MILLIS - 123L;
68        Work work = mBuilder
69                .withBackoffCriteria(
70                        BackoffPolicy.EXPONENTIAL,
71                        backoffDuration,
72                        TimeUnit.MILLISECONDS)
73                .build();
74        assertThat(getWorkSpec(work).backoffDelayDuration, is(BaseWork.MIN_BACKOFF_MILLIS));
75    }
76
77    @Test
78    @SdkSuppress(minSdkVersion = 23)
79    public void testBuild_backoffAndIdleMode_throwsIllegalArgumentException() {
80        mThrown.expect(IllegalArgumentException.class);
81        Constraints constraints = new Constraints.Builder()
82                .setRequiresDeviceIdle(true)
83                .build();
84        mBuilder.withBackoffCriteria(BackoffPolicy.EXPONENTIAL, 123L, TimeUnit.MILLISECONDS)
85                .withConstraints(constraints)
86                .build();
87    }
88}
89