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
allprojects { | |
repositories { | |
... | |
maven { url 'https://jitpack.io' } | |
} | |
} |
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
dependencies { | |
testImplementation 'com.github.IvanShafran:shared-preferences-mock:1.0' | |
} |
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
class BuyPreferencesImpl(context: Context) : BuyPreferences { | |
companion object { | |
private val PREFERENCES_FILENAME = "BUY_PREFERENCES" | |
private val BUY_COUNT_KEY = "BUY_COUNT_KEY" | |
} | |
private val sharedPreferences = context.getSharedPreferences( | |
PREFERENCES_FILENAME, | |
Context.MODE_PRIVATE | |
) |
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
interface BuyPreferences { | |
fun incrementBuyCount() | |
fun getBuyCount(): Int | |
} | |
class BuyPreferencesImpl(context: Context) : BuyPreferences { | |
// ... | |
private val sharedPreferences: SharedPreferences = context.getSharedPreferences(...) | |
override fun incrementBuyCount() { |
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
interface Time { | |
fun getCurrentTimeMillis(): Long | |
} | |
class TimeImpl : Time { | |
override fun getCurrentTimeMillis() = System.currentTimeMillis() | |
} |
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
class ShowRateUsLogic( | |
private val rateUsPreferences: RateUsPreferences, | |
private val buyPreferences: BuyPreferences, | |
private val time: Time | |
) { | |
fun shouldShowRateUs(): Boolean { | |
val timeFromLastShown = time.getCurrentTimeMillis() - rateUsPreferences.getLastShownTimeMillis() | |
return when { | |
// User doesn't want to see "rate us" again | |
rateUsPreferences.isNeverShownAgainClicked() -> false |
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
public class BuyPreferencesMock implements BuyPreferences { | |
private int count; | |
@Override public void incrementBuyCount() { | |
++count; | |
} | |
@Override public int getBuyCount() { | |
return count; | |
} |
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
public class ShowRateUsLogicTest { | |
private RateUsPreferencesMock rateUsPreferences; | |
private BuyPreferencesMock buyPreferences; | |
private TimeMock time; | |
private ShowRateUsLogic showRateUsLogic; | |
@Test public void test1() { | |
rateUsPreferences = new RateUsPreferencesMock(); | |
buyPreferences = new BuyPreferencesMock(); | |
time = new TimeMock(); |
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
class ShowRateUsLogicTest { | |
// property declaration and setup are skipped | |
private fun prepareConditions( | |
buyClickedTimes: Int = 0, | |
isNeverShownAgainClicked: Boolean = false, | |
isRateNowClicked: Boolean = false, | |
lastShownTimeMillis: Long = 0, | |
currentTimeMillis: Long = 0 | |
) { | |
repeat(buyClickedTimes) { buyPreferences.incrementBuyCount() } |