public static Object getMountContent(ComponentContext c, Component component) { return ComponentsPools.acquireMountContent(c.getAndroidContext(), component); } }
@Override protected void onMeasure( ComponentContext c, ComponentLayout layout, int widthSpec, int heightSpec, Size size) { final V toMeasure = (V) ComponentsPools.acquireMountContent(c.getAndroidContext(), this); final ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(size.width, size.height); toMeasure.setLayoutParams(layoutParams); mViewBinder.bind(toMeasure); if (toMeasure.getVisibility() == View.GONE) { // No need to measure the view if binding it caused its visibility to become GONE. size.width = 0; size.height = 0; } else { toMeasure.measure(widthSpec, heightSpec); size.width = toMeasure.getMeasuredWidth(); size.height = toMeasure.getMeasuredHeight(); } mViewBinder.unbind(toMeasure); ComponentsPools.release(c.getAndroidContext(), this, toMeasure); }
@Test public void testAllocationsCountTowardsPreallocationLimit() { for (int i = 0; i < POOL_SIZE - 1; i++) { maybePreallocateContent(mContext1, mLifecycle); acquireMountContent(mContext1, mLifecycle); } acquireMountContent(mContext1, mLifecycle); // Allocation limit should be hit now, so we shouldn't preallocate anything maybePreallocateContent(mContext1, mLifecycle); mNewMountContent = new View(mContext1); assertThat(acquireMountContent(mContext1, mLifecycle)).isSameAs(mNewMountContent); }
@Test public void testDoNotPreallocateContentBeyondPoolSize() { for (int i = 0; i < POOL_SIZE; i++) { maybePreallocateContent(mContext1, mLifecycle); acquireMountContent(mContext1, mLifecycle); } maybePreallocateContent(mContext1, mLifecycle); mNewMountContent = new View(mContext1); assertThat(acquireMountContent(mContext1, mLifecycle)).isSameAs(mNewMountContent); }
@Test public void testPreallocateContent() { assertThat(acquireMountContent(mContext1, mLifecycle)).isSameAs(mNewMountContent); maybePreallocateContent(mContext1, mLifecycle); // Change the content that's returned when we create new mount content to make sure we're // getting the one from preallocating above. mNewMountContent = new View(mContext1); assertThat(acquireMountContent(mContext1, mLifecycle)).isNotSameAs(mNewMountContent); }
@Test public void testPreallocateWithEmptyPoolSize() { maybePreallocateContent(mContext1, mLifecycleWithEmptyPoolSize); mNewMountContent = new View(mContext1); assertThat(acquireMountContent(mContext1, mLifecycleWithEmptyPoolSize)) .isSameAs(mNewMountContent); } }
@Test public void testAcquireMountContentWithSameContext() { assertThat(acquireMountContent(mContext1, mLifecycle)).isSameAs(mNewMountContent); release(mContext1, mLifecycle, mMountContent); assertThat(mMountContent).isSameAs(acquireMountContent(mContext1, mLifecycle)); }
@Test public void testAcquireMountContentWithSameRootContext() { assertThat(acquireMountContent(mContext1, mLifecycle)).isSameAs(mNewMountContent); release(mContext1, mLifecycle, mMountContent); assertThat(acquireMountContent(mContext2, mLifecycle)).isSameAs(mNewMountContent); }
@Test public void testReleaseMountContentForDestroyedContextDoesNothing() { // Assert pooling was working before assertThat(acquireMountContent(mActivity, mLifecycle)).isSameAs(mNewMountContent); release(mActivity, mLifecycle, mMountContent); assertThat(mMountContent).isSameAs(acquireMountContent(mActivity, mLifecycle)); // Now destroy it and assert pooling no longer works mActivityController.destroy(); release(mActivity, mLifecycle, mMountContent); assertThat(acquireMountContent(mActivity, mLifecycle)).isSameAs(mNewMountContent); }
@Test public void testReleaseAndAcquireWithNoPoolSize() { release(mContext1, mLifecycleWithEmptyPoolSize, mMountContent); assertThat(acquireMountContent(mContext1, mLifecycleWithEmptyPoolSize)) .isSameAs(mNewMountContent); }
ComponentsPools.acquireMountContent(mContext.getAndroidContext(), component);
@Test public void testDestroyingActivityDoesNotAffectPoolingOfOtherContexts() { mActivityController.destroy(); ComponentsPools.onContextDestroyed(mActivity); release(mContext1, mLifecycle, mMountContent); assertThat(acquireMountContent(mContext1, mLifecycle)).isSameAs(mMountContent); }