inherited_test.dart 12.7 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';

8 9
import 'test_widgets.dart';

10
class TestInherited extends InheritedWidget {
11
  const TestInherited({ Key key, Widget child, this.shouldNotify: true })
12 13 14 15 16 17 18 19 20 21
    : super(key: key, child: child);

  final bool shouldNotify;

  @override
  bool updateShouldNotify(InheritedWidget oldWidget) {
    return shouldNotify;
  }
}

22
class ValueInherited extends InheritedWidget {
23
  const ValueInherited({ Key key, Widget child, this.value })
24 25 26 27 28 29 30 31
    : super(key: key, child: child);

  final int value;

  @override
  bool updateShouldNotify(ValueInherited oldWidget) => value != oldWidget.value;
}

32
class ExpectFail extends StatefulWidget {
33
  const ExpectFail(this.onError);
34 35 36 37 38 39 40 41 42 43 44 45 46
  final VoidCallback onError;

  @override
  ExpectFailState createState() => new ExpectFailState();
}

class ExpectFailState extends State<ExpectFail> {
  @override
  void initState() {
    super.initState();
    try {
      context.inheritFromWidgetOfExactType(TestInherited); // should fail
    } catch (e) {
47
      widget.onError();
48 49 50 51 52 53 54
    }
  }

  @override
  Widget build(BuildContext context) => new Container();
}

55
void main() {
56
  testWidgets('Inherited notifies dependents', (WidgetTester tester) async {
57
    final List<TestInherited> log = <TestInherited>[];
58

59
    final Builder builder = new Builder(
60 61 62 63 64
      builder: (BuildContext context) {
        log.add(context.inheritFromWidgetOfExactType(TestInherited));
        return new Container();
      }
    );
65

66
    final TestInherited first = new TestInherited(child: builder);
67
    await tester.pumpWidget(first);
68

69
    expect(log, equals(<TestInherited>[first]));
70

71
    final TestInherited second = new TestInherited(child: builder, shouldNotify: false);
72
    await tester.pumpWidget(second);
73

74
    expect(log, equals(<TestInherited>[first]));
75

76
    final TestInherited third = new TestInherited(child: builder, shouldNotify: true);
77
    await tester.pumpWidget(third);
78

79
    expect(log, equals(<TestInherited>[first, third]));
80 81
  });

82
  testWidgets('Update inherited when reparenting state', (WidgetTester tester) async {
83 84
    final GlobalKey globalKey = new GlobalKey();
    final List<TestInherited> log = <TestInherited>[];
85 86 87 88 89 90 91 92 93 94 95

    TestInherited build() {
      return new TestInherited(
        key: new UniqueKey(),
        child: new Container(
          key: globalKey,
          child: new Builder(
            builder: (BuildContext context) {
              log.add(context.inheritFromWidgetOfExactType(TestInherited));
              return new Container();
            }
96
          )
97 98 99
        )
      );
    }
100

101
    final TestInherited first = build();
102
    await tester.pumpWidget(first);
103

104
    expect(log, equals(<TestInherited>[first]));
105

106
    final TestInherited second = build();
107
    await tester.pumpWidget(second);
108

109
    expect(log, equals(<TestInherited>[first, second]));
110
  });
111

112
  testWidgets('Update inherited when removing node', (WidgetTester tester) async {
113 114
    final List<String> log = <String>[];

115
    await tester.pumpWidget(
116 117 118 119 120 121 122 123 124 125 126 127 128 129
      new Container(
        child: new ValueInherited(
          value: 1,
          child: new Container(
            child: new FlipWidget(
              left: new Container(
                child: new ValueInherited(
                  value: 2,
                  child: new Container(
                    child: new ValueInherited(
                      value: 3,
                      child: new Container(
                        child: new Builder(
                          builder: (BuildContext context) {
130
                            final ValueInherited v = context.inheritFromWidgetOfExactType(ValueInherited);
131
                            log.add('a: ${v.value}');
Ian Hickson's avatar
Ian Hickson committed
132
                            return const Text('', textDirection: TextDirection.ltr);
133 134 135 136 137 138 139 140 141 142 143 144 145 146
                          }
                        )
                      )
                    )
                  )
                )
              ),
              right: new Container(
                child: new ValueInherited(
                  value: 2,
                  child: new Container(
                    child: new Container(
                      child: new Builder(
                        builder: (BuildContext context) {
147
                          final ValueInherited v = context.inheritFromWidgetOfExactType(ValueInherited);
148
                          log.add('b: ${v.value}');
Ian Hickson's avatar
Ian Hickson committed
149
                          return const Text('', textDirection: TextDirection.ltr);
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
                        }
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    );

    expect(log, equals(<String>['a: 3']));
    log.clear();

165
    await tester.pump();
166 167 168 169 170

    expect(log, equals(<String>[]));
    log.clear();

    flipStatefulWidget(tester);
171
    await tester.pump();
172 173 174 175 176

    expect(log, equals(<String>['b: 2']));
    log.clear();

    flipStatefulWidget(tester);
177
    await tester.pump();
178 179 180 181 182

    expect(log, equals(<String>['a: 3']));
    log.clear();
  });

183
  testWidgets('Update inherited when removing node and child has global key', (WidgetTester tester) async {
184 185 186

    final List<String> log = <String>[];

187
    final Key key = new GlobalKey();
188

189
    await tester.pumpWidget(
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
      new Container(
        child: new ValueInherited(
          value: 1,
          child: new Container(
            child: new FlipWidget(
              left: new Container(
                child: new ValueInherited(
                  value: 2,
                  child: new Container(
                    child: new ValueInherited(
                      value: 3,
                      child: new Container(
                        key: key,
                        child: new Builder(
                          builder: (BuildContext context) {
205
                            final ValueInherited v = context.inheritFromWidgetOfExactType(ValueInherited);
206
                            log.add('a: ${v.value}');
Ian Hickson's avatar
Ian Hickson committed
207
                            return const Text('', textDirection: TextDirection.ltr);
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
                          }
                        )
                      )
                    )
                  )
                )
              ),
              right: new Container(
                child: new ValueInherited(
                  value: 2,
                  child: new Container(
                    child: new Container(
                      key: key,
                      child: new Builder(
                        builder: (BuildContext context) {
223
                          final ValueInherited v = context.inheritFromWidgetOfExactType(ValueInherited);
224
                          log.add('b: ${v.value}');
Ian Hickson's avatar
Ian Hickson committed
225
                          return const Text('', textDirection: TextDirection.ltr);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
                        }
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    );

    expect(log, equals(<String>['a: 3']));
    log.clear();

241
    await tester.pump();
242 243 244 245 246

    expect(log, equals(<String>[]));
    log.clear();

    flipStatefulWidget(tester);
247
    await tester.pump();
248 249 250 251 252

    expect(log, equals(<String>['b: 2']));
    log.clear();

    flipStatefulWidget(tester);
253
    await tester.pump();
254 255 256 257 258

    expect(log, equals(<String>['a: 3']));
    log.clear();
  });

259
  testWidgets('Update inherited when removing node and child has global key with constant child', (WidgetTester tester) async {
260 261
    final List<int> log = <int>[];

262
    final Key key = new GlobalKey();
263

264
    final Widget child = new Builder(
265
      builder: (BuildContext context) {
266
        final ValueInherited v = context.inheritFromWidgetOfExactType(ValueInherited);
267
        log.add(v.value);
Ian Hickson's avatar
Ian Hickson committed
268
        return const Text('', textDirection: TextDirection.ltr);
269 270 271
      }
    );

272
    await tester.pumpWidget(
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
      new Container(
        child: new ValueInherited(
          value: 1,
          child: new Container(
            child: new FlipWidget(
              left: new Container(
                child: new ValueInherited(
                  value: 2,
                  child: new Container(
                    child: new ValueInherited(
                      value: 3,
                      child: new Container(
                        key: key,
                        child: child
                      )
                    )
                  )
                )
              ),
              right: new Container(
                child: new ValueInherited(
                  value: 2,
                  child: new Container(
                    child: new Container(
                      key: key,
                      child: child
                    )
                  )
                )
              )
            )
          )
        )
      )
    );

    expect(log, equals(<int>[3]));
    log.clear();

312
    await tester.pump();
313 314 315 316 317

    expect(log, equals(<int>[]));
    log.clear();

    flipStatefulWidget(tester);
318
    await tester.pump();
319 320 321 322 323

    expect(log, equals(<int>[2]));
    log.clear();

    flipStatefulWidget(tester);
324
    await tester.pump();
325 326 327 328 329

    expect(log, equals(<int>[3]));
    log.clear();
  });

330
  testWidgets('Update inherited when removing node and child has global key with constant child, minimised', (WidgetTester tester) async {
331 332 333

    final List<int> log = <int>[];

334
    final Widget child = new Builder(
335 336
      key: new GlobalKey(),
      builder: (BuildContext context) {
337
        final ValueInherited v = context.inheritFromWidgetOfExactType(ValueInherited);
338
        log.add(v.value);
Ian Hickson's avatar
Ian Hickson committed
339
        return const Text('', textDirection: TextDirection.ltr);
340 341 342
      }
    );

343
    await tester.pumpWidget(
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
      new ValueInherited(
        value: 2,
        child: new FlipWidget(
          left: new ValueInherited(
            value: 3,
            child: child
          ),
          right: child
        )
      )
    );

    expect(log, equals(<int>[3]));
    log.clear();

359
    await tester.pump();
360 361 362 363 364

    expect(log, equals(<int>[]));
    log.clear();

    flipStatefulWidget(tester);
365
    await tester.pump();
366 367 368 369 370

    expect(log, equals(<int>[2]));
    log.clear();

    flipStatefulWidget(tester);
371
    await tester.pump();
372 373 374 375 376

    expect(log, equals(<int>[3]));
    log.clear();
  });

377
  testWidgets('Inherited widget notifies descendants when descendant previously failed to find a match', (WidgetTester tester) async {
378 379 380 381 382 383
    int inheritedValue = -1;

    final Widget inner = new Container(
      key: new GlobalKey(),
      child: new Builder(
        builder: (BuildContext context) {
384
          final ValueInherited widget = context.inheritFromWidgetOfExactType(ValueInherited);
385 386 387 388 389 390
          inheritedValue = widget?.value;
          return new Container();
        }
      )
    );

391
    await tester.pumpWidget(
392 393 394 395 396
      inner
    );
    expect(inheritedValue, isNull);

    inheritedValue = -2;
397
    await tester.pumpWidget(
398 399 400 401 402 403 404 405
      new ValueInherited(
        value: 3,
        child: inner
      )
    );
    expect(inheritedValue, equals(3));
  });

406
  testWidgets('Inherited widget doesn\'t notify descendants when descendant did not previously fail to find a match and had no dependencies', (WidgetTester tester) async {
407 408 409 410 411 412 413 414 415 416 417 418
    int buildCount = 0;

    final Widget inner = new Container(
      key: new GlobalKey(),
      child: new Builder(
        builder: (BuildContext context) {
          buildCount += 1;
          return new Container();
        }
      )
    );

419
    await tester.pumpWidget(
420 421 422 423
      inner
    );
    expect(buildCount, equals(1));

424
    await tester.pumpWidget(
425 426 427 428 429 430 431 432
      new ValueInherited(
        value: 3,
        child: inner
      )
    );
    expect(buildCount, equals(1));
  });

433
  testWidgets('Inherited widget does notify descendants when descendant did not previously fail to find a match but did have other dependencies', (WidgetTester tester) async {
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    int buildCount = 0;

    final Widget inner = new Container(
      key: new GlobalKey(),
      child: new TestInherited(
        shouldNotify: false,
        child: new Builder(
          builder: (BuildContext context) {
            context.inheritFromWidgetOfExactType(TestInherited);
            buildCount += 1;
            return new Container();
          }
        )
      )
    );

450
    await tester.pumpWidget(
451 452 453 454
      inner
    );
    expect(buildCount, equals(1));

455
    await tester.pumpWidget(
456 457 458 459 460 461 462
      new ValueInherited(
        value: 3,
        child: inner
      )
    );
    expect(buildCount, equals(2));
  });
463 464 465 466 467

  testWidgets('initState() dependency on Inherited asserts', (WidgetTester tester) async {
    // This is a regression test for https://github.com/flutter/flutter/issues/5491
    bool exceptionCaught = false;

468
    final TestInherited parent = new TestInherited(child: new ExpectFail(() {
469 470 471 472 473 474
      exceptionCaught = true;
    }));
    await tester.pumpWidget(parent);

    expect(exceptionCaught, isTrue);
  });
475
}