This repository has been archived by the owner on Jan 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plugin initialization component and telemetry (#125)
- Loading branch information
Showing
5 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
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
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/com/google/container/tools/init/PluginInitComponent.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,46 @@ | ||
/* | ||
* Copyright 2018 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.container.tools.init | ||
|
||
import com.google.container.tools.core.PluginInfo | ||
import com.google.container.tools.core.analytics.UsageTrackerManagerService | ||
import com.intellij.ide.plugins.PluginManager | ||
import com.intellij.openapi.components.BaseComponent | ||
import com.intellij.openapi.extensions.PluginId | ||
|
||
/** | ||
* Performs initialization tasks when the plugin is loaded. | ||
*/ | ||
class PluginInitComponent : BaseComponent { | ||
|
||
override fun initComponent() { | ||
val isGctInstalled = | ||
PluginManager.isPluginInstalled(PluginId.getId(PluginInfo.GOOGLE_CLOUD_TOOLS_PLUGIN_ID)) | ||
|
||
/* Initialize feature usage tracking. Only do this if: | ||
1. The Google Cloud Tools plugin is not installed | ||
2. Usage tracking is available | ||
3. A tracking preference has not already been recorded | ||
*/ | ||
if (!isGctInstalled && | ||
UsageTrackerManagerService.instance.isUsageTrackingAvailable() && | ||
!UsageTrackerManagerService.instance.hasUserRecordedTrackingPreference() | ||
) { | ||
UsageTrackerManagerService.instance.setTrackingOptedIn(true) | ||
} | ||
} | ||
} |
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
90 changes: 90 additions & 0 deletions
90
src/test/kotlin/com/google/container/tools/init/PluginInitComponentTest.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 2018 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.container.tools.init | ||
|
||
import com.google.container.tools.core.analytics.UsageTrackerManagerService | ||
import com.google.container.tools.test.ContainerToolsRule | ||
import com.google.container.tools.test.TestService | ||
import com.intellij.ide.plugins.PluginManagerCore | ||
import com.intellij.ide.plugins.PluginNode | ||
import com.intellij.openapi.extensions.PluginId | ||
import io.mockk.every | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.verify | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
/** | ||
* Tests for [PluginInitComponent]. | ||
*/ | ||
class PluginInitComponentTest { | ||
@get:Rule | ||
val containerToolsRule = ContainerToolsRule(this) | ||
|
||
@MockK | ||
@TestService | ||
private lateinit var usageTrackerManagerService: UsageTrackerManagerService | ||
|
||
private lateinit var pluginInitComponent: PluginInitComponent | ||
|
||
@Before | ||
fun setUp() { | ||
pluginInitComponent = PluginInitComponent() | ||
} | ||
|
||
@Test | ||
fun `feature tracking is set in when all conditions are met`() { | ||
every { usageTrackerManagerService.isUsageTrackingAvailable() } answers { true } | ||
every { usageTrackerManagerService.hasUserRecordedTrackingPreference() } answers { false } | ||
|
||
pluginInitComponent.initComponent() | ||
|
||
verify { usageTrackerManagerService.setTrackingOptedIn(true) } | ||
} | ||
|
||
@Test | ||
fun `feature tracking is not set when the gct plugin is installed`() { | ||
PluginManagerCore.setPlugins(arrayOf(PluginNode(PluginId.getId("com.google.gct.core")))) | ||
every { usageTrackerManagerService.isUsageTrackingAvailable() } answers { true } | ||
every { usageTrackerManagerService.hasUserRecordedTrackingPreference() } answers { false } | ||
|
||
pluginInitComponent.initComponent() | ||
|
||
verify(exactly = 0) { usageTrackerManagerService.setTrackingOptedIn(any()) } | ||
} | ||
|
||
@Test | ||
fun `feature tracking is not set when the feature tracking is not available`() { | ||
every { usageTrackerManagerService.isUsageTrackingAvailable() } answers { false } | ||
every { usageTrackerManagerService.hasUserRecordedTrackingPreference() } answers { false } | ||
|
||
pluginInitComponent.initComponent() | ||
|
||
verify(exactly = 0) { usageTrackerManagerService.setTrackingOptedIn(any()) } | ||
} | ||
|
||
@Test | ||
fun `feature tracking is not set when the feature tracking preference has already been set`() { | ||
every { usageTrackerManagerService.isUsageTrackingAvailable() } answers { true } | ||
every { usageTrackerManagerService.hasUserRecordedTrackingPreference() } answers { true } | ||
|
||
pluginInitComponent.initComponent() | ||
|
||
verify(exactly = 0) { usageTrackerManagerService.setTrackingOptedIn(any()) } | ||
} | ||
} |