• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.android.quicksearchbox.util.MockTask.assertRanNever;
20 import static com.android.quicksearchbox.util.MockTask.assertRanOnce;
21 
22 import android.test.suitebuilder.annotation.SmallTest;
23 
24 import junit.framework.TestCase;
25 
26 /**
27  * Tests for {@link PerNameExecutor}.
28  */
29 @SmallTest
30 public class PerNameExecutorTest extends TestCase {
31 
32     private MockNamedTaskExecutorFactory mExecutorFactory;
33     private NamedTaskExecutor mExecutor;
34 
35     @Override
setUp()36     protected void setUp() throws Exception {
37         super.setUp();
38 
39         mExecutorFactory = new MockNamedTaskExecutorFactory();
40         mExecutor = new PerNameExecutor(mExecutorFactory);
41     }
42 
testExecute()43     public void testExecute() {
44         MockTask a1 = addTask("a", 1);
45         MockTask a2 = addTask("a", 2);
46         MockTask a3 = addTask("a", 3);
47         MockTask b1 = addTask("b", 1);
48 
49         assertRanNever("step 0", a1, a2, a3, b1);
50         step(); // run a1, b1
51         assertRanOnce("step 1", a1, b1);
52         assertRanNever("step 1", a2, a3);
53         step(); // run a2
54         assertRanOnce("step 2", a1, b1, a2);
55         assertRanNever("step 2", a3);
56         step(); // run a3
57         assertRanOnce("step 3", a1, b1, a2, a3);
58         step(); // should do nothing
59         assertRanOnce("step 4", a1, b1, a2, a3);
60     }
61 
testCancelPendingTasks()62     public void testCancelPendingTasks() {
63         MockTask a1 = addTask("a", 1);
64         MockTask a2 = addTask("a", 2);
65         MockTask b1 = addTask("b", 1);
66 
67         step(); // run a1, b1
68         assertRanOnce("step 1", a1, b1);
69         assertRanNever("step 1", a2);
70         mExecutor.cancelPendingTasks(); // cancel a2
71         assertRanOnce("step 1, after cancel", a1, b1);
72         assertRanNever("step 1, after cancel", a2);
73         step(); // do nothing
74         assertRanOnce("step 2", a1, b1);
75         assertRanNever("step 2", a2);
76 
77         MockTask a3 = addTask("a" , 3);
78         MockTask c1 = addTask("c" , 1);
79 
80         assertRanNever("step 2, new tasks", a3, c1, a2);
81         step(); // run a3, c1
82         assertRanOnce("step 3", a1, b1, a3, c1);
83         assertRanNever("step 3", a2);
84         step(); // do nothing
85         assertRanOnce("step 4", a1, b1, a3, c1);
86         assertRanNever("step 4", a2);
87     }
88 
testClose()89     public void testClose() {
90         MockTask a1 = new MockTask("a", 1);
91         mExecutor.execute(a1);
92 
93         assertRanNever("before close()", a1);
94         mExecutor.close();
95         assertRanNever("after close() 1", a1);
96         mExecutor.close();
97         assertRanNever("after close() 2", a1);
98     }
99 
addTask(String name, int id)100     private MockTask addTask(String name, int id) {
101         MockTask task = new MockTask(name, id);
102         mExecutor.execute(task);
103         return task;
104     }
105 
106     /**
107      * Run one task for each name.
108      */
step()109     private void step() {
110         mExecutorFactory.runNext();
111     }
112 
113 }
114