summaryrefslogtreecommitdiff
path: root/logcat
diff options
context:
space:
mode:
authorAlon Albert <aalbert@google.com>2022-07-14 13:25:10 -0700
committerTreeHugger Robot <treehugger-gerrit@google.com>2022-07-14 22:11:04 +0000
commit9291aaca74cf8a5e5b15bea833ffde1815d32d21 (patch)
tree931449a55a4bd0caaaaf28d7b5832647eee90949 /logcat
parentfe28285db7d9853c0f49c86674b0cd86f5874945 (diff)
downloadidea-9291aaca74cf8a5e5b15bea833ffde1815d32d21.tar.gz
Register The ProjectAppMonitor
Broken by http://ag/18615157 Added a test to verify it's installed properly. Fixes: 238795013 Test: Added Change-Id: I3a99954d51e04ee4366990bf5ce5d27bcda4f686
Diffstat (limited to 'logcat')
-rw-r--r--logcat/src/com/android/tools/idea/logcat/LogcatMainPanel.kt7
-rw-r--r--logcat/src/com/android/tools/idea/logcat/ProjectAppMonitor.kt12
-rw-r--r--logcat/testSrc/com/android/tools/idea/logcat/LogcatMainPanelTest.kt43
-rw-r--r--logcat/testSrc/com/android/tools/idea/logcat/ProjectAppMonitorTest.kt120
4 files changed, 125 insertions, 57 deletions
diff --git a/logcat/src/com/android/tools/idea/logcat/LogcatMainPanel.kt b/logcat/src/com/android/tools/idea/logcat/LogcatMainPanel.kt
index 58f989327c6..6c68ecb222e 100644
--- a/logcat/src/com/android/tools/idea/logcat/LogcatMainPanel.kt
+++ b/logcat/src/com/android/tools/idea/logcat/LogcatMainPanel.kt
@@ -17,6 +17,7 @@ package com.android.tools.idea.logcat
import com.android.adblib.AdbSession
import com.android.annotations.concurrency.UiThread
+import com.android.ddmlib.AndroidDebugBridge
import com.android.tools.adtui.toolwindow.splittingtabs.state.SplittingTabsStateProvider
import com.android.tools.idea.adb.processnamemonitor.ProcessNameMonitor
import com.android.tools.idea.adblib.AdbLibService
@@ -224,6 +225,7 @@ internal class LogcatMainPanel(
private var ignoreCaretAtBottom = false // Derived from similar code in ConsoleViewImpl. See initScrollToEndStateHandling()
private val connectedDevice = AtomicReference<Device?>()
private val logcatServiceChannel = Channel<LogcatServiceEvent>(1)
+ private val clientListener = ProjectAppMonitor(this, packageNamesProvider)
init {
editor.apply {
@@ -291,6 +293,9 @@ internal class LogcatMainPanel(
}
}
}
+
+ AndroidDebugBridge.addDeviceChangeListener(clientListener)
+ AndroidDebugBridge.addClientChangeListener(clientListener)
}
private fun getPopupActionGroup(actions: Array<AnAction>): ActionGroup {
@@ -393,6 +398,8 @@ internal class LogcatMainPanel(
override fun dispose() {
EditorFactory.getInstance().releaseEditor(editor)
+ AndroidDebugBridge.removeDeviceChangeListener(clientListener)
+ AndroidDebugBridge.removeClientChangeListener(clientListener)
}
override fun applyLogcatSettings(logcatSettings: AndroidLogcatSettings) {
diff --git a/logcat/src/com/android/tools/idea/logcat/ProjectAppMonitor.kt b/logcat/src/com/android/tools/idea/logcat/ProjectAppMonitor.kt
index fb53954fe04..f57b75800b0 100644
--- a/logcat/src/com/android/tools/idea/logcat/ProjectAppMonitor.kt
+++ b/logcat/src/com/android/tools/idea/logcat/ProjectAppMonitor.kt
@@ -36,22 +36,16 @@ import org.jetbrains.annotations.VisibleForTesting
internal class ProjectAppMonitor(
private val logcatPresenter: LogcatPresenter,
private val projectPackageNamesProvider: PackageNamesProvider,
- private val device: IDevice,
) : IDeviceChangeListener, IClientChangeListener {
@GuardedBy("itself")
private val projectClients = mutableMapOf<Int, Client>()
- init {
- // Initialize the projectClients list by triggering a deviceChanged(CHANGE_CLIENT_LIST) event.
- deviceChanged(device, CHANGE_CLIENT_LIST)
- }
-
override fun deviceConnected(device: IDevice) {}
override fun deviceDisconnected(device: IDevice) {}
@AnyThread
override fun deviceChanged(device: IDevice, changeMask: Int) {
- if (device != this.device || changeMask and CHANGE_CLIENT_LIST == 0) {
+ if (device.serialNumber != getMonitoredSerialNumber() || changeMask and CHANGE_CLIENT_LIST == 0) {
return
}
@@ -74,7 +68,7 @@ internal class ProjectAppMonitor(
@AnyThread
override fun clientChanged(client: Client, changeMask: Int) {
- if (changeMask and CHANGE_NAME == 0 || client.device != this.device || !client.isProjectClient()) {
+ if (changeMask and CHANGE_NAME == 0 || client.device.serialNumber != getMonitoredSerialNumber() || !client.isProjectClient()) {
return
}
val added = synchronized(projectClients) {
@@ -87,6 +81,8 @@ internal class ProjectAppMonitor(
}
}
+ private fun getMonitoredSerialNumber() = logcatPresenter.getConnectedDevice()?.serialNumber
+
private fun Client.isProjectClient() = clientData.packageName in projectPackageNamesProvider.getPackageNames()
}
diff --git a/logcat/testSrc/com/android/tools/idea/logcat/LogcatMainPanelTest.kt b/logcat/testSrc/com/android/tools/idea/logcat/LogcatMainPanelTest.kt
index 2a68f6372a0..4017f61fe94 100644
--- a/logcat/testSrc/com/android/tools/idea/logcat/LogcatMainPanelTest.kt
+++ b/logcat/testSrc/com/android/tools/idea/logcat/LogcatMainPanelTest.kt
@@ -18,8 +18,14 @@ package com.android.tools.idea.logcat
import com.android.adblib.AdbSession
import com.android.adblib.DeviceState
import com.android.adblib.testing.FakeAdbSession
+import com.android.ddmlib.AndroidDebugBridge
+import com.android.ddmlib.Client
+import com.android.ddmlib.ClientData
+import com.android.ddmlib.IDevice
+import com.android.ddmlib.IDevice.CHANGE_CLIENT_LIST
import com.android.testutils.MockitoKt.eq
import com.android.testutils.MockitoKt.mock
+import com.android.testutils.MockitoKt.whenever
import com.android.tools.adtui.TreeWalker
import com.android.tools.adtui.swing.FakeMouse.Button.CTRL_LEFT
import com.android.tools.adtui.swing.FakeUi
@@ -948,6 +954,43 @@ class LogcatMainPanelTest {
assertThat(banner.text).isEqualTo("Logcat is paused")
}
+ @Test
+ fun projectAppMonitorInstalled() {
+ val testDevice = TestDevice("device1", DeviceState.ONLINE, 11, 30, "Google", "Pixel", "")
+ fakeAdbSession.deviceServices.setupCommandsForDevice(testDevice)
+ fakeAdbSession.hostServices.setDevices(testDevice)
+ val fakePackageNamesProvider = FakePackageNamesProvider("myapp")
+ val logcatMainPanel = runInEdtAndGet {
+ logcatMainPanel(adbSession = fakeAdbSession, packageNamesProvider = fakePackageNamesProvider).also {
+ waitForCondition(TIMEOUT_SEC, SECONDS) { it.getConnectedDevice() != null }
+ }
+ }
+ val iDevice = mock<IDevice>()
+ val client = mock<Client>()
+ val clientData = mock<ClientData>()
+
+ whenever(clientData.packageName).thenReturn("myapp")
+ whenever(client.clientData).thenReturn(clientData)
+ whenever(iDevice.serialNumber).thenReturn("device1")
+ whenever(iDevice.clients).thenReturn(arrayOf(client))
+ AndroidDebugBridge.deviceChanged(iDevice, CHANGE_CLIENT_LIST)
+
+ waitForCondition(TIMEOUT_SEC, SECONDS) {
+ logcatMainPanel.editor.document.text.contains("PROCESS STARTED (0) for package myapp")
+ }
+ }
+
+ @RunsInEdt
+ @Test
+ fun projectAppMonitorRemoved() {
+ val logcatMainPanel = logcatMainPanel()
+ assertThat(AndroidDebugBridge.getDeviceChangeListenerCount() == 1)
+
+ Disposer.dispose(logcatMainPanel)
+
+ assertThat(AndroidDebugBridge.getDeviceChangeListenerCount() == 0)
+ }
+
private fun logcatMainPanel(
splitterPopupActionGroup: ActionGroup = EMPTY_GROUP,
logcatColors: LogcatColors = LogcatColors(),
diff --git a/logcat/testSrc/com/android/tools/idea/logcat/ProjectAppMonitorTest.kt b/logcat/testSrc/com/android/tools/idea/logcat/ProjectAppMonitorTest.kt
index 7ce46966c4f..7b547c085e4 100644
--- a/logcat/testSrc/com/android/tools/idea/logcat/ProjectAppMonitorTest.kt
+++ b/logcat/testSrc/com/android/tools/idea/logcat/ProjectAppMonitorTest.kt
@@ -24,6 +24,7 @@ import com.android.ddmlib.IDevice.CHANGE_CLIENT_LIST
import com.android.ddmlib.IDevice.CHANGE_STATE
import com.android.testutils.MockitoKt.mock
import com.android.testutils.MockitoKt.whenever
+import com.android.tools.idea.logcat.devices.Device
import com.google.common.truth.Truth.assertThat
import com.intellij.openapi.util.Disposer
import com.intellij.testFramework.ApplicationRule
@@ -45,7 +46,7 @@ class ProjectAppMonitorTest {
@get:Rule
val rule = ApplicationRule()
- private val device = mock<IDevice>().apply { setClients() }
+ private val device1 = mockDevice("device1")
private val logcatPresenter = FakeLogcatPresenter()
@After
@@ -55,10 +56,11 @@ class ProjectAppMonitorTest {
@Test
fun deviceChanged_addClients() {
- val projectAppMonitor = projectAppMonitor(logcatPresenter, device, "client1", "client2")
+ logcatPresenter.attachedDevice = createDevice(device1.serialNumber)
+ val projectAppMonitor = projectAppMonitor(logcatPresenter, "client1", "client2")
- device.setClients(client1, client2, client3)
- projectAppMonitor.deviceChanged(device, CHANGE_CLIENT_LIST)
+ device1.setClients(client1, client2, client3)
+ projectAppMonitor.deviceChanged(device1, CHANGE_CLIENT_LIST)
runInEdtAndWait {
assertThat(logcatPresenter.messageBatches).containsExactly(listOf(client1.startedMessage(), client2.startedMessage()))
@@ -67,12 +69,13 @@ class ProjectAppMonitorTest {
@Test
fun deviceChanged_addMoreClients() {
- val projectAppMonitor = projectAppMonitor(logcatPresenter, device, "client1", "client2", "client3")
- device.setClients(client1)
- projectAppMonitor.deviceChanged(device, CHANGE_CLIENT_LIST)
+ logcatPresenter.attachedDevice = createDevice(device1.serialNumber)
+ val projectAppMonitor = projectAppMonitor(logcatPresenter, "client1", "client2", "client3")
+ device1.setClients(client1)
+ projectAppMonitor.deviceChanged(device1, CHANGE_CLIENT_LIST)
- device.setClients(client1, client2, client3)
- projectAppMonitor.deviceChanged(device, CHANGE_CLIENT_LIST)
+ device1.setClients(client1, client2, client3)
+ projectAppMonitor.deviceChanged(device1, CHANGE_CLIENT_LIST)
runInEdtAndWait {
assertThat(logcatPresenter.messageBatches).containsExactly(
@@ -84,12 +87,13 @@ class ProjectAppMonitorTest {
@Test
fun deviceChanged_removeClients() {
- val projectAppMonitor = projectAppMonitor(logcatPresenter, device, "client1", "client2", "client3")
- device.setClients(client1, client2, client3)
- projectAppMonitor.deviceChanged(device, CHANGE_CLIENT_LIST)
+ logcatPresenter.attachedDevice = createDevice(device1.serialNumber)
+ val projectAppMonitor = projectAppMonitor(logcatPresenter, "client1", "client2", "client3")
+ device1.setClients(client1, client2, client3)
+ projectAppMonitor.deviceChanged(device1, CHANGE_CLIENT_LIST)
- device.setClients(client3)
- projectAppMonitor.deviceChanged(device, CHANGE_CLIENT_LIST)
+ device1.setClients(client3)
+ projectAppMonitor.deviceChanged(device1, CHANGE_CLIENT_LIST)
runInEdtAndWait {
assertThat(logcatPresenter.messageBatches).containsExactly(
@@ -101,12 +105,13 @@ class ProjectAppMonitorTest {
@Test
fun deviceChanged_addAndRemoveClients() {
- val projectAppMonitor = projectAppMonitor(logcatPresenter, device, "client1", "client2", "client3")
- device.setClients(client1, client2)
- projectAppMonitor.deviceChanged(device, CHANGE_CLIENT_LIST)
+ logcatPresenter.attachedDevice = createDevice(device1.serialNumber)
+ val projectAppMonitor = projectAppMonitor(logcatPresenter, "client1", "client2", "client3")
+ device1.setClients(client1, client2)
+ projectAppMonitor.deviceChanged(device1, CHANGE_CLIENT_LIST)
- device.setClients(client2, client3)
- projectAppMonitor.deviceChanged(device, CHANGE_CLIENT_LIST)
+ device1.setClients(client2, client3)
+ projectAppMonitor.deviceChanged(device1, CHANGE_CLIENT_LIST)
runInEdtAndWait {
assertThat(logcatPresenter.messageBatches).containsExactly(
@@ -118,12 +123,13 @@ class ProjectAppMonitorTest {
@Test
fun deviceChanged_noChanges() {
- val projectAppMonitor = projectAppMonitor(logcatPresenter, device, "client1", "client2")
- device.setClients(client1, client2)
- projectAppMonitor.deviceChanged(device, CHANGE_CLIENT_LIST)
+ logcatPresenter.attachedDevice = createDevice(device1.serialNumber)
+ val projectAppMonitor = projectAppMonitor(logcatPresenter, "client1", "client2")
+ device1.setClients(client1, client2)
+ projectAppMonitor.deviceChanged(device1, CHANGE_CLIENT_LIST)
- device.setClients(client2, client1)
- projectAppMonitor.deviceChanged(device, CHANGE_CLIENT_LIST)
+ device1.setClients(client2, client1)
+ projectAppMonitor.deviceChanged(device1, CHANGE_CLIENT_LIST)
runInEdtAndWait {
assertThat(logcatPresenter.messageBatches).containsExactly(
@@ -134,10 +140,10 @@ class ProjectAppMonitorTest {
@Test
fun deviceChanged_wrongMask() {
- val projectAppMonitor = projectAppMonitor(logcatPresenter, device, "client1", "client2")
+ val projectAppMonitor = projectAppMonitor(logcatPresenter, "client1", "client2")
- device.setClients(client1, client2)
- projectAppMonitor.deviceChanged(device, CHANGE_STATE)
+ device1.setClients(client1, client2)
+ projectAppMonitor.deviceChanged(device1, CHANGE_STATE)
runInEdtAndWait {
assertThat(logcatPresenter.messageBatches).isEmpty()
@@ -146,11 +152,11 @@ class ProjectAppMonitorTest {
@Test
fun deviceChanged_wrongDevice() {
- val projectAppMonitor = projectAppMonitor(logcatPresenter, device, "client1", "client2")
+ val projectAppMonitor = projectAppMonitor(logcatPresenter, "client1", "client2")
val otherDevice = mock<IDevice>()
otherDevice.setClients(client1, client2)
- projectAppMonitor.deviceChanged(device, CHANGE_STATE)
+ projectAppMonitor.deviceChanged(device1, CHANGE_STATE)
runInEdtAndWait {
assertThat(logcatPresenter.messageBatches).isEmpty()
@@ -159,8 +165,9 @@ class ProjectAppMonitorTest {
@Test
fun clientChanged() {
- val projectAppMonitor = projectAppMonitor(logcatPresenter, device, "client1", "client2")
- device.setClients(client1)
+ logcatPresenter.attachedDevice = createDevice(device1.serialNumber)
+ val projectAppMonitor = projectAppMonitor(logcatPresenter, "client1", "client2")
+ device1.setClients(client1)
projectAppMonitor.clientChanged(client1, CHANGE_NAME)
@@ -171,8 +178,8 @@ class ProjectAppMonitorTest {
@Test
fun clientChanged_wrongMask() {
- val projectAppMonitor = projectAppMonitor(logcatPresenter, device, "client1", "client2")
- device.setClients(client1)
+ val projectAppMonitor = projectAppMonitor(logcatPresenter, "client1", "client2")
+ device1.setClients(client1)
projectAppMonitor.clientChanged(client1, CHANGE_DEBUGGER_STATUS)
@@ -183,8 +190,9 @@ class ProjectAppMonitorTest {
@Test
fun clientChanged_wrongDevice() {
- val projectAppMonitor = projectAppMonitor(logcatPresenter, device, "client1", "client2")
- val otherDevice = mock<IDevice>()
+ logcatPresenter.attachedDevice = createDevice("device1")
+ val projectAppMonitor = projectAppMonitor(logcatPresenter, "client1", "client2")
+ val otherDevice = mockDevice("device2")
otherDevice.setClients(client1)
projectAppMonitor.clientChanged(client1, CHANGE_NAME)
@@ -196,8 +204,8 @@ class ProjectAppMonitorTest {
@Test
fun clientChanged_wrongPackage() {
- val projectAppMonitor = projectAppMonitor(logcatPresenter, device, "client2")
- device.setClients(client1)
+ val projectAppMonitor = projectAppMonitor(logcatPresenter, "client2")
+ device1.setClients(client1)
projectAppMonitor.clientChanged(client1, CHANGE_NAME)
@@ -208,9 +216,10 @@ class ProjectAppMonitorTest {
@Test
fun clientChanged_alreadyAdded() {
- val projectAppMonitor = projectAppMonitor(logcatPresenter, device, "client1")
- device.setClients(client1)
- projectAppMonitor.deviceChanged(device, CHANGE_CLIENT_LIST)
+ logcatPresenter.attachedDevice = createDevice(device1.serialNumber)
+ val projectAppMonitor = projectAppMonitor(logcatPresenter, "client1")
+ device1.setClients(client1)
+ projectAppMonitor.deviceChanged(device1, CHANGE_CLIENT_LIST)
projectAppMonitor.clientChanged(client1, CHANGE_NAME)
@@ -221,12 +230,13 @@ class ProjectAppMonitorTest {
@Test
fun clientChanged_thenRemoved() {
- val projectAppMonitor = projectAppMonitor(logcatPresenter, device, "client1")
+ logcatPresenter.attachedDevice = createDevice(device1.serialNumber)
+ val projectAppMonitor = projectAppMonitor(logcatPresenter, "client1")
- device.setClients(client1)
+ device1.setClients(client1)
projectAppMonitor.clientChanged(client1, CHANGE_NAME)
- device.setClients()
- projectAppMonitor.deviceChanged(device, CHANGE_CLIENT_LIST)
+ device1.setClients()
+ projectAppMonitor.deviceChanged(device1, CHANGE_CLIENT_LIST)
runInEdtAndWait {
assertThat(logcatPresenter.messageBatches).containsExactly(
@@ -238,10 +248,11 @@ class ProjectAppMonitorTest {
@Test
fun clientAddedWithoutPackageNameAndThenUpdated() {
- val projectAppMonitor = projectAppMonitor(logcatPresenter, device, "client", "client2")
+ logcatPresenter.attachedDevice = createDevice(device1.serialNumber)
+ val projectAppMonitor = projectAppMonitor(logcatPresenter, "client", "client2")
val client = client(1, "")
- device.setClients(client, client2)
- projectAppMonitor.deviceChanged(device, CHANGE_CLIENT_LIST)
+ device1.setClients(client, client2)
+ projectAppMonitor.deviceChanged(device1, CHANGE_CLIENT_LIST)
whenever(client.clientData.packageName).thenReturn("client")
projectAppMonitor.clientChanged(client, CHANGE_NAME)
@@ -255,6 +266,13 @@ class ProjectAppMonitorTest {
}
}
+private fun mockDevice(serialNumber: String) : IDevice {
+ val device = mock<IDevice>()
+ whenever(device.serialNumber).thenReturn(serialNumber)
+ whenever(device.clients).thenReturn(emptyArray())
+ return device
+}
+
private fun IDevice.setClients(vararg clients: Client) {
whenever(this.clients).thenReturn(clients)
clients.forEach {
@@ -274,5 +292,9 @@ private fun client(pid: Int, packageName: String): Client {
return client
}
-private fun projectAppMonitor(logcatPresenter: LogcatPresenter, device: IDevice, vararg packageNames: String) =
- ProjectAppMonitor(logcatPresenter, FakePackageNamesProvider(*packageNames), device) \ No newline at end of file
+private fun projectAppMonitor(logcatPresenter: LogcatPresenter, vararg packageNames: String) =
+ ProjectAppMonitor(logcatPresenter, FakePackageNamesProvider(*packageNames))
+
+private fun createDevice(serialNumber: String): Device {
+ return Device.createPhysical(serialNumber, true, 11, 30, "Google", "Pixel")
+}