|
| 1 | +/* |
| 2 | + * Android SDK for Matomo |
| 3 | + * |
| 4 | + * @link https://github.com/matomo-org/matomo-android-sdk |
| 5 | + * @license https://github.com/matomo-org/matomo-sdk-android/blob/master/LICENSE BSD-3 Clause |
| 6 | + */ |
| 7 | +package org.matomo.sdk |
| 8 | + |
| 9 | +import android.annotation.SuppressLint |
| 10 | +import android.app.Application |
| 11 | +import androidx.test.core.app.ApplicationProvider |
| 12 | +import org.hamcrest.MatcherAssert |
| 13 | +import org.hamcrest.Matchers |
| 14 | +import org.junit.Assert |
| 15 | +import org.junit.Test |
| 16 | +import org.junit.runner.RunWith |
| 17 | +import org.matomo.sdk.Matomo.Companion.getInstance |
| 18 | +import org.matomo.sdk.dispatcher.DefaultDispatcher |
| 19 | +import org.matomo.sdk.dispatcher.DefaultDispatcherFactory |
| 20 | +import org.matomo.sdk.dispatcher.Dispatcher |
| 21 | +import org.matomo.sdk.dispatcher.DispatcherFactory |
| 22 | +import org.matomo.sdk.dispatcher.EventCache |
| 23 | +import org.matomo.sdk.dispatcher.EventDiskCache |
| 24 | +import org.matomo.sdk.dispatcher.Packet |
| 25 | +import org.matomo.sdk.dispatcher.PacketFactory |
| 26 | +import org.matomo.sdk.dispatcher.PacketSender |
| 27 | +import org.matomo.sdk.extra.TrackHelper |
| 28 | +import org.matomo.sdk.tools.Connectivity |
| 29 | +import org.mockito.ArgumentMatchers |
| 30 | +import org.mockito.Mockito |
| 31 | +import org.robolectric.annotation.Config |
| 32 | +import testhelpers.BaseTest |
| 33 | +import testhelpers.FullEnvTestRunner |
| 34 | +import testhelpers.MatomoTestApplication |
| 35 | + |
| 36 | +@Config(sdk = [28], manifest = Config.NONE, application = MatomoTestApplication::class) |
| 37 | +@RunWith(FullEnvTestRunner::class) |
| 38 | +class MatomoTest : BaseTest() { |
| 39 | + @Test |
| 40 | + fun testNewTracker() { |
| 41 | + val app = ApplicationProvider.getApplicationContext<MatomoTestApplication>() |
| 42 | + val tracker = app.onCreateTrackerConfig().build(getInstance(ApplicationProvider.getApplicationContext())) |
| 43 | + Assert.assertNotNull(tracker) |
| 44 | + Assert.assertEquals(app.onCreateTrackerConfig().apiUrl, tracker.apiUrl) |
| 45 | + Assert.assertEquals(app.onCreateTrackerConfig().siteId.toLong(), tracker.siteId.toLong()) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun testNormalTracker() { |
| 50 | + val matomo = getInstance(ApplicationProvider.getApplicationContext()) |
| 51 | + val tracker = TrackerBuilder("http://test/matomo.php", 1, "Default Tracker").build(matomo) |
| 52 | + Assert.assertEquals("http://test/matomo.php", tracker.apiUrl) |
| 53 | + Assert.assertEquals(1, tracker.siteId.toLong()) |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun testTrackerNaming() { |
| 58 | + // TODO can we somehow detect naming collisions on tracker creation? |
| 59 | + // Would probably requiring us to track created trackers |
| 60 | + } |
| 61 | + |
| 62 | + @SuppressLint("InlinedApi") |
| 63 | + @Test |
| 64 | + fun testLowMemoryDispatch() { |
| 65 | + val app = ApplicationProvider.getApplicationContext<MatomoTestApplication>() |
| 66 | + val packetSender = Mockito.mock(PacketSender::class.java) |
| 67 | + app.matomo.dispatcherFactory = object : DefaultDispatcherFactory() { |
| 68 | + override fun build(tracker: Tracker): Dispatcher { |
| 69 | + return DefaultDispatcher( |
| 70 | + EventCache(EventDiskCache(tracker)), |
| 71 | + Connectivity(tracker.matomo.context), |
| 72 | + PacketFactory(tracker.apiUrl), |
| 73 | + packetSender |
| 74 | + ) |
| 75 | + } |
| 76 | + } |
| 77 | + val tracker = app.tracker |
| 78 | + Assert.assertNotNull(tracker) |
| 79 | + tracker.setDispatchInterval(-1) |
| 80 | + |
| 81 | + tracker.track(TrackHelper.track().screen("test").build()) |
| 82 | + tracker.dispatch() |
| 83 | + Mockito.verify(packetSender, Mockito.timeout(500).times(1)).send(ArgumentMatchers.any(Packet::class.java)) |
| 84 | + |
| 85 | + tracker.track(TrackHelper.track().screen("test").build()) |
| 86 | + Mockito.verify(packetSender, Mockito.timeout(500).times(1)).send(ArgumentMatchers.any(Packet::class.java)) |
| 87 | + |
| 88 | + app.onTrimMemory(Application.TRIM_MEMORY_UI_HIDDEN) |
| 89 | + Mockito.verify(packetSender, Mockito.timeout(500).atLeast(2)).send(ArgumentMatchers.any(Packet::class.java)) |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun testGetSettings() { |
| 94 | + val tracker1 = Mockito.mock(Tracker::class.java) |
| 95 | + Mockito.`when`(tracker1.name).thenReturn("1") |
| 96 | + val tracker2 = Mockito.mock(Tracker::class.java) |
| 97 | + Mockito.`when`(tracker2.name).thenReturn("2") |
| 98 | + val tracker3 = Mockito.mock(Tracker::class.java) |
| 99 | + Mockito.`when`(tracker3.name).thenReturn("1") |
| 100 | + |
| 101 | + val matomo = getInstance(ApplicationProvider.getApplicationContext()) |
| 102 | + Assert.assertEquals(matomo.getTrackerPreferences(tracker1), matomo.getTrackerPreferences(tracker1)) |
| 103 | + Assert.assertNotEquals(matomo.getTrackerPreferences(tracker1), matomo.getTrackerPreferences(tracker2)) |
| 104 | + Assert.assertEquals(matomo.getTrackerPreferences(tracker1), matomo.getTrackerPreferences(tracker3)) |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + fun testSetDispatcherFactory() { |
| 109 | + val matomo = getInstance(ApplicationProvider.getApplicationContext()) |
| 110 | + val dispatcher = Mockito.mock(Dispatcher::class.java) |
| 111 | + val factory = Mockito.mock(DispatcherFactory::class.java) |
| 112 | + Mockito.`when`(factory.build(ArgumentMatchers.any(Tracker::class.java))).thenReturn(dispatcher) |
| 113 | + MatcherAssert.assertThat(matomo.dispatcherFactory, Matchers.`is`(Matchers.not(Matchers.nullValue()))) |
| 114 | + matomo.dispatcherFactory = factory |
| 115 | + MatcherAssert.assertThat(matomo.dispatcherFactory, Matchers.`is`(factory)) |
| 116 | + } |
| 117 | +} |
0 commit comments