af4cab20bdcb681215c30b0b3a7f237a670aa0ec
[idea/community.git] / platform / testFramework / src / com / intellij / testFramework / ServiceContainerUtil.kt
1 // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 @file:JvmName("ServiceContainerUtil")
3 package com.intellij.testFramework
4
5 import com.intellij.openapi.Disposable
6 import com.intellij.openapi.components.ComponentManager
7 import com.intellij.openapi.extensions.BaseExtensionPointName
8 import com.intellij.openapi.extensions.DefaultPluginDescriptor
9 import com.intellij.serviceContainer.PlatformComponentManagerImpl
10 import org.jetbrains.annotations.TestOnly
11
12 @TestOnly
13 fun <T : Any> ComponentManager.registerServiceInstance(serviceInterface: Class<T>, instance: T) {
14   (this as PlatformComponentManagerImpl).registerServiceInstance(serviceInterface, instance, DefaultPluginDescriptor("test"))
15 }
16
17 @TestOnly
18 fun <T : Any> ComponentManager.replaceService(serviceInterface: Class<T>, instance: T, parentDisposable: Disposable) {
19   (this as PlatformComponentManagerImpl).replaceServiceInstance(serviceInterface, instance, parentDisposable)
20 }
21
22 /**
23  * Returns old instance.
24  */
25 @TestOnly
26 fun <T : Any> ComponentManager.registerComponentInstance(componentInterface: Class<T>, instance: T, parentDisposable: Disposable?): T? {
27   return (this as PlatformComponentManagerImpl).registerComponentInstance(componentInterface, instance, parentDisposable)
28 }
29
30 @Suppress("DeprecatedCallableAddReplaceWith")
31 @TestOnly
32 @Deprecated("Pass parentDisposable")
33 fun <T : Any> ComponentManager.registerComponentInstance(componentInterface: Class<T>, instance: T): T? {
34   return (this as PlatformComponentManagerImpl).registerComponentInstance(componentInterface, instance, null)
35 }
36
37 @TestOnly
38 @JvmOverloads
39 fun ComponentManager.registerComponentImplementation(componentInterface: Class<*>, componentImplementation: Class<*>, shouldBeRegistered: Boolean = false) {
40   (this as PlatformComponentManagerImpl).registerComponentImplementation(componentInterface, componentImplementation, shouldBeRegistered)
41 }
42
43 @TestOnly
44 fun <T : Any> ComponentManager.registerExtension(name: BaseExtensionPointName, instance: T, parentDisposable: Disposable) {
45   extensionArea.getExtensionPoint<T>(name.name).registerExtension(instance, parentDisposable)
46 }
47
48 @TestOnly
49 fun ComponentManager.getServiceImplementationClassNames(prefix: String): List<String> {
50   return (this as PlatformComponentManagerImpl).getServiceImplementationClassNames(prefix)
51 }