13e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert/*
23e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Copyright (C) 2009 The Android Open Source Project
33e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *
43e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License");
53e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * you may not use this file except in compliance with the License.
63e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * You may obtain a copy of the License at
73e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *
83e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *      http://www.apache.org/licenses/LICENSE-2.0
93e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *
103e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Unless required by applicable law or agreed to in writing, software
113e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS,
123e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * See the License for the specific language governing permissions and
143e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * limitations under the License.
153e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert */
163e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
173e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringertpackage com.android.quicksearchbox;
183e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
19e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwoodimport com.android.quicksearchbox.util.Consumer;
20e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwoodimport com.android.quicksearchbox.util.NowOrLater;
21e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood
223e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringertimport android.test.AndroidTestCase;
233e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
243e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert/**
253e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Base class for tests for {@link IconLoader} subclasses.
263e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *
273e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert */
283e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringertpublic abstract class IconLoaderTest extends AndroidTestCase {
293e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
303e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    protected IconLoader mLoader;
313e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
323e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    @Override
333e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    protected void setUp() throws Exception {
343e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert        mLoader = create();
353e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    }
363e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
37e1fc4581ebab65220a82bcdb3272b4eb85453837Bjorn Bringert    protected abstract IconLoader create() throws Exception;
383e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
393e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    public void testGetIcon() {
403e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert        assertNull(mLoader.getIcon(null));
413e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert        assertNull(mLoader.getIcon(""));
423e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert        assertNull(mLoader.getIcon("0"));
433e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert        assertNotNull(mLoader.getIcon(String.valueOf(android.R.drawable.star_on)));
443e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    }
453e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
46e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    public void assertNull(NowOrLater<?> value) {
47e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        if (value.haveNow()) {
48e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            assertNull(value.getNow());
49e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        } else {
50e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            AssertConsumer<Object> consumer = new AssertConsumer<Object>();
51e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            value.getLater(consumer);
52e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            consumer.assertNull();
53e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        }
54e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    }
55e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood
56e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    public void assertNotNull(NowOrLater<?> value) {
57e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        if (value.haveNow()) {
58e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            assertNotNull(value.getNow());
59e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        } else {
60e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            AssertConsumer<Object> consumer = new AssertConsumer<Object>();
61e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            value.getLater(consumer);
62e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            consumer.assertNotNull();
63e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        }
64e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    }
65e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood
66e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    protected static class AssertConsumer<C> implements Consumer<C> {
67e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        private boolean mConsumed = false;
68e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        private Object mValue;
69e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        public boolean consume(Object value) {
70e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            synchronized(this) {
71e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                mConsumed = true;
72e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                mValue = value;
73e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                this.notifyAll();
74e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            }
75e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            return true;
76e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        }
77e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        public synchronized Object waitFor() {
78e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            if (!mConsumed) {
79e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                try {
80e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                    this.wait();
81e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                } catch (InterruptedException e) {
82e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                }
83e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            }
84e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            return mValue;
85e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        }
86e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        public void assertNull() {
87e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            IconLoaderTest.assertNull(waitFor());
88e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        }
89e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        public void assertNotNull() {
90e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            IconLoaderTest.assertNotNull(waitFor());
91e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        }
92e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        public AssertConsumer<C> reset() {
93e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            mConsumed = false;
94e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            mValue = null;
95e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            return this;
96e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        }
97e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    }
98e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood
993e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert}
100