Skip to content

Commit 2762516

Browse files
committed
test(android): add mock provider and manager in test app
1 parent e9d0f1b commit 2762516

File tree

1 file changed

+136
-1
lines changed

1 file changed

+136
-1
lines changed
Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,149 @@
11
package io.ionic.portals.testapp
22

33
import android.app.Application
4+
import android.content.Context
5+
import android.util.Log
6+
7+
import io.ionic.liveupdatesprovider.LiveUpdatesError.InvalidConfiguration
8+
import io.ionic.liveupdatesprovider.LiveUpdatesError.SyncFailed
9+
import io.ionic.liveupdatesprovider.LiveUpdatesManager
10+
import io.ionic.liveupdatesprovider.LiveUpdatesProvider
11+
import io.ionic.liveupdatesprovider.LiveUpdatesRegistry
12+
import io.ionic.liveupdatesprovider.SyncCallback
13+
import io.ionic.liveupdatesprovider.models.ProviderConfig
14+
import io.ionic.liveupdatesprovider.models.SyncResult
415
import io.ionic.portals.PortalManager
16+
import java.io.File
17+
18+
19+
/**
20+
* Mock implementation of LiveUpdatesManager for testing purposes.
21+
* Allows testing config parsing and sync behavior without actual network requests.
22+
*/
23+
internal class MockLiveUpdatesManager(
24+
private val appId: String?,
25+
private val channel: String?,
26+
private val latestAppDir: File?,
27+
private val shouldFail: Boolean,
28+
private val failureDetails: String,
29+
private val didUpdate: Boolean
30+
) : LiveUpdatesManager {
31+
override fun sync(callback: SyncCallback?) {
32+
// Simulate async behavior with a small delay
33+
Thread(Runnable {
34+
try {
35+
Thread.sleep(5000)
36+
} catch (e: InterruptedException) {
37+
e.printStackTrace()
38+
}
39+
if (this.shouldFail) {
40+
val error = SyncFailed(this.failureDetails, null)
41+
if (callback != null) {
42+
callback.onError(error)
43+
}
44+
} else {
45+
val result = SyncResult(this.didUpdate, this.latestAppDir)
46+
if (callback != null) {
47+
callback.onComplete(result)
48+
}
49+
}
50+
}).start()
51+
}
52+
53+
override fun latestAppDirectory(): File? {
54+
return this.latestAppDir
55+
}
56+
}
57+
58+
59+
class MockLiveUpdatesProvider(override val id: String) : LiveUpdatesProvider {
60+
@Throws(InvalidConfiguration::class)
61+
override fun createManager(context: Context, config: ProviderConfig): LiveUpdatesManager {
62+
63+
val data: MutableMap<String?, Any?> = config.data as MutableMap<String?, Any?>
64+
var shouldFail = false
65+
val shouldFailObj = data.get("shouldFail")
66+
if (shouldFailObj is Boolean) {
67+
shouldFail = shouldFailObj
68+
}
69+
70+
var failureDetails = "Mock sync failed"
71+
val failureDetailsObj = data.get("failureDetails")
72+
if (failureDetailsObj is String) {
73+
failureDetails = failureDetailsObj
74+
}
75+
76+
var didUpdate = false
77+
val didUpdateObj = data.get("didUpdate")
78+
if (didUpdateObj is Boolean) {
79+
didUpdate = didUpdateObj
80+
}
81+
82+
// For testing purposes, we can point to a static directory that simulates the latest app version.
83+
// This was gotten from the logs after a successful sync with the real provider
84+
val filePath =
85+
"/data/user/0/io.ionic.portals.ecommercewebapp/files/ionic_apps/3fde24f8/5966bde5-da2e-4b40-8487-2b0fef7c458b"
86+
val latestAppDir = File(filePath)
87+
88+
return MockLiveUpdatesManager(
89+
data.get("appId") as? String,
90+
data.get("channel") as? String,
91+
latestAppDir, // latestAppDir
92+
shouldFail,
93+
failureDetails,
94+
didUpdate
95+
)
96+
}
97+
}
98+
599

6100
class TestApplication: Application() {
7101

8102
override fun onCreate() {
9103
super.onCreate()
10104

11105
PortalManager.register(BuildConfig.PORTALS_KEY)
12-
PortalManager.newPortal("testportal").create()
106+
Log.d("TestApplication", "Registered portal with key: ${BuildConfig.PORTALS_KEY}")
107+
val portalBuilder = PortalManager.newPortal("testportal")
108+
109+
110+
111+
// Register provider
112+
LiveUpdatesRegistry.register(MockLiveUpdatesProvider("mock"))
113+
114+
// Resolve the provider where you want in the app
115+
val provider = LiveUpdatesRegistry.resolve("mock")
116+
if (provider == null) {
117+
Log.e("TestApplication", "Failed to register MockLiveUpdatesProvider")
118+
} else {
119+
Log.d("TestApplication", "Successfully registered MockLiveUpdatesProvider with ID: ${provider.id}")
120+
}
121+
122+
// create the 3rd party manager
123+
val manager = provider?.createManager(
124+
this,
125+
ProviderConfig(
126+
mapOf(
127+
"appId" to "testAppId",
128+
"channel" to "testChannel",
129+
"shouldFail" to false,
130+
"failureDetails" to "Simulated sync failure",
131+
"didUpdate" to true,
132+
"endpoint" to "https://cloud.provider.io",
133+
"apiKey" to "<PROVIDER_API_KEY>"
134+
)
135+
)
136+
)
137+
if (manager == null) {
138+
Log.e("TestApplication", "Failed to create LiveUpdatesManager from MockLiveUpdatesProvider")
139+
} else {
140+
Log.d("TestApplication", "Successfully created LiveUpdatesManager from MockLiveUpdatesProvider")
141+
142+
143+
// set the 3rd party manager
144+
portalBuilder.setLiveUpdateManager(this.applicationContext, manager);
145+
}
146+
147+
val portal = portalBuilder.create()
13148
}
14149
}

0 commit comments

Comments
 (0)