1 /* 2 * Copyright (C) 2017 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.gtestrunner; 18 19 import org.junit.runner.Description; 20 import org.junit.runner.Runner; 21 import org.junit.runner.notification.RunNotifier; 22 23 public class GtestRunner extends Runner { 24 private static boolean sOnceFlag = false; 25 26 private Class mTargetClass; 27 private Description mDescription; 28 GtestRunner(Class testClass)29 public GtestRunner(Class testClass) { 30 synchronized (GtestRunner.class) { 31 if (sOnceFlag) { 32 throw new IllegalStateException("Error multiple GtestRunners defined"); 33 } 34 sOnceFlag = true; 35 } 36 37 mTargetClass = testClass; 38 TargetLibrary library = (TargetLibrary) testClass.getAnnotation(TargetLibrary.class); 39 if (library == null) { 40 throw new IllegalStateException("Missing required @TargetLibrary annotation"); 41 } 42 System.loadLibrary(library.value()); 43 mDescription = Description.createSuiteDescription(testClass); 44 nInitialize(mDescription); 45 } 46 47 @Override getDescription()48 public Description getDescription() { 49 return mDescription; 50 } 51 52 @Override run(RunNotifier notifier)53 public void run(RunNotifier notifier) { 54 nRun(notifier); 55 } 56 nInitialize(Description description)57 private static native void nInitialize(Description description); nRun(RunNotifier notifier)58 private static native void nRun(RunNotifier notifier); 59 } 60