diff options
author | Farruh Habibullaev <farruhh@google.com> | 2021-10-27 17:04:51 -0700 |
---|---|---|
committer | Farruh Habibullaev <farruhh@google.com> | 2021-11-04 12:36:54 -0700 |
commit | 0e46b89bef1b13c2bfc7ccf9c0bbaa4f6c002bed (patch) | |
tree | 0aff892dc837d5127f4592a291aa0fafaa7cf03f | |
parent | fe4fc7c6fc606b4de29717dbd0ad2b5e19bd9988 (diff) | |
download | setupwizard-0e46b89bef1b13c2bfc7ccf9c0bbaa4f6c002bed.tar.gz |
Add a better fragment support in BaseSetupWizardActivity
- by adding the frame layout for fragment rendering in
split-nav layouts
- by adding necessary unit tests.
Test: added necessary unit tests.
BUG: 204211185
Change-Id: I27f234d6db1c280df56bcb06a180318b60c77454
-rw-r--r-- | library/main/src/com/android/car/setupwizardlib/BaseSetupWizardActivity.java | 27 | ||||
-rw-r--r-- | library/main/tests/robotests/src/com/android/car/setupwizardlib/BaseCompatActivityTest.java | 58 |
2 files changed, 77 insertions, 8 deletions
diff --git a/library/main/src/com/android/car/setupwizardlib/BaseSetupWizardActivity.java b/library/main/src/com/android/car/setupwizardlib/BaseSetupWizardActivity.java index 1addd71..f8d9342 100644 --- a/library/main/src/com/android/car/setupwizardlib/BaseSetupWizardActivity.java +++ b/library/main/src/com/android/car/setupwizardlib/BaseSetupWizardActivity.java @@ -176,6 +176,7 @@ abstract class BaseSetupWizardActivity extends FragmentActivity { @CallSuper protected void setContentFragmentWithBackstack(Fragment fragment) { if (mAllowFragmentCommits) { + inflateEmptyFragmentFrameLayout(); getSupportFragmentManager().beginTransaction() .replace(getFragmentContainerViewId(), fragment, CONTENT_FRAGMENT_TAG) .addToBackStack(null) @@ -199,6 +200,7 @@ abstract class BaseSetupWizardActivity extends FragmentActivity { @CallSuper protected void setContentFragment(Fragment fragment) { if (mAllowFragmentCommits) { + inflateEmptyFragmentFrameLayout(); getSupportFragmentManager().beginTransaction() .setCustomAnimations( android.R.animator.fade_in, @@ -539,17 +541,26 @@ abstract class BaseSetupWizardActivity extends FragmentActivity { @VisibleForTesting int getFragmentContainerViewId() { - if (isSplitNavLayoutSupported()) { - ViewStub viewStub = findViewById(R.id.layout_content_stub); + // Check if the inflated frame layout is still available. Add the fragment to frame + // layout only if it's available. Otherwise, fall back to default layout to attach + // fragment. Note that frame layout might not be available if hosting activity inflated + // viewStub already with other views than before calling setFragmentContent(). + View frameLayout = findViewById(R.id.empty_fragment_frame_layout); + int containerViewId = frameLayout == null ? R.id.car_setup_wizard_layout : + R.id.empty_fragment_frame_layout; + Log.v(TAG, "fragmentContainerViewId: " + + getResources().getResourceEntryName(containerViewId)); + return containerViewId; + + } + + @VisibleForTesting + void inflateEmptyFragmentFrameLayout() { + ViewStub viewStub = findViewById(R.id.layout_content_stub); + if (viewStub != null) { viewStub.setLayoutResource(R.layout.empty_fragment_frame_layout); viewStub.inflate(); - Log.v(TAG, "fragmentContainerViewId: " - + getResources().getResourceEntryName(R.id.empty_fragment_frame_layout)); - return R.id.empty_fragment_frame_layout; } - Log.v(TAG, "fragmentContainerViewId: " - + getResources().getResourceEntryName(R.id.car_setup_wizard_layout)); - return R.id.car_setup_wizard_layout; } @VisibleForTesting diff --git a/library/main/tests/robotests/src/com/android/car/setupwizardlib/BaseCompatActivityTest.java b/library/main/tests/robotests/src/com/android/car/setupwizardlib/BaseCompatActivityTest.java index 5cb9ef0..b504625 100644 --- a/library/main/tests/robotests/src/com/android/car/setupwizardlib/BaseCompatActivityTest.java +++ b/library/main/tests/robotests/src/com/android/car/setupwizardlib/BaseCompatActivityTest.java @@ -34,6 +34,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewStub; import android.widget.Button; +import android.widget.FrameLayout; import android.widget.ImageView; import androidx.annotation.StyleRes; @@ -626,4 +627,61 @@ public class BaseCompatActivityTest extends BaseRobolectricTest { spyBaseCompatActivity.nextAction(Activity.RESULT_OK); verify(spyBaseCompatActivity, times(2)).startActivity(Mockito.any()); } + + /** + * Test that {@link BaseCompatActivity#getFragmentContainerViewId()} returns + * layout_content_fragment when split-nav is enabled and view stub is not inflated yet. + */ + @Test + public void testGetFragmentContainerViewId_viewStubNotInflated() { + BaseCompatActivity spyBaseCompatActivity = createSpyBaseCompatActivity(); + FrameLayout mockFrameLayout = mock(FrameLayout.class); + ViewStub mockViewStub = mock(ViewStub.class); + when(spyBaseCompatActivity.findViewById(R.id.empty_fragment_frame_layout)) + .thenReturn(mockFrameLayout); + when(spyBaseCompatActivity.findViewById(R.id.layout_content_stub)).thenReturn(mockViewStub); + + int fragmentContainerViewId = spyBaseCompatActivity.getFragmentContainerViewId(); + + verify(mockViewStub, times(1)).inflate(); + assertThat(fragmentContainerViewId).isEqualTo(R.id.empty_fragment_frame_layout); + } + + /** + * Test that {@link BaseCompatActivity#getFragmentContainerViewId()} returns + * layout_content_fragment when split-nav is enabled and view stub is already inflated + * with frame layout for fragment attachment. + */ + @Test + public void testGetFragmentContainerViewId_viewStubInflatedFrameLayout() { + BaseCompatActivity spyBaseCompatActivity = createSpyBaseCompatActivity(); + FrameLayout mockFrameLayout = mock(FrameLayout.class); + when(spyBaseCompatActivity.findViewById(R.id.empty_fragment_frame_layout)) + .thenReturn(mockFrameLayout); + when(spyBaseCompatActivity.findViewById(R.id.layout_content_stub)) + .thenReturn(null); + + int fragmentContainerViewId = spyBaseCompatActivity.getFragmentContainerViewId(); + + assertThat(fragmentContainerViewId).isEqualTo(R.id.empty_fragment_frame_layout); + } + + /** + * Test that {@link BaseCompatActivity#getFragmentContainerViewId()} returns + * layout_content_fragment when split-nav is enabled and view stub is already inflated + * with some other view than fragment layout. The frame layout is not available for fragment + * attachment. + */ + @Test + public void testGetFragmentContainerViewId_frameLayoutNotAvailable() { + BaseCompatActivity spyBaseCompatActivity = createSpyBaseCompatActivity(); + when(spyBaseCompatActivity.findViewById(R.id.layout_content_stub)) + .thenReturn(null); + when(spyBaseCompatActivity.findViewById(R.id.empty_fragment_frame_layout)) + .thenReturn(null); + + int fragmentContainerViewId = spyBaseCompatActivity.getFragmentContainerViewId(); + + assertThat(fragmentContainerViewId).isEqualTo(R.id.car_setup_wizard_layout); + } } |