Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Conversation

@GaryQian
Copy link
Contributor

Adds registration calls onAttach and onDetach to address flutter/flutter#105346

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@GaryQian
Copy link
Contributor Author

cc @camsim99

Imports

Type

Init fragment with custom engine

Formatting:

tests

Args

Fix test

Clean

Formatting

Formatting

Spy

Format

imports

typo

Typo
@GaryQian GaryQian requested review from dnfield and xster June 23, 2022 09:50
Comment on lines 779 to 781
if (delegate == null) {
delegate = new FlutterActivityAndFragmentDelegate(this);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems strange - maybe we're constructing the test incorrectly?

I'm under the impression that onAttach should never get called twice before onDetach is called in a real case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically here, I am passing in a mock delegate that is being blindly overwritten here. The delegate needs to be mocked as it requires a ton of setup to be able to successfully call delegate.onAttach(). This null check allows us to mock it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead have a delegate factory this class uses, perhaps only visible for testing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to use a factory.

new FlutterEngine(spyCtx, new FlutterLoader(), flutterJNI, null, false);
FlutterEngineCache.getInstance().put("my_cached_engine", flutterEngine);

FlutterFragment fragment = FlutterFragment.withCachedEngine("my_cached_engine").build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. here -

FlutterFragment.withCachedEngine("my_cached_engine").withDelegateFactory(someObjectThatAlwaysReturnsTheMockDelegate).build();

}

// We track the attached context so we can unregister component callbacks when detaching.
private Context attachedContext;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you don't need to do this. I think just getContext is fine, and if the context changes because the fragment gets moved to a different activity or something, onAttach should be called (and get registered to another callback)

@dnfield
Copy link
Contributor

dnfield commented Jun 27, 2022

(My comments should not block landing this, but if they can be addressed it might make future work in this area less confusing.)

@GaryQian GaryQian requested a review from dnfield June 28, 2022 06:01
@VisibleForTesting
/* package */ void setDelegate(@NonNull FlutterActivityAndFragmentDelegate delegate) {
this.delegate = delegate;
/* package */ void setDelegateFactory(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove todo and close issue if not relevant anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue still seems to apply.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue itself is to remove the method you're removing here. We still need some way to test this class and this seems like an appropriate way to have some DI.

@GaryQian GaryQian added waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land. and removed needs tests labels Jun 28, 2022
@fluttergithubbot fluttergithubbot merged commit 8e63563 into flutter:main Jun 28, 2022
@xster
Copy link
Member

xster commented Jul 11, 2022

@dnfield
Copy link
Contributor

dnfield commented Jul 11, 2022

Yes

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

platform-android waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants