1 /* 2 * Copyright 2024 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.photopicker.tests 18 19 import android.app.Activity 20 import android.app.Application 21 import android.content.Context 22 import android.os.Bundle 23 import androidx.test.runner.AndroidJUnitRunner 24 import dagger.hilt.android.testing.HiltTestApplication 25 26 /** A wrapper class that Hilt can use for setting up dependency injection in a test context. */ 27 class HiltTestRunner : AndroidJUnitRunner() { newApplicationnull28 override fun newApplication( 29 cl: ClassLoader?, 30 className: String?, 31 context: Context? 32 ): Application { 33 val app = super.newApplication(cl, HiltTestApplication::class.java.name, context) 34 35 app.registerActivityLifecycleCallbacks( 36 object : Application.ActivityLifecycleCallbacks { 37 override fun onActivityCreated(activity: Activity, bundle: Bundle?) {} 38 39 override fun onActivityDestroyed(activity: Activity) {} 40 41 override fun onActivityPaused(activity: Activity) {} 42 43 override fun onActivityResumed(activity: Activity) {} 44 45 override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {} 46 47 override fun onActivityStarted(activity: Activity) {} 48 49 override fun onActivityStopped(activity: Activity) {} 50 } 51 ) 52 return app 53 } 54 } 55