Fix visibility on Elements - make sure the highest-level view of the Element has the correct visibility set.

PiperOrigin-RevId: 269632451
Change-Id: I1c32ebd96f84394cd27a12dd521c6ebb383e2000
diff --git a/src/main/java/com/google/android/libraries/feed/piet/ElementAdapter.java b/src/main/java/com/google/android/libraries/feed/piet/ElementAdapter.java
index 12ffd23..18ba076 100644
--- a/src/main/java/com/google/android/libraries/feed/piet/ElementAdapter.java
+++ b/src/main/java/com/google/android/libraries/feed/piet/ElementAdapter.java
@@ -355,7 +355,7 @@
     activeVisibilityActions.addAll(actions.getOnHideActionsList());
   }
 
-  private void initializeOverflow(Element baseElement) {
+  private static void initializeOverflow(Element baseElement) {
     switch (baseElement.getHorizontalOverflow()) {
       case OVERFLOW_HIDDEN:
       case OVERFLOW_UNSPECIFIED:
@@ -557,6 +557,13 @@
   }
 
   void setVisibilityOnView(Visibility visibility) {
+    setVisibilityOnView(visibility, getView());
+    if (getView() != getBaseView()) {
+      setVisibilityOnView(visibility, getBaseView());
+    }
+  }
+
+  private static void setVisibilityOnView(Visibility visibility, View view) {
     switch (visibility) {
       case INVISIBLE:
         view.setVisibility(View.INVISIBLE);
@@ -568,10 +575,6 @@
       case VISIBLE:
         view.setVisibility(View.VISIBLE);
         break;
-      default:
-        throw new PietFatalException(
-            ErrorCode.ERR_MISSING_OR_UNHANDLED_CONTENT,
-            String.format("Unhandled visibility: %s", visibility));
     }
   }
 
diff --git a/src/test/java/com/google/android/libraries/feed/piet/ElementAdapterTest.java b/src/test/java/com/google/android/libraries/feed/piet/ElementAdapterTest.java
index 54b158b..e3c77b3 100644
--- a/src/test/java/com/google/android/libraries/feed/piet/ElementAdapterTest.java
+++ b/src/test/java/com/google/android/libraries/feed/piet/ElementAdapterTest.java
@@ -1108,6 +1108,14 @@
     adapter.bindModel(element, frameContext);
 
     assertThat(adapter.getView()).isSameInstanceAs(roundedCornerWrapperView);
+    assertThat(adapter.getView().getVisibility()).isEqualTo(View.VISIBLE);
+
+    // Try setting visibility back to GONE and ensure that RCWV is GONE
+    when(frameContext.getVisibilityFromBinding(visibilityBinding)).thenReturn(Visibility.GONE);
+    adapter.unbindModel();
+    adapter.bindModel(element, frameContext);
+    assertThat(adapter.getView()).isSameInstanceAs(roundedCornerWrapperView);
+    assertThat(adapter.getView().getVisibility()).isEqualTo(View.GONE);
   }
 
   @Test