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 junit.framework.Assert;
     20 
     21 
     22 /**
     23  * A simple executor that maintains a queue and executes one task synchronously every
     24  * time {@link #runNext()} is called. This gives us predictable scheduling for the tests to
     25  * avoid timeouts waiting for threads to finish.
     26  */
     27 public class MockNamedTaskExecutor implements NamedTaskExecutor {
     28 
     29     private final MockExecutor mExecutor = new MockExecutor();
     30 
     31     public void execute(NamedTask task) {
     32         mExecutor.execute(task);
     33     }
     34 
     35     public void cancelPendingTasks() {
     36         mExecutor.cancelPendingTasks();
     37     }
     38 
     39     public void close() {
     40         mExecutor.close();
     41     }
     42 
     43     public boolean runNext() {
     44         return mExecutor.runNext();
     45     }
     46 
     47     public void assertPendingTaskCount(int expected) {
     48         Assert.assertEquals("Wrong number of pending tasks",
     49                 expected, mExecutor.countPendingTasks());
     50     }
     51 
     52     public void assertDone() {
     53         assertPendingTaskCount(0);
     54     }
     55 }
     56