This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
713 additions
and
545 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package ch.sdp.vibester | ||
|
||
/* | ||
* Copyright (C) 2019 The Android Open Source Project | ||
* | ||
* 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. | ||
*/ | ||
|
||
import android.content.ComponentName | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.annotation.StyleRes | ||
import androidx.core.util.Preconditions | ||
import androidx.fragment.app.Fragment | ||
import androidx.test.core.app.ActivityScenario | ||
import androidx.test.core.app.ApplicationProvider | ||
|
||
/** | ||
* launchFragmentInContainer from the androidx.fragment:fragment-testing library | ||
* is NOT possible to use right now as it uses a hardcoded Activity under the hood | ||
* (i.e. [EmptyFragmentActivity]) which is not annotated with @AndroidEntryPoint. | ||
* | ||
* As a workaround, use this function that is equivalent. It requires you to add | ||
* [HiltTestActivity] in the debug folder and include it in the debug AndroidManifest.xml file | ||
* as can be found in this project. | ||
*/ | ||
inline fun <reified T : Fragment> launchFragmentInHiltContainer( | ||
fragmentArgs: Bundle? = null, | ||
@StyleRes themeResId: Int = R.style.FragmentScenarioEmptyFragmentActivityTheme, | ||
crossinline action: Fragment.() -> Unit = {} | ||
) { | ||
val startActivityIntent = Intent.makeMainActivity( | ||
ComponentName( | ||
ApplicationProvider.getApplicationContext(), | ||
HiltTestActivity::class.java | ||
) | ||
).putExtra( | ||
"androidx.fragment.app.testing.FragmentScenario.EmptyFragmentActivity.THEME_EXTRAS_BUNDLE_KEY", | ||
themeResId | ||
) | ||
|
||
ActivityScenario.launch<HiltTestActivity>(startActivityIntent).onActivity { activity -> | ||
val fragment: Fragment = activity.supportFragmentManager.fragmentFactory.instantiate( | ||
Preconditions.checkNotNull(T::class.java.classLoader), | ||
T::class.java.name | ||
) | ||
fragment.arguments = fragmentArgs | ||
activity.supportFragmentManager | ||
.beginTransaction() | ||
.add(android.R.id.content, fragment, "") | ||
.commitNow() | ||
|
||
fragment.action() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.