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 18 package com.android.quicksearchbox.util; 19 20 import android.test.AndroidTestCase; 21 import android.test.suitebuilder.annotation.MediumTest; 22 23 import java.util.concurrent.ThreadFactory; 24 25 /** 26 * Tests for {@link PriorityThreadFactory}. 27 */ 28 @MediumTest 29 public class PriorityThreadFactoryTest extends AndroidTestCase { 30 31 public void testPriority() throws InterruptedException { 32 priorityTest(android.os.Process.THREAD_PRIORITY_BACKGROUND); 33 priorityTest(android.os.Process.THREAD_PRIORITY_DEFAULT); 34 priorityTest(android.os.Process.THREAD_PRIORITY_FOREGROUND); 35 } 36 37 /** 38 * Helper method for {@link #testPriority()}. 39 */ 40 private void priorityTest(int priority) throws InterruptedException { 41 ThreadFactory factory = new PriorityThreadFactory(priority); 42 CheckPriorityRunnable r = new CheckPriorityRunnable(); 43 Thread t = factory.newThread(r); 44 t.start(); 45 assertEquals(priority, r.getPriority()); 46 } 47 48 /** 49 * Helper class for {@link #priorityTest(int)}. 50 */ 51 private static class CheckPriorityRunnable implements Runnable { 52 private Integer mPriority = null; 53 public synchronized int getPriority() throws InterruptedException { 54 while (mPriority == null) { 55 wait(); 56 } 57 return mPriority.intValue(); 58 } 59 public synchronized void run() { 60 int tid = android.os.Process.myTid(); 61 mPriority = new Integer(android.os.Process.getThreadPriority(tid)); 62 notify(); 63 } 64 } 65 66 } 67