Skip to content
This repository has been archived by the owner on Jan 11, 2022. It is now read-only.

Commit

Permalink
Plugin initialization component and telemetry (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
etanshaul authored Jan 11, 2019
1 parent 15a221c commit d629517
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class PluginInfo {
companion object {
private const val UNKNOWN_VERSION = "unknown"
const val CONTAINER_TOOLS_PLUGIN_ID = "com.google.container.tools"
const val GOOGLE_CLOUD_TOOLS_PLUGIN_ID = "com.google.gct.core"
const val PLUGIN_NAME_EXTERNAL = "google-container-tools-intellij"
const val PLUGIN_USER_AGENT = "google-container-tools-intellij-plugin"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class UsageTrackerManagerService(
get() = ServiceManager.getService(UsageTrackerManagerService::class.java)!!
}

fun hasUserRecordedTrackingPreference(): Boolean =
trackingPreferenceProperty.getValue(USAGE_TRACKING_PREFERENCE_KEY) != null

/**
* Tracking is considered "available" if it can be collected. This occurs when there is a
* retrievable Analytics ID, and we are not in unit test mode.
Expand Down Expand Up @@ -71,9 +74,10 @@ class UsageTrackerManagerService(

/**
* Stores the user tracking preference backed by a persistent [PropertiesComponent].
* Writes the value as a string because otherwise a false value unsets the property.
*/
fun setTrackingOptedIn(optIn: Boolean) {
trackingPreferenceProperty.setValue(USAGE_TRACKING_PREFERENCE_KEY, optIn)
trackingPreferenceProperty.setValue(USAGE_TRACKING_PREFERENCE_KEY, optIn.toString())
}

/**
Expand Down
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)
}
}
}
6 changes: 6 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
<xi:include href="/META-INF/skaffold.xml" xpointer="xpointer(/idea-plugin/*)"/>
<xi:include href="/META-INF/skaffold-editing.xml" xpointer="xpointer(/idea-plugin/*)"/>

<application-components>
<component>
<implementation-class>com.google.container.tools.init.PluginInitComponent</implementation-class>
</component>
</application-components>

<extensions defaultExtensionNs="com.intellij">
<errorHandler
implementation="com.google.container.tools.diagnostics.GoogleFeedbackErrorReporter"/>
Expand Down
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()) }
}
}

0 comments on commit d629517

Please sign in to comment.