Home | History | Annotate | Download | only in quicksearchbox
      1 /*
      2  * Copyright (C) 2009 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;
     18 
     19 import com.android.quicksearchbox.util.Consumer;
     20 import com.android.quicksearchbox.util.NowOrLater;
     21 
     22 import android.test.AndroidTestCase;
     23 
     24 /**
     25  * Base class for tests for {@link IconLoader} subclasses.
     26  *
     27  */
     28 public abstract class IconLoaderTest extends AndroidTestCase {
     29 
     30     protected IconLoader mLoader;
     31 
     32     @Override
     33     protected void setUp() throws Exception {
     34         mLoader = create();
     35     }
     36 
     37     protected abstract IconLoader create() throws Exception;
     38 
     39     public void testGetIcon() {
     40         assertNull(mLoader.getIcon(null));
     41         assertNull(mLoader.getIcon(""));
     42         assertNull(mLoader.getIcon("0"));
     43         assertNotNull(mLoader.getIcon(String.valueOf(android.R.drawable.star_on)));
     44     }
     45 
     46     public void assertNull(NowOrLater<?> value) {
     47         if (value.haveNow()) {
     48             assertNull(value.getNow());
     49         } else {
     50             AssertConsumer<Object> consumer = new AssertConsumer<Object>();
     51             value.getLater(consumer);
     52             consumer.assertNull();
     53         }
     54     }
     55 
     56     public void assertNotNull(NowOrLater<?> value) {
     57         if (value.haveNow()) {
     58             assertNotNull(value.getNow());
     59         } else {
     60             AssertConsumer<Object> consumer = new AssertConsumer<Object>();
     61             value.getLater(consumer);
     62             consumer.assertNotNull();
     63         }
     64     }
     65 
     66     protected static class AssertConsumer<C> implements Consumer<C> {
     67         private boolean mConsumed = false;
     68         private Object mValue;
     69         public boolean consume(Object value) {
     70             synchronized(this) {
     71                 mConsumed = true;
     72                 mValue = value;
     73                 this.notifyAll();
     74             }
     75             return true;
     76         }
     77         public synchronized Object waitFor() {
     78             if (!mConsumed) {
     79                 try {
     80                     this.wait();
     81                 } catch (InterruptedException e) {
     82                 }
     83             }
     84             return mValue;
     85         }
     86         public void assertNull() {
     87             IconLoaderTest.assertNull(waitFor());
     88         }
     89         public void assertNotNull() {
     90             IconLoaderTest.assertNotNull(waitFor());
     91         }
     92         public AssertConsumer<C> reset() {
     93             mConsumed = false;
     94             mValue = null;
     95             return this;
     96         }
     97     }
     98 
     99 }
    100