-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not track the screen view again when app comes to foreground (close …
- Loading branch information
1 parent
dd9f1af
commit 5975f74
Showing
3 changed files
with
106 additions
and
5 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
.../src/androidTest/java/com/snowplowanalytics/snowplow/screen/ScreenViewAutotrackingTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright (c) 2015-2023 Snowplow Analytics Ltd. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
*/ | ||
package com.snowplowanalytics.snowplow.tracker | ||
|
||
import android.content.Context | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import com.snowplowanalytics.core.constants.TrackerConstants | ||
import com.snowplowanalytics.core.utils.NotificationCenter | ||
import com.snowplowanalytics.snowplow.Snowplow | ||
import com.snowplowanalytics.snowplow.Snowplow.removeAllTrackers | ||
import com.snowplowanalytics.snowplow.configuration.Configuration | ||
import com.snowplowanalytics.snowplow.configuration.NetworkConfiguration | ||
import com.snowplowanalytics.snowplow.configuration.PluginConfiguration | ||
import com.snowplowanalytics.snowplow.controller.TrackerController | ||
import com.snowplowanalytics.snowplow.event.ScreenView | ||
import com.snowplowanalytics.snowplow.event.Structured | ||
import com.snowplowanalytics.snowplow.network.HttpMethod | ||
import com.snowplowanalytics.snowplow.payload.SelfDescribingJson | ||
import org.junit.After | ||
import org.junit.Assert | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import java.util.* | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class ScreenViewAutotrackingTest { | ||
|
||
@After | ||
fun tearDown() { | ||
removeAllTrackers() | ||
} | ||
|
||
// --- TESTS | ||
@Test | ||
fun doesntTrackTheSameScreenViewMultipleTimes() { | ||
var numberOfScreenViews = 0 | ||
|
||
val countPlugin = PluginConfiguration("test") | ||
countPlugin.afterTrack { | ||
if (it.schema == TrackerConstants.SCHEMA_SCREEN_VIEW) { | ||
numberOfScreenViews += 1 | ||
} | ||
} | ||
|
||
createTracker(listOf(countPlugin)) | ||
Thread.sleep(200) | ||
|
||
NotificationCenter.postNotification("SnowplowScreenView", mapOf( | ||
"event" to ScreenView(name = "Screen1").activityClassName("Screen1") | ||
)) | ||
Thread.sleep(200) | ||
|
||
NotificationCenter.postNotification("SnowplowScreenView", mapOf( | ||
"event" to ScreenView(name = "Screen1").activityClassName("Screen1") | ||
)) | ||
Thread.sleep(200) | ||
|
||
NotificationCenter.postNotification("SnowplowScreenView", mapOf( | ||
"event" to ScreenView(name = "Screen2").activityClassName("Screen2") | ||
)) | ||
Thread.sleep(200) | ||
|
||
Assert.assertEquals(2, numberOfScreenViews) | ||
} | ||
|
||
// --- PRIVATE | ||
private val context: Context | ||
get() = InstrumentationRegistry.getInstrumentation().targetContext | ||
|
||
private fun createTracker(configurations: List<Configuration>): TrackerController { | ||
val networkConfig = NetworkConfiguration(MockNetworkConnection(HttpMethod.POST, 200)) | ||
return Snowplow.createTracker( | ||
context, | ||
namespace = "testScreenView" + Math.random().toString(), | ||
network = networkConfig, | ||
configurations = configurations.toTypedArray() | ||
) | ||
} | ||
} |
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
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