website icon indicating copy to clipboard operation
website copied to clipboard

Add an example to test Landscape orientation in a widget test

Open zoechi opened this issue 7 years ago • 3 comments

@eseidelGoogle commented on Thu May 25 2017

@sethladd asked me how difficult it was to test landscape layouts in tests. I suspect it's straightforward to do, but I'm not aware of an example to give.


@Hixie commented on Thu May 25 2017

In practice all tests are landscape. :-)


@eseidelGoogle commented on Thu May 25 2017

It seems like we will want to explain to individuals in flutter.io/testing how to test landscape vs. portrait for their widgets (as well as layouts at different sizes). For our own widgets, coverage will tell us if we're testing the landscape vs. portrait configurations of course. e.g. https://coveralls.io/builds/11679479/source?filename=lib%2Fsrc%2Fmaterial%2Fdate_picker.dart#L101 (I'm not actually sure if that's telling me the portrait code is covered or not).


@Hixie commented on Thu May 25 2017

Indeed. Where's a good place to put this information? API docs? Where?


@eseidelGoogle commented on Thu May 25 2017

Presumably https://flutter.io/testing?


@zoechi commented on Wed Jun 27 2018

Related SO question https://stackoverflow.com/questions/50158364/in-flutter-widget-testing-how-to-make-media-orientation-to-portrait


@zoechi commented on Tue Dec 11 2018

Probably a better way (not tested, wasn't aware of that before) https://stackoverflow.com/questions/53706569/how-to-test-flutter-widgets-on-different-screen-sizes

zoechi avatar Dec 11 '18 11:12 zoechi

We have this, but could still use the info in the testing doc.

sfshaza2 avatar Jan 29 '19 21:01 sfshaza2

Orientation in testing is entirely different. See for example the last 2 links in the initial comment. There is no easy to find way how to specify screen dimension/orientation for tests.

zoechi avatar Jan 30 '19 08:01 zoechi

There is no easy to find way how to specify screen dimension/orientation for tests.

This works just fine for me:

const double PORTRAIT_WIDTH = 400.0;
const double PORTRAIT_HEIGHT = 800.0;
const double LANDSCAPE_WIDTH = PORTRAIT_HEIGHT;
const double LANDSCAPE_HEIGHT = PORTRAIT_WIDTH;

final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();

await binding.setSurfaceSize(Size(PORTRAIT_WIDTH, PORTRAIT_HEIGHT));
await tester.pumpWidget(MyWidget());

// test in portrait

await binding.setSurfaceSize(Size(LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT));
await tester.pumpAndSettle();

// OrientationBuilder gets triggered

// test in landscape

eggenstein avatar Mar 27 '19 14:03 eggenstein