Home | History | Annotate | Download | only in widget
      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 androidx.wear.widget;
     18 
     19 import static org.junit.Assert.assertFalse;
     20 import static org.junit.Assert.assertNotNull;
     21 import static org.junit.Assert.assertTrue;
     22 
     23 import android.content.Intent;
     24 import android.support.test.filters.MediumTest;
     25 import android.support.test.rule.ActivityTestRule;
     26 import android.support.test.runner.AndroidJUnit4;
     27 
     28 import androidx.wear.test.R;
     29 
     30 import org.junit.Before;
     31 import org.junit.Rule;
     32 import org.junit.Test;
     33 import org.junit.runner.RunWith;
     34 
     35 import java.util.concurrent.TimeUnit;
     36 
     37 @MediumTest
     38 @RunWith(AndroidJUnit4.class)
     39 public class CircularProgressLayoutTest {
     40 
     41     private static final long TOTAL_TIME = TimeUnit.SECONDS.toMillis(1);
     42 
     43     @Rule
     44     public final ActivityTestRule<LayoutTestActivity> mActivityRule = new ActivityTestRule<>(
     45             LayoutTestActivity.class, true, false);
     46     private CircularProgressLayout mLayoutUnderTest;
     47 
     48     @Before
     49     public void setUp() {
     50         mActivityRule.launchActivity(new Intent().putExtra(LayoutTestActivity
     51                 .EXTRA_LAYOUT_RESOURCE_ID, R.layout.circular_progress_layout));
     52         mLayoutUnderTest = mActivityRule.getActivity().findViewById(R.id.circular_progress_layout);
     53         mLayoutUnderTest.setOnTimerFinishedListener(new FakeListener());
     54     }
     55 
     56     @Test
     57     public void testListenerIsNotified() {
     58         mLayoutUnderTest.setTotalTime(TOTAL_TIME);
     59         startTimerOnUiThread();
     60         waitForTimer(TOTAL_TIME + 100);
     61         assertNotNull(mLayoutUnderTest.getOnTimerFinishedListener());
     62         assertTrue(((FakeListener) mLayoutUnderTest.getOnTimerFinishedListener()).mFinished);
     63     }
     64 
     65     @Test
     66     public void testListenerIsNotNotifiedWhenStopped() {
     67         mLayoutUnderTest.setTotalTime(TOTAL_TIME);
     68         startTimerOnUiThread();
     69         stopTimerOnUiThread();
     70         waitForTimer(TOTAL_TIME + 100);
     71         assertNotNull(mLayoutUnderTest.getOnTimerFinishedListener());
     72         assertFalse(((FakeListener) mLayoutUnderTest.getOnTimerFinishedListener()).mFinished);
     73     }
     74 
     75     private class FakeListener implements CircularProgressLayout.OnTimerFinishedListener {
     76 
     77         boolean mFinished;
     78 
     79         @Override
     80         public void onTimerFinished(CircularProgressLayout layout) {
     81             mFinished = true;
     82         }
     83     }
     84 
     85     private void startTimerOnUiThread() {
     86         mActivityRule.getActivity().runOnUiThread(new Runnable() {
     87             @Override
     88             public void run() {
     89                 mLayoutUnderTest.startTimer();
     90             }
     91         });
     92     }
     93 
     94     private void stopTimerOnUiThread() {
     95         mActivityRule.getActivity().runOnUiThread(new Runnable() {
     96             @Override
     97             public void run() {
     98                 mLayoutUnderTest.stopTimer();
     99             }
    100         });
    101     }
    102 
    103     private void waitForTimer(long time) {
    104         try {
    105             Thread.sleep(time);
    106         } catch (InterruptedException e) {
    107             e.printStackTrace();
    108         }
    109     }
    110 }
    111