1 /** 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package com.android.healthconnect.controller.tests 15 16 import android.app.Activity 17 import android.app.Application 18 import android.content.Context 19 import android.os.Bundle 20 import android.view.WindowManager 21 import androidx.test.runner.AndroidJUnitRunner 22 import dagger.hilt.android.testing.HiltTestApplication 23 24 class HiltTestRunner : AndroidJUnitRunner() { newApplicationnull25 override fun newApplication( 26 cl: ClassLoader?, 27 className: String?, 28 context: Context? 29 ): Application { 30 val app = super.newApplication(cl, HiltTestApplication::class.java.name, context) 31 app.registerActivityLifecycleCallbacks( 32 object : Application.ActivityLifecycleCallbacks { 33 override fun onActivityCreated(activity: Activity, bundle: Bundle?) { 34 // Show activity on top of keyguard 35 activity.window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD) 36 // Turn on screen to prevent activity being paused by system. See b/31262906 37 activity.window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) 38 activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) 39 } 40 41 override fun onActivityDestroyed(activity: Activity) {} 42 43 override fun onActivityPaused(activity: Activity) {} 44 45 override fun onActivityResumed(activity: Activity) {} 46 47 override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {} 48 49 override fun onActivityStarted(activity: Activity) {} 50 51 override fun onActivityStopped(activity: Activity) {} 52 }) 53 return app 54 } 55 } 56