Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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.os.cts;
     18 
     19 import java.util.ArrayList;
     20 
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.os.CountDownTimer;
     24 import android.test.InstrumentationTestCase;
     25 
     26 public class CountDownTimerTest extends InstrumentationTestCase {
     27     private Context mContext;
     28     private CountDownTimerTestStub mActivity;
     29     private long mStartTime;
     30     private final long OFFSET = 200;
     31 
     32     @Override
     33     protected void setUp() throws Exception {
     34         super.setUp();
     35         mContext = getInstrumentation().getTargetContext();
     36         Intent intent = new Intent(mContext, CountDownTimerTestStub.class);
     37         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     38         mActivity = (CountDownTimerTestStub) getInstrumentation().startActivitySync(intent);
     39         mStartTime = System.currentTimeMillis();
     40         mActivity.countDownTimer.start();
     41     }
     42 
     43     public void testCountDownTimer() {
     44         int count = (int) (mActivity.MILLISINFUTURE / mActivity.INTERVAL);
     45         final long TIMEOUT_MSEC = mActivity.MILLISINFUTURE + mActivity.INTERVAL + OFFSET * count;
     46         waitForAction(TIMEOUT_MSEC);
     47         assertTrue(mActivity.onFinished);
     48         assertEqualsTickTime(mActivity.tickTimes, OFFSET);
     49     }
     50 
     51     public void testCountDownTimerCancel() {
     52         final long DELAY_MSEC = mActivity.INTERVAL + OFFSET;
     53         assertTrue(DELAY_MSEC < mActivity.MILLISINFUTURE);
     54         waitForAction(DELAY_MSEC);
     55         assertFalse(mActivity.onFinished);
     56         mActivity.countDownTimer.cancel();
     57         final long TIMEOUT_MSEC = mActivity.MILLISINFUTURE + mActivity.INTERVAL;
     58         waitForAction(TIMEOUT_MSEC);
     59         assertFalse(mActivity.onFinished);
     60         // it will call onTick after start countDownTimer, so count plus 1;
     61         int count = Long.valueOf(DELAY_MSEC / mActivity.INTERVAL).intValue() + 1;
     62         assertEquals(count, mActivity.tickTimes.size());
     63         assertEqualsTickTime(mActivity.tickTimes, OFFSET);
     64     }
     65 
     66     private void assertEqualsTickTime(ArrayList<Long> tickTimes, long offset) {
     67         for (int i = 0; i < tickTimes.size(); i++) {
     68             long tickTime = tickTimes.get(i);
     69             long expecTickTime = mStartTime + i * mActivity.INTERVAL;
     70             assertTrue(Math.abs(expecTickTime - tickTime) < offset);
     71         }
     72     }
     73 
     74     /**
     75      * Wait for an action to complete.
     76      *
     77      * @param time The time to wait.
     78      */
     79     private void waitForAction(long time) {
     80         try {
     81             Thread.sleep(time);
     82         } catch (final InterruptedException e) {
     83             fail("error occurs when wait for an action: " + e.toString());
     84         }
     85     }
     86 
     87     @Override
     88     protected void tearDown() throws Exception {
     89         super.tearDown();
     90         if (mActivity != null) {
     91             mActivity.finish();
     92         }
     93     }
     94 
     95 }
     96