-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from binaryshrey/feat/v1.3
Feat/v1.3
- Loading branch information
Showing
35 changed files
with
475 additions
and
57 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,49 @@ | ||
package dev.shreyansh.dice | ||
|
||
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() | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
app/src/androidTest/java/dev/shreyansh/dice/HiltTestRunner.kt
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,17 @@ | ||
package dev.shreyansh.dice | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import androidx.test.runner.AndroidJUnitRunner | ||
import dagger.hilt.android.testing.HiltTestApplication | ||
|
||
class HiltTestRunner : AndroidJUnitRunner() { | ||
|
||
override fun newApplication( | ||
cl: ClassLoader?, | ||
className: String?, | ||
context: Context? | ||
): Application { | ||
return super.newApplication(cl, HiltTestApplication::class.java.name, context) | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
app/src/androidTest/java/dev/shreyansh/dice/ui/intro/IntroFragmentTest.kt
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,52 @@ | ||
package dev.shreyansh.dice.ui.intro | ||
|
||
import android.os.Bundle | ||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule | ||
import androidx.fragment.app.testing.FragmentScenario | ||
import androidx.fragment.app.testing.launchFragmentInContainer | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.test.espresso.Espresso | ||
import androidx.test.espresso.assertion.ViewAssertions | ||
import androidx.test.espresso.matcher.ViewMatchers | ||
import androidx.test.espresso.matcher.ViewMatchers.withId | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.filters.MediumTest | ||
import androidx.test.filters.SmallTest | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import org.junit.Assert.* | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import dev.shreyansh.dice.R | ||
import dev.shreyansh.dice.launchFragmentInHiltContainer | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import org.junit.Rule | ||
|
||
@ExperimentalCoroutinesApi | ||
@MediumTest | ||
@HiltAndroidTest | ||
@RunWith(AndroidJUnit4::class) | ||
class IntroFragmentTest{ | ||
|
||
@get:Rule | ||
var hiltRule = HiltAndroidRule(this) | ||
|
||
@get:Rule | ||
var instantTaskExecutorRule = InstantTaskExecutorRule() | ||
|
||
@Before | ||
fun setup() { | ||
hiltRule.inject() | ||
} | ||
|
||
|
||
@Test | ||
fun testFrag(){ | ||
launchFragmentInHiltContainer<IntroFragment>(Bundle(),R.style.Theme_AppCompat) | ||
Espresso.onView(withId(R.id.appname_textView)) | ||
.check(ViewAssertions.matches(ViewMatchers.withText("DICE"))) | ||
|
||
|
||
} | ||
} |
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,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="dev.shreyansh.dice"> | ||
|
||
<application> | ||
<activity | ||
android:name=".HiltTestActivity" | ||
android:exported="false" | ||
android:theme="@style/Theme.Dice" /> | ||
</application> | ||
|
||
</manifest> |
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,7 @@ | ||
package dev.shreyansh.dice | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
||
@AndroidEntryPoint | ||
class HiltTestActivity : AppCompatActivity() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package dev.shreyansh.dice.di | ||
|
||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
class AppModule { | ||
|
||
@Singleton | ||
@Provides | ||
fun provideNetworkConnDeps(@ApplicationContext context: Context) : ConnectivityManager{ | ||
return context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
} | ||
} |
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.