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 : Spek({ | |
// property declaration, setup and preparation are skipped | |
describe("show rate us logic") { | |
context("first conditions checks") { | |
context("buy clicked once") { | |
beforeEachTest { | |
prepareConditions(buyClickedTimes = 1) | |
} | |
it("should not show 'rate us'") { |
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() } |
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 { | |
// property declaration is skipped | |
@Before public void setUp() { | |
final Context mockedContext = new SPMockBuilder().createContext(); | |
rateUsPreferences = new RateUsPreferencesImpl(mockedContext); | |
buyPreferences = new BuyPreferencesImpl(mockedContext); | |
time = Mockito.mock(Time.class); | |
showRateUsLogic = new ShowRateUsLogic(rateUsPreferences, buyPreferences, time); | |
} |
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 { | |
// property declaration is skipped | |
@Before public void setUp() { | |
rateUsPreferences = new RateUsPreferencesMock(); | |
buyPreferences = new BuyPreferencesMock(); | |
time = new TimeMock(); | |
showRateUsLogic = new ShowRateUsLogic(rateUsPreferences, buyPreferences, time); | |
} | |
@Test public void onFirstCheckAndOneClickItShouldNotShow() { |
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
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
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
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 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() { |