Home | History | Annotate | Download | only in cts
      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 
     17 package android.autofillservice.cts;
     18 
     19 import static android.autofillservice.cts.Helper.assertFloat;
     20 
     21 import static com.google.common.truth.Truth.assertThat;
     22 
     23 import static org.mockito.Mockito.when;
     24 import static org.testng.Assert.assertThrows;
     25 import static org.testng.Assert.expectThrows;
     26 
     27 import org.junit.Test;
     28 import org.junit.runner.RunWith;
     29 import org.mockito.Mock;
     30 import org.mockito.junit.MockitoJUnitRunner;
     31 
     32 import java.util.concurrent.Callable;
     33 
     34 @RunWith(MockitoJUnitRunner.class)
     35 public class TimeoutTest {
     36 
     37     private static final String NAME = "TIME, Y U NO OUT?";
     38     private static final String DESC = "something";
     39 
     40     @Mock
     41     private Callable<Object> mJob;
     42 
     43     @Test
     44     public void testInvalidConstructor() {
     45         // Invalid name
     46         assertThrows(IllegalArgumentException.class, ()-> new Timeout(null, 1, 2, 2));
     47         assertThrows(IllegalArgumentException.class, ()-> new Timeout("", 1, 2, 2));
     48         // Invalid initial value
     49         assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, -1, 2, 2));
     50         assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, 0, 2, 2));
     51         // Invalid multiplier
     52         assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, 1, -1, 2));
     53         assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, 1, 0, 2));
     54         assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, 1, 1, 2));
     55         // Invalid max value
     56         assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, 1, 2, -1));
     57         assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, 1, 2, 0));
     58         // Max value cannot be less than initial
     59         assertThrows(IllegalArgumentException.class, ()-> new Timeout(NAME, 2, 2, 1));
     60     }
     61 
     62     @Test
     63     public void testGetters() {
     64         final Timeout timeout = new Timeout(NAME, 1, 2, 5);
     65         assertThat(timeout.ms()).isEqualTo(1);
     66         assertFloat(timeout.getMultiplier(), 2);
     67         assertThat(timeout.getMaxValue()).isEqualTo(5);
     68         assertThat(timeout.getName()).isEqualTo(NAME);
     69     }
     70 
     71     @Test
     72     public void testIncrease() {
     73         final Timeout timeout = new Timeout(NAME, 1, 2, 5);
     74         // Pre-maximum
     75         assertThat(timeout.increase()).isEqualTo(1);
     76         assertThat(timeout.ms()).isEqualTo(2);
     77         assertThat(timeout.increase()).isEqualTo(2);
     78         assertThat(timeout.ms()).isEqualTo(4);
     79         // Post-maximum
     80         assertThat(timeout.increase()).isEqualTo(4);
     81         assertThat(timeout.ms()).isEqualTo(5);
     82         assertThat(timeout.increase()).isEqualTo(5);
     83         assertThat(timeout.ms()).isEqualTo(5);
     84     }
     85 
     86     @Test
     87     public void testRun_invalidArgs() {
     88         final Timeout timeout = new Timeout(NAME, 1, 2, 5);
     89         // Invalid description
     90         assertThrows(IllegalArgumentException.class, ()-> timeout.run(null, mJob));
     91         assertThrows(IllegalArgumentException.class, ()-> timeout.run("", mJob));
     92         // Invalid max attempts
     93         assertThrows(IllegalArgumentException.class, ()-> timeout.run(DESC, -1, mJob));
     94         assertThrows(IllegalArgumentException.class, ()-> timeout.run(DESC, 0, mJob));
     95         // Invalid job
     96         assertThrows(IllegalArgumentException.class, ()-> timeout.run(DESC, null));
     97     }
     98 
     99     @Test
    100     public void testRun_successOnFirstAttempt() throws Exception {
    101         final Timeout timeout = new Timeout(NAME, 100, 2, 500);
    102         final Object result = new Object();
    103         when(mJob.call()).thenReturn(result);
    104         assertThat(timeout.run(DESC, 1, mJob)).isSameAs(result);
    105     }
    106 
    107     @Test
    108     public void testRun_successOnSecondAttempt() throws Exception {
    109         final Timeout timeout = new Timeout(NAME, 100, 2, 500);
    110         final Object result = new Object();
    111         when(mJob.call()).thenReturn((Object) null, result);
    112         assertThat(timeout.run(DESC, 10, mJob)).isSameAs(result);
    113     }
    114 
    115     @Test
    116     public void testRun_allAttemptsFailed() throws Exception {
    117         final Timeout timeout = new Timeout(NAME, 100, 2, 500);
    118         final RetryableException e = expectThrows(RetryableException.class,
    119                 () -> timeout.run(DESC, 10, mJob));
    120         assertThat(e.getMessage()).contains(DESC);
    121         assertThat(e.getTimeout()).isSameAs(timeout);
    122     }
    123 }
    124