1 /* 2 * Copyright (C) 2013 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 com.android.inputmethod.latin.utils; 18 19 import static org.junit.Assert.assertEquals; 20 21 import android.util.Log; 22 23 import androidx.test.filters.MediumTest; 24 import androidx.test.runner.AndroidJUnit4; 25 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 @MediumTest 30 @RunWith(AndroidJUnit4.class) 31 public class AsyncResultHolderTests { 32 static final String TAG = AsyncResultHolderTests.class.getSimpleName(); 33 34 private static final int TIMEOUT_IN_MILLISECONDS = 500; 35 private static final int MARGIN_IN_MILLISECONDS = 250; 36 private static final int DEFAULT_VALUE = 2; 37 private static final int SET_VALUE = 1; 38 setAfterGivenTime(final AsyncResultHolder<T> holder, final T value, final long time)39 private static <T> void setAfterGivenTime(final AsyncResultHolder<T> holder, final T value, 40 final long time) { 41 new Thread(new Runnable() { 42 @Override 43 public void run() { 44 try { 45 Thread.sleep(time); 46 } catch (InterruptedException e) { 47 Log.d(TAG, "Exception while sleeping", e); 48 } 49 holder.set(value); 50 } 51 }).start(); 52 } 53 54 @Test testGetWithoutSet()55 public void testGetWithoutSet() { 56 final AsyncResultHolder<Integer> holder = new AsyncResultHolder<>("Test"); 57 final int resultValue = holder.get(DEFAULT_VALUE, TIMEOUT_IN_MILLISECONDS); 58 assertEquals(DEFAULT_VALUE, resultValue); 59 } 60 61 @Test testGetBeforeSet()62 public void testGetBeforeSet() { 63 final AsyncResultHolder<Integer> holder = new AsyncResultHolder<>("Test"); 64 setAfterGivenTime(holder, SET_VALUE, TIMEOUT_IN_MILLISECONDS + MARGIN_IN_MILLISECONDS); 65 final int resultValue = holder.get(DEFAULT_VALUE, TIMEOUT_IN_MILLISECONDS); 66 assertEquals(DEFAULT_VALUE, resultValue); 67 } 68 69 @Test testGetAfterSet()70 public void testGetAfterSet() { 71 final AsyncResultHolder<Integer> holder = new AsyncResultHolder<>("Test"); 72 holder.set(SET_VALUE); 73 final int resultValue = holder.get(DEFAULT_VALUE, TIMEOUT_IN_MILLISECONDS); 74 assertEquals(SET_VALUE, resultValue); 75 } 76 77 @Test testGetBeforeTimeout()78 public void testGetBeforeTimeout() { 79 final AsyncResultHolder<Integer> holder = new AsyncResultHolder<>("Test"); 80 setAfterGivenTime(holder, SET_VALUE, TIMEOUT_IN_MILLISECONDS - MARGIN_IN_MILLISECONDS); 81 final int resultValue = holder.get(DEFAULT_VALUE, TIMEOUT_IN_MILLISECONDS); 82 assertEquals(SET_VALUE, resultValue); 83 } 84 } 85