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 android.test.AndroidTestCase;
20 import android.test.suitebuilder.annotation.MediumTest;
21 import android.util.Log;
22 
23 @MediumTest
24 public class AsyncResultHolderTests extends AndroidTestCase {
25     static final String TAG = AsyncResultHolderTests.class.getSimpleName();
26 
27     private static final int TIMEOUT_IN_MILLISECONDS = 500;
28     private static final int MARGIN_IN_MILLISECONDS = 250;
29     private static final int DEFAULT_VALUE = 2;
30     private static final int SET_VALUE = 1;
31 
setAfterGivenTime(final AsyncResultHolder<T> holder, final T value, final long time)32     private static <T> void setAfterGivenTime(final AsyncResultHolder<T> holder, final T value,
33             final long time) {
34         new Thread(new Runnable() {
35             @Override
36             public void run() {
37                 try {
38                     Thread.sleep(time);
39                 } catch (InterruptedException e) {
40                     Log.d(TAG, "Exception while sleeping", e);
41                 }
42                 holder.set(value);
43             }
44         }).start();
45     }
46 
testGetWithoutSet()47     public void testGetWithoutSet() {
48         final AsyncResultHolder<Integer> holder = new AsyncResultHolder<>("Test");
49         final int resultValue = holder.get(DEFAULT_VALUE, TIMEOUT_IN_MILLISECONDS);
50         assertEquals(DEFAULT_VALUE, resultValue);
51     }
52 
testGetBeforeSet()53     public void testGetBeforeSet() {
54         final AsyncResultHolder<Integer> holder = new AsyncResultHolder<>("Test");
55         setAfterGivenTime(holder, SET_VALUE, TIMEOUT_IN_MILLISECONDS + MARGIN_IN_MILLISECONDS);
56         final int resultValue = holder.get(DEFAULT_VALUE, TIMEOUT_IN_MILLISECONDS);
57         assertEquals(DEFAULT_VALUE, resultValue);
58     }
59 
testGetAfterSet()60     public void testGetAfterSet() {
61         final AsyncResultHolder<Integer> holder = new AsyncResultHolder<>("Test");
62         holder.set(SET_VALUE);
63         final int resultValue = holder.get(DEFAULT_VALUE, TIMEOUT_IN_MILLISECONDS);
64         assertEquals(SET_VALUE, resultValue);
65     }
66 
testGetBeforeTimeout()67     public void testGetBeforeTimeout() {
68         final AsyncResultHolder<Integer> holder = new AsyncResultHolder<>("Test");
69         setAfterGivenTime(holder, SET_VALUE, TIMEOUT_IN_MILLISECONDS - MARGIN_IN_MILLISECONDS);
70         final int resultValue = holder.get(DEFAULT_VALUE, TIMEOUT_IN_MILLISECONDS);
71         assertEquals(SET_VALUE, resultValue);
72     }
73 }
74