1 /* 2 * Copyright 2020 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 androidx.camera.video.internal.encoder 18 19 import android.os.SystemClock 20 import org.mockito.exceptions.base.MockitoAssertionError 21 import org.mockito.exceptions.base.MockitoException 22 import org.mockito.internal.invocation.InvocationMarker 23 import org.mockito.internal.invocation.InvocationsFinder 24 import org.mockito.internal.verification.VerificationModeFactory 25 import org.mockito.internal.verification.api.VerificationData 26 import org.mockito.verification.Timeout 27 import org.mockito.verification.VerificationMode 28 29 private class NoInvocationVerificationMode : VerificationMode { 30 31 private val noInvokeDuration: Long 32 private var invocationCount = -1 33 private var checkNoInvocationStartTime = 0L 34 35 constructor(noInvokeDuration: Long) { 36 if (noInvokeDuration < 0) { 37 throw MockitoException("Negative value is not allowed here") 38 } 39 this.noInvokeDuration = noInvokeDuration 40 } 41 verifynull42 override fun verify(data: VerificationData?) { 43 val invocations = data!!.allInvocations 44 val wanted = data.target 45 val actualInvocations = InvocationsFinder.findInvocations(invocations, wanted) 46 val actualCount = actualInvocations.size 47 val currentTime = SystemClock.uptimeMillis() 48 49 if (invocationCount == -1) { 50 invocationCount = actualCount 51 checkNoInvocationStartTime = currentTime 52 throw MockitoAssertionError("The first check for no invocation condition.") 53 } else if (invocationCount == -1 || actualCount > invocationCount) { 54 invocationCount = actualCount 55 checkNoInvocationStartTime = currentTime 56 throw MockitoAssertionError("There is new invocation.") 57 } else if (currentTime - checkNoInvocationStartTime > noInvokeDuration) { 58 InvocationMarker.markVerified(actualInvocations, wanted) 59 } else { 60 throw MockitoAssertionError("Keep monitoring invocation") 61 } 62 } 63 descriptionnull64 override fun description(description: String?): VerificationMode { 65 return VerificationModeFactory.description(this, description) 66 } 67 } 68 noInvocationnull69fun noInvocation(noInvocationDuration: Long, timeout: Long): Timeout { 70 return Timeout(timeout, NoInvocationVerificationMode(noInvocationDuration)) 71 } 72