Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2010 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.quicksearchbox.util;
     18 
     19 import com.android.quicksearchbox.ConsumerTrap;
     20 
     21 import android.test.suitebuilder.annotation.SmallTest;
     22 
     23 import junit.framework.TestCase;
     24 
     25 /**
     26  * Tests for {@link CachedLater}.
     27  */
     28 @SmallTest
     29 public class CachedLaterTest extends TestCase {
     30 
     31     private Harness mHarness;
     32     private ConsumerTrap<String> mTrap;
     33     private ConsumerTrap<String> mTrap2;
     34 
     35     @Override
     36     protected void setUp() throws Exception {
     37         super.setUp();
     38         mHarness = new Harness();
     39         mTrap = new ConsumerTrap<String>();
     40         mTrap2 = new ConsumerTrap<String>();
     41     }
     42 
     43     public void testGetLaterAsync() {
     44         mHarness.getLater(mTrap);
     45         // The consumer should not have been called here
     46         assertNull(mTrap.getValue());
     47         mHarness.finishCreate("foo");
     48         assertEquals("foo", mTrap.getValue());
     49     }
     50 
     51     public void testGetLaterSync() {
     52         mHarness.getLater(mTrap);  // trigger create()
     53         mHarness.finishCreate("foo");
     54         mHarness.getLater(mTrap2);  // sync call
     55         assertEquals("foo", mTrap2.getValue());
     56     }
     57 
     58     public void testGetLaterWhileCreating() {
     59         mHarness.getLater(mTrap);  // trigger create()
     60         mHarness.getLater(mTrap2);  // register a second consumer
     61         mHarness.finishCreate("foo");
     62         assertEquals("foo", mTrap.getValue());
     63         assertEquals("foo", mTrap2.getValue());
     64     }
     65 
     66     public void testGetLaterAfterClear() {
     67         mHarness.getLater(mTrap);  // trigger create()
     68         mHarness.finishCreate("foo");
     69         mHarness.clear();
     70         mHarness.getLater(mTrap2);
     71         mHarness.finishCreate("bar");
     72         assertEquals("bar", mTrap2.getValue());
     73     }
     74 
     75     public void testHaveNow() {
     76         assertFalse(mHarness.haveNow());
     77         mHarness.getLater(mTrap);
     78         assertFalse(mHarness.haveNow());
     79         mHarness.finishCreate("foo");
     80         assertTrue(mHarness.haveNow());
     81     }
     82 
     83     public void testClear() {
     84         mHarness.getLater(mTrap);  // trigger create()
     85         mHarness.finishCreate("foo");
     86         mHarness.clear();
     87         assertFalse(mHarness.haveNow());
     88         mHarness.getLater(mTrap2);
     89         assertNull(mTrap2.getValue());
     90     }
     91 
     92     private static class Harness extends CachedLater<String> {
     93 
     94         private boolean mCreatePending = false;
     95 
     96         protected void create() {
     97             mCreatePending = true;
     98         }
     99 
    100         public void finishCreate(String str) {
    101             if (mCreatePending) {
    102                 store(str);
    103                 mCreatePending = false;
    104             }
    105         }
    106 
    107     }
    108 
    109 }
    110