Skip to content

Switch to lazy arguments in the components constructors #682

Description

@Nikitae57

Sometimes you have to manually instanciate a lot of components when you need to customize just one. For example, if you want to provide custom implementation of a systemLanguage parameter in the Kaspresso builder you have to instanciate several utility classes. Otherwise you get UninitializedPropertyException. Example below:

    Kaspresso.Builder.simple {
        testLogger = UiTestLoggerImpl(Kaspresso.DEFAULT_TEST_LOGGER_TAG)
        libLogger = UiTestLoggerImpl(Kaspresso.DEFAULT_LIB_LOGGER_TAG)
        adbServer = AdbServerImpl(LogLevel.WARN, libLogger)
        hackPermissions = HackPermissionsImpl(
            libLogger,
            instrumentalDependencyProviderFactory.getComponentProvider<HackPermissionsImpl>(instrumentation),
            adbServer
        )
        systemLanguage = SystemLanguage(instrumentation.context, testLogger, hackPermissions)
    }

Here both loggers, adbServer and hackPermissions get the default implementation - the same they would've got if we didn't customize systemLanguage value. It would be better if at least core components had the lazy arguments in their constructors so we could pass the properties without UninitializedPropertyException e.g.

open class SystemLanguage(
    private val context: Lazy<Context>,
    private val logger: Lazy<UiTestLogger>,
    private val hackPermissions: Lazy<HackPermissions>,
)

instead of the current

open class SystemLanguage(
    private val context: Context,
    private val logger: UiTestLogger,
    private val hackPermissions: HackPermissions
)

so the customization would look as follows:

    Kaspresso.Builder.simple {
        systemLanguage = SystemLanguage(
                { instrumentation.context }, 
                { testLogger }, 
                { hackPermissions },
        )
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions