Home | History | Annotate | Download | only in listener
      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 package android.longevity.core.listener;
     17 
     18 import static org.mockito.Mockito.never;
     19 import static org.mockito.Mockito.when;
     20 import static org.mockito.Mockito.verify;
     21 
     22 import android.os.SystemClock;
     23 
     24 import java.util.HashMap;
     25 import java.util.Map;
     26 
     27 import org.junit.Before;
     28 import org.junit.Test;
     29 import org.junit.runner.Description;
     30 import org.junit.runner.RunWith;
     31 import org.junit.runner.notification.RunNotifier;
     32 import org.junit.runners.JUnit4;
     33 
     34 import org.mockito.Mock;
     35 import org.mockito.MockitoAnnotations;
     36 
     37 /**
     38  * Unit tests for the platform-specific {@link TimeoutTerminator}.
     39  */
     40 @RunWith(JUnit4.class)
     41 public class TimeoutTerminatorTest {
     42     private TimeoutTerminator mListener;
     43     @Mock private RunNotifier mNotifier;
     44 
     45     @Before
     46     public void setupListener() {
     47         MockitoAnnotations.initMocks(this);
     48         Map<String, String> args = new HashMap<>();
     49         args.put(TimeoutTerminator.OPTION, String.valueOf(50L));
     50         mListener = new TimeoutTerminator(mNotifier, args);
     51     }
     52     /**
     53      * Unit test the listener's kill logic.
     54      */
     55     @Test
     56     public void testTimeoutTerminator_pass() throws Exception {
     57         mListener.testStarted(Description.EMPTY);
     58         SystemClock.sleep(10L);
     59         verify(mNotifier, never()).pleaseStop();
     60     }
     61 
     62     /**
     63      * Unit test the listener's kill logic.
     64      */
     65     @Test
     66     public void testTimeoutTerminator_timeout() throws Exception {
     67         mListener.testStarted(Description.EMPTY);
     68         SystemClock.sleep(60L);
     69         mListener.testFinished(Description.EMPTY);
     70         verify(mNotifier).pleaseStop();
     71     }
     72 }
     73