Fix broken CTS tests.
The default focus highlight should be necessary when the view is
attached to window.
Test: cts-tradefed run singleCommand cts --skip-device-info
--skip-preconditions --abi armeabi-v7a -m CtsViewTestCases -t
android.view.cts.ViewTest
Test: cts-tradefed run singleCommand cts --skip-device-info
--skip-preconditions --abi armeabi-v7a -m CtsWidgetTestCases -t
android.widget.cts.MediaControllerTest
Bug: 37082416
Change-Id: I5d6b0c00b5ba03c8f62e4f71be9336823a73134b
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index 546d8f7..9072bf9 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -19712,13 +19712,15 @@
* <li>This view is not in touch mode.</li>
* <li>This view doesn't opt out for a default focus highlight, via
* {@link #setDefaultFocusHighlightEnabled(boolean)}.</li>
+ * <li>This view is attached to window.</li>
* </ul>
* @return {@code true} if a default focus highlight is needed.
*/
private boolean isDefaultFocusHighlightNeeded(Drawable background) {
final boolean hasFocusStateSpecified = background == null || !background.isStateful()
|| !background.hasFocusStateSpecified();
- return !isInTouchMode() && getDefaultFocusHighlightEnabled() && hasFocusStateSpecified;
+ return !isInTouchMode() && getDefaultFocusHighlightEnabled() && hasFocusStateSpecified
+ && isAttachedToWindow();
}
/**