Skip to content

Commit 5c2135a

Browse files
authored
Merge pull request #132 from superwall/ir/fix/memory-and-threading
Optimise memory and threading
2 parents 5050413 + be43f26 commit 5c2135a

36 files changed

+937
-554
lines changed

app/src/androidTest/java/com/example/superapp/test/FlowScreenshotTestExecutor.kt

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import com.example.superapp.utils.waitFor
1212
import com.superwall.sdk.Superwall
1313
import com.superwall.sdk.analytics.superwall.SuperwallEvent
1414
import com.superwall.superapp.test.UITestHandler
15+
import kotlinx.coroutines.CoroutineScope
1516
import kotlinx.coroutines.Dispatchers
16-
import kotlinx.coroutines.launch
17+
import kotlinx.coroutines.async
1718
import org.junit.Rule
1819
import org.junit.Test
1920
import org.junit.runner.RunWith
@@ -29,6 +30,8 @@ class FlowScreenshotTestExecutor {
2930
imageComparator = CustomComparator(),
3031
)
3132

33+
val mainScope = CoroutineScope(Dispatchers.Main)
34+
3235
@Test
3336
fun test_paywall_reappers_with_video() =
3437
with(dropshots) {
@@ -37,7 +40,7 @@ class FlowScreenshotTestExecutor {
3740
it.waitFor { it is SuperwallEvent.PaywallWebviewLoadComplete }
3841
awaitUntilShimmerDisappears()
3942
awaitUntilWebviewAppears()
40-
delayFor(100.milliseconds)
43+
delayFor(500.milliseconds)
4144
}
4245
step("second_paywall") {
4346
awaitUntilWebviewAppears()
@@ -56,23 +59,33 @@ class FlowScreenshotTestExecutor {
5659
awaitUntilWebviewAppears()
5760
delayFor(500.milliseconds)
5861
// We scroll a bit to display the button
59-
Superwall.instance.paywallViewController
60-
?.webView
61-
?.scrollBy(0, 300)
62+
mainScope
63+
.async {
64+
Superwall.instance.paywallView
65+
?.webView
66+
?.scrollBy(0, 300) ?: kotlin.run {
67+
throw IllegalStateException("No viewcontroller found")
68+
}
69+
}.await()
6270
// We delay a bit to ensure the button is visible
6371
delayFor(100.milliseconds)
6472
// We scroll back to the top
65-
Superwall.instance.paywallViewController
66-
?.webView
67-
?.scrollTo(0, 0)
73+
mainScope
74+
.async {
75+
Superwall.instance.paywallView
76+
?.webView
77+
?.scrollTo(0, 0) ?: kotlin.run {
78+
throw IllegalStateException("No viewcontroller found")
79+
}
80+
}.await()
6881
// We delay a bit to ensure scroll has finished
6982
delayFor(1.seconds)
7083
}
7184
}
7285
}
7386

7487
@Test
75-
fun test_paywall_reappers_after_dismissing() =
88+
fun test_paywall_reappears_after_dismissing() =
7689
with(dropshots) {
7790
screenshotFlow(UITestHandler.test11Info) {
7891
step {
@@ -86,7 +99,6 @@ class FlowScreenshotTestExecutor {
8699
delayFor(1.seconds)
87100
}
88101
step {
89-
it.waitFor { it is SuperwallEvent.PaywallClose }
90102
it.waitFor { it is SuperwallEvent.PaywallOpen }
91103
awaitUntilWebviewAppears()
92104
delayFor(1.seconds)
@@ -100,7 +112,8 @@ class FlowScreenshotTestExecutor {
100112
screenshotFlow(UITestHandler.test34Info) {
101113
step("") {
102114
it.waitFor { it is SuperwallEvent.PaywallWebviewLoadComplete }
103-
awaitUntilShimmerDisappears() || awaitUntilWebviewAppears()
115+
awaitUntilShimmerDisappears()
116+
awaitUntilWebviewAppears()
104117
}
105118
step {
106119
it.waitFor { it is SuperwallEvent.Reset }
@@ -117,27 +130,36 @@ class FlowScreenshotTestExecutor {
117130
it.waitFor { it is SuperwallEvent.PaywallWebviewLoadComplete }
118131
awaitUntilShimmerDisappears()
119132
awaitUntilWebviewAppears()
120-
delayFor(500.milliseconds)
121-
launch(Dispatchers.Main) {
122-
// We scroll a bit to display the button
123-
Superwall.instance.paywallViewController
124-
?.webView
125-
?.scrollBy(0, 300)
126-
}
133+
delayFor(100.milliseconds)
134+
mainScope
135+
.async {
136+
// We scroll a bit to display the button
137+
Superwall.instance.paywallView
138+
?.webView
139+
?.apply {
140+
// Disable the scrollbar for the test
141+
// so its not visible in screenshots
142+
isVerticalScrollBarEnabled = false
143+
scrollTo(0, 300)
144+
}
145+
}.await()
127146
// We delay a bit to ensure the button is visible
128147
delayFor(100.milliseconds)
129148
// We scroll back to the top
130-
launch(Dispatchers.Main) {
131-
Superwall.instance.paywallViewController
132-
?.webView
133-
?.scrollTo(0, 0)
134-
}
149+
mainScope
150+
.async {
151+
Superwall.instance.paywallView
152+
?.webView
153+
?.apply {
154+
scrollTo(0, 0)
155+
}
156+
}.await()
135157
// We delay a bit to ensure scroll has finished
136-
delayFor(1.seconds)
158+
delayFor(500.milliseconds)
137159
}
138160

139161
step {
140-
it.waitFor { it is SuperwallEvent.PaywallClose }
162+
delayFor(10.seconds)
141163
}
142164
}
143165
}

app/src/androidTest/java/com/example/superapp/utils/TestingUtils.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fun Dropshots.screenshotFlow(
110110
}
111111
}
112112

113-
runTest(timeout = 3.minutes) {
113+
runTest(timeout = 5.minutes) {
114114
try {
115115
flow.steps.forEach {
116116
if (!testReady.value) {
@@ -164,12 +164,12 @@ suspend fun CoroutineScope.awaitUntilShimmerDisappears(): Boolean {
164164
}
165165

166166
private fun getShimmerFromPaywall() =
167-
Superwall.instance.paywallViewController
167+
Superwall.instance.paywallView
168168
?.children
169169
?.find { it is ShimmerView }
170170

171171
private fun getWebviewFromPaywall() =
172-
Superwall.instance.paywallViewController
172+
Superwall.instance.paywallView
173173
?.children
174174
?.find { it is WebView }
175175

-565 Bytes
Loading
-1.03 KB
Loading
94.6 KB
Loading
20 KB
Loading
-4.88 KB
Loading
729 Bytes
Loading
28.1 KB
Loading
-4.95 KB
Loading

0 commit comments

Comments
 (0)