1/* 2 * Copyright (C) 2014 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 17package android.support.test.internal.runner; 18 19import static android.support.test.internal.util.Checks.checkNotNull; 20 21import android.app.Instrumentation; 22 23import java.util.concurrent.atomic.AtomicReference; 24 25/** 26 * Holds a reference to the instrumentation running in the process. 27 */ 28public final class InstrumentationRegistry { 29 30 private static final AtomicReference<Instrumentation> sInstrumentationRef = 31 new AtomicReference<Instrumentation>(null); 32 33 /** 34 * Returns the instrumentation currently running. 35 * 36 * @throws IllegalStateException if instrumentation hasn't been registered 37 */ 38 public static Instrumentation getInstance() { 39 return checkNotNull(sInstrumentationRef.get(), "No instrumentation registered. " + 40 "Must run under a registering instrumentation."); 41 } 42 43 /** 44 * Records/exposes the instrumentation currently running. 45 * <p> 46 * This is a global registry - so be aware of the impact of calling this method! 47 * </p> 48 */ 49 public static void registerInstance(Instrumentation instrumentation) { 50 sInstrumentationRef.set(instrumentation); 51 } 52 53 private InstrumentationRegistry() { } 54} 55