|
| 1 | +package com.github.ivanshafran.unit_tests_evolution.v5 |
| 2 | + |
| 3 | +import com.github.ivanshafran.sharedpreferencesmock.SPMockBuilder |
| 4 | +import com.github.ivanshafran.unit_tests_evolution.BuyPreferencesImpl |
| 5 | +import com.github.ivanshafran.unit_tests_evolution.RateUsPreferencesImpl |
| 6 | +import com.github.ivanshafran.unit_tests_evolution.ShowRateUsLogic |
| 7 | +import com.github.ivanshafran.unit_tests_evolution.Time |
| 8 | +import com.nhaarman.mockitokotlin2.mock |
| 9 | +import com.nhaarman.mockitokotlin2.whenever |
| 10 | +import org.junit.Assert |
| 11 | +import org.spekframework.spek2.Spek |
| 12 | +import org.spekframework.spek2.style.specification.describe |
| 13 | +import java.util.* |
| 14 | +import java.util.concurrent.TimeUnit |
| 15 | + |
| 16 | +class ShowRateUsLogicTest : Spek({ |
| 17 | + |
| 18 | + val calendar = Calendar.getInstance() |
| 19 | + calendar.set(2019, Calendar.JULY, 7) |
| 20 | + |
| 21 | + val SOME_DAY_IN_MILLIS = calendar.timeInMillis |
| 22 | + val LESS_THAN_TWO_MONTHS = TimeUnit.DAYS.toMillis(59) |
| 23 | + val MORE_THAN_TWO_MONTHS = TimeUnit.DAYS.toMillis(61) |
| 24 | + |
| 25 | + val mockedContext = SPMockBuilder().createContext() |
| 26 | + |
| 27 | + val rateUsPreferences by memoized { RateUsPreferencesImpl(mockedContext) } |
| 28 | + val buyPreferences by memoized { BuyPreferencesImpl(mockedContext) } |
| 29 | + val time by memoized { mock<Time>() } |
| 30 | + val showRateUsLogic by memoized { |
| 31 | + ShowRateUsLogic( |
| 32 | + rateUsPreferences, |
| 33 | + buyPreferences, |
| 34 | + time |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + fun prepareConditions( |
| 39 | + buyClickedTimes: Int = 0, |
| 40 | + isNeverShownAgainClicked: Boolean = false, |
| 41 | + isRateNowClicked: Boolean = false, |
| 42 | + lastShownTimeMillis: Long = 0, |
| 43 | + currentTimeMillis: Long = System.currentTimeMillis() |
| 44 | + ) { |
| 45 | + repeat(buyClickedTimes) { buyPreferences.incrementBuyCount() } |
| 46 | + |
| 47 | + if (isNeverShownAgainClicked) rateUsPreferences.setNeverShownAgainClicked() |
| 48 | + if (isRateNowClicked) rateUsPreferences.setRateNowClickedClicked() |
| 49 | + rateUsPreferences.setLastShownTimeMillis(lastShownTimeMillis) |
| 50 | + |
| 51 | + whenever(time.getCurrentTimeMillis()).thenReturn(currentTimeMillis) |
| 52 | + } |
| 53 | + |
| 54 | + describe("show rate us logic") { |
| 55 | + |
| 56 | + context("first conditions checks") { |
| 57 | + context("buy clicked once") { |
| 58 | + beforeEachTest { |
| 59 | + prepareConditions(buyClickedTimes = 1) |
| 60 | + } |
| 61 | + |
| 62 | + it("should not show 'rate us'") { |
| 63 | + Assert.assertFalse(showRateUsLogic.shouldShowRateUs()) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + context("buy clicked two times") { |
| 68 | + beforeEachTest { |
| 69 | + prepareConditions(buyClickedTimes = 2) |
| 70 | + } |
| 71 | + |
| 72 | + it("should show 'rate us'") { |
| 73 | + Assert.assertTrue(showRateUsLogic.shouldShowRateUs()) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + context("'rate us' was shown already") { |
| 79 | + context("user clicked 'show me later' on the dialog") { |
| 80 | + context("less than two months passed and user clicks buy") { |
| 81 | + beforeEachTest { |
| 82 | + prepareConditions( |
| 83 | + buyClickedTimes = 3, |
| 84 | + lastShownTimeMillis = SOME_DAY_IN_MILLIS, |
| 85 | + currentTimeMillis = SOME_DAY_IN_MILLIS + MORE_THAN_TWO_MONTHS |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + it("should show 'rate us' again") { |
| 90 | + Assert.assertTrue(showRateUsLogic.shouldShowRateUs()) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + context("more than two months passed and user clicks buy") { |
| 95 | + beforeEachTest { |
| 96 | + prepareConditions( |
| 97 | + buyClickedTimes = 3, |
| 98 | + lastShownTimeMillis = SOME_DAY_IN_MILLIS, |
| 99 | + currentTimeMillis = SOME_DAY_IN_MILLIS + MORE_THAN_TWO_MONTHS |
| 100 | + ) |
| 101 | + } |
| 102 | + |
| 103 | + it("should show 'rate us' again") { |
| 104 | + Assert.assertTrue(showRateUsLogic.shouldShowRateUs()) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +}) |
0 commit comments