Skip to content

Commit

Permalink
chore: standard activity lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfzc committed Mar 20, 2021
1 parent 6dc7434 commit 8c6de29
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2021 williamfzc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.williamfzc.randunit.exceptions

class RUInstanceException(reason: String) : RUException(reason)
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package com.williamfzc.randunit.operations

import android.app.Activity
import com.williamfzc.randunit.exceptions.RUInstanceException
import org.robolectric.Robolectric
import org.robolectric.android.controller.ActivityController

class ActivityOperation(t: Class<*>) : AbstractAndroidOperation(t) {
private var activityController: ActivityController<Activity>? = null

override fun getInstance(): Activity {
@Suppress("UNCHECKED_CAST")
return Robolectric.buildActivity(type as Class<Activity>).create().get()
activityController = Robolectric.buildActivity(type as Class<Activity>)

// robolectric issue #2068
activityController?.run {
return this.create().start().resume().visible().get()
}
throw RUInstanceException("create activity failed")
}

override fun tearDown(caller: Any) {
(caller as Activity).finish()
activityController?.run {
(caller as ActivityController<*>).pause().stop().destroy()
cleanInstanceCache()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class ServiceOperation(t: Class<*>) : AbstractAndroidOperation(t) {
}

override fun tearDown(caller: Any) {
(caller as Service).stopSelf()
val c = (caller as Service)
c.onDestroy()
c.stopSelf()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object DefaultOperationType

abstract class AbstractOperation(val type: Class<*> = DefaultOperationType::class.java) {
// inst cache
var cacheInst: Any? = null
private var cacheInst: Any? = null
fun getInstanceWithCache(): Any {
cacheInst?.let { return it }
// create
Expand Down

0 comments on commit 8c6de29

Please sign in to comment.