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

Conversation

@dkwingsmt
Copy link
Contributor

@dkwingsmt dkwingsmt commented May 26, 2023

This PR refactors ShellTest::CreateShell so that instead of taking a list of optional parameters, it takes a Config struct, almost all fields of which have default values. This makes calling this method cleaner, and makes it easier to add more parameters. A short version is also kept that takes settings and task_runners, since this version is used so often.

Currently ShellTest::CreateShell's full version has 2 mandatory parameters and 5 optional parameters. Many calls to this method have to list a long list of default parameters before going to the single one that needs overwriting, and adding new parameters to this call is even more tedious.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide and the C++, Objective-C, Java style guides.
  • I listed at least one issue that this PR fixes in the description above.
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See testing the engine for instructions on writing and running engine tests.
  • I updated/added relevant documentation (doc comments with ///).
  • I signed the CLA.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Copy link
Contributor

@dnfield dnfield left a comment

Choose a reason for hiding this comment

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

I like this idea, the config object could be cleaned up a bit.

If assigning defaults needs more than simple logic (like perhaps the default for task_runners), consider using a builder pattern.

// Required.
const Settings& settings;
// Defaults to &GetTaskRunnersForFixture().
const TaskRunners* task_runners;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please don't use a raw pointer here.

Just keep this as a value - avoids one more semantic change in the way these tests work, and avoids ambiguity around whether this can be nullptr and what that means.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also in general here: instead of specifying defaults in a comment, initialize the values here with a default.

@zanderso
Copy link
Member

zanderso commented Jun 8, 2023

From PR review triage: Converting to a draft.

@zanderso zanderso marked this pull request as draft June 8, 2023 20:09
@flutter-dashboard
Copy link

This pull request has been changed to a draft. The currently pending flutter-gold status will not be able to resolve until a new commit is pushed or the change is marked ready for review again.

// Required.
const Settings& settings;
// Defaults to GetTaskRunnersForFixture().
std::optional<TaskRunners> task_runners = {};
Copy link
Contributor

Choose a reason for hiding this comment

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

This is where I think the builder could be helpful - you don't want the public interface here to actually be optional.

But if you don't want to use a builder I think we should make the std::optional be on the constructor for this struct and make the member non-optional - because otherwise callers will always have to check (and get linted against not checking now) whether the optional has a value when it really always does.

Copy link
Contributor Author

@dkwingsmt dkwingsmt Jun 9, 2023

Choose a reason for hiding this comment

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

Not that I don't want to use a builder, but here's the idea: I can create a TaskRunnersBuilder, which has two ctors:

// implicit conversion, so that existing tests work without modification
TaskRunnersBuilder(TaskRunner);
// Defaults to GetTaskRunnersForFixture()
TaskRunnersBuilder();

But I think it's really just reinventing the wheel for std::optional because it's not semantically obvious what the default behavior is (and the doc is now hidden even deeper).

And what's worse, because this field is a value, not a pointer of a function, I can't really specify an "empty" value if not for the default ctor.

As for

because otherwise callers will always have to check (and get linted against not checking now) whether the optional has a value when it really always does

I don't think this is a problem, because this struct really is only used by ShellTest::CreateShell.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another option is to mark task_runners as mandatory so that you always explicit write GetTaskRunnersForFixture() for every call.

@dkwingsmt
Copy link
Contributor Author

Also I created a builder for PlatformView. Let me know what you think!

ShellTestPlatformViewBuilder::ShellTestPlatformViewBuilder(Config config)
: config_(config) {}

std::unique_ptr<PlatformView> ShellTestPlatformViewBuilder::operator()(
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: avoid defining a custom operator for this, just make a Build method.

Copy link
Contributor Author

@dkwingsmt dkwingsmt Jun 11, 2023

Choose a reason for hiding this comment

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

The custom operator is to make this class assignable to the current std::function parameter. We can turn this into a Build method instead, but then the several cases that currently define a custom closure will have to define a new class that takes a closure. Should I do it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  Shell::CreateCallback<PlatformView> platform_view_create_callback =
      [task_runners, main_context](flutter::Shell& shell) {
        auto result = std::make_unique<TestPlatformView>(shell, task_runners);
        ON_CALL(*result, CreateRenderingSurface())
            .WillByDefault(::testing::Invoke([main_context] {
              auto surface = std::make_unique<MockSurface>();
              ON_CALL(*surface, GetContext())
                  .WillByDefault(Return(main_context.get()));
              ON_CALL(*surface, IsValid()).WillByDefault(Return(true));
              ON_CALL(*surface, MakeRenderContextCurrent())
                  .WillByDefault(::testing::Invoke([] {
                    return std::make_unique<GLContextDefaultResult>(true);
                  }));
              return surface;
            }));
        return result;
      };

  auto shell = CreateShell({
      .settings = settings,
      .task_runners = task_runners,
      .platform_view_create_callback = platform_view_create_callback,
  });

Shell::CreateCallback<PlatformView> platform_view_create_callback =

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fine then.

Copy link
Contributor

@dnfield dnfield left a comment

Choose a reason for hiding this comment

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

LGTM with nit

@dkwingsmt dkwingsmt marked this pull request as ready for review June 11, 2023 07:58
@dkwingsmt dkwingsmt added the autosubmit Merge PR when tree becomes green via auto submit App label Jun 11, 2023
@auto-submit
Copy link
Contributor

auto-submit bot commented Jun 11, 2023

auto label is removed for flutter/engine, pr: 42332, due to - The status or check suite Linux Host clang-tidy has failed. Please fix the issues identified (or deflake) before re-applying this label.

@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jun 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants