1a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert/*
2a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert * Copyright (C) 2010 The Android Open Source Project
3a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert *
4a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License");
5a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert * you may not use this file except in compliance with the License.
6a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert * You may obtain a copy of the License at
7a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert *
8a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert *      http://www.apache.org/licenses/LICENSE-2.0
9a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert *
10a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert * Unless required by applicable law or agreed to in writing, software
11a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS,
12a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert * See the License for the specific language governing permissions and
14a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert * limitations under the License.
15a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert */
16a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert
17a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringertpackage com.android.quicksearchbox.util;
18a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert
19ced9f76b761536341d739e9a243c98a4bf90638cBjorn Bringertimport junit.framework.Assert;
20ced9f76b761536341d739e9a243c98a4bf90638cBjorn Bringert
21a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert
22a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert/**
23a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert * A simple executor that maintains a queue and executes one task synchronously every
24a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert * time {@link #runNext()} is called. This gives us predictable scheduling for the tests to
25a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert * avoid timeouts waiting for threads to finish.
26a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert */
27a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringertpublic class MockNamedTaskExecutor implements NamedTaskExecutor {
28a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert
29148154b406d3e483018e98f8f0296b59302020a3Bjorn Bringert    private final MockExecutor mExecutor = new MockExecutor();
30a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert
31a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert    public void execute(NamedTask task) {
32148154b406d3e483018e98f8f0296b59302020a3Bjorn Bringert        mExecutor.execute(task);
33a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert    }
34a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert
35a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert    public void cancelPendingTasks() {
36148154b406d3e483018e98f8f0296b59302020a3Bjorn Bringert        mExecutor.cancelPendingTasks();
37a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert    }
38a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert
39a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert    public void close() {
40148154b406d3e483018e98f8f0296b59302020a3Bjorn Bringert        mExecutor.close();
41a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert    }
42a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert
43a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert    public boolean runNext() {
44148154b406d3e483018e98f8f0296b59302020a3Bjorn Bringert        return mExecutor.runNext();
45a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert    }
46a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert
47ced9f76b761536341d739e9a243c98a4bf90638cBjorn Bringert    public void assertPendingTaskCount(int expected) {
48ced9f76b761536341d739e9a243c98a4bf90638cBjorn Bringert        Assert.assertEquals("Wrong number of pending tasks",
49ced9f76b761536341d739e9a243c98a4bf90638cBjorn Bringert                expected, mExecutor.countPendingTasks());
50ced9f76b761536341d739e9a243c98a4bf90638cBjorn Bringert    }
51ced9f76b761536341d739e9a243c98a4bf90638cBjorn Bringert
52ced9f76b761536341d739e9a243c98a4bf90638cBjorn Bringert    public void assertDone() {
53ced9f76b761536341d739e9a243c98a4bf90638cBjorn Bringert        assertPendingTaskCount(0);
54ced9f76b761536341d739e9a243c98a4bf90638cBjorn Bringert    }
55a48af083ff81555261f334a1e050eae3b02a746cBjorn Bringert}
56