inherited_test.dart 12.4 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 11 12 13 14 15 16 17 18 19 20 21
class TestInherited extends InheritedWidget {
  TestInherited({ Key key, Widget child, this.shouldNotify: true })
    : super(key: key, child: child);

  final bool shouldNotify;

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

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

  final int value;

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

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
class ExpectFail extends StatefulWidget {
  ExpectFail(this.onError);
  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) {
      config.onError();
    }
  }

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

55
void main() {
56
  testWidgets('Inherited notifies dependents', (WidgetTester tester) async {
57 58 59 60 61 62 63 64
    List<TestInherited> log = <TestInherited>[];

    Builder builder = new Builder(
      builder: (BuildContext context) {
        log.add(context.inheritFromWidgetOfExactType(TestInherited));
        return new Container();
      }
    );
65

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

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

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

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

76
    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 85 86 87 88 89 90 91 92 93 94 95
    GlobalKey globalKey = new GlobalKey();
    List<TestInherited> log = <TestInherited>[];

    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
    TestInherited first = build();
102
    await tester.pumpWidget(first);
103

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

106
    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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
      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) {
                            ValueInherited v = context.inheritFromWidgetOfExactType(ValueInherited);
                            log.add('a: ${v.value}');
                            return new Text('');
                          }
                        )
                      )
                    )
                  )
                )
              ),
              right: new Container(
                child: new ValueInherited(
                  value: 2,
                  child: new Container(
                    child: new Container(
                      child: new Builder(
                        builder: (BuildContext context) {
                          ValueInherited v = context.inheritFromWidgetOfExactType(ValueInherited);
                          log.add('b: ${v.value}');
                          return new Text('');
                        }
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    );

    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 187 188

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

    Key key = new GlobalKey();

189
    await tester.pumpWidget(
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
      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) {
                            ValueInherited v = context.inheritFromWidgetOfExactType(ValueInherited);
                            log.add('a: ${v.value}');
                            return new Text('');
                          }
                        )
                      )
                    )
                  )
                )
              ),
              right: new Container(
                child: new ValueInherited(
                  value: 2,
                  child: new Container(
                    child: new Container(
                      key: key,
                      child: new Builder(
                        builder: (BuildContext context) {
                          ValueInherited v = context.inheritFromWidgetOfExactType(ValueInherited);
                          log.add('b: ${v.value}');
                          return new Text('');
                        }
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    );

    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 262 263 264 265 266 267 268 269 270 271
    final List<int> log = <int>[];

    Key key = new GlobalKey();

    Widget child = new Builder(
      builder: (BuildContext context) {
        ValueInherited v = context.inheritFromWidgetOfExactType(ValueInherited);
        log.add(v.value);
        return new Text('');
      }
    );

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 334 335 336 337 338 339 340 341 342

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

    Widget child = new Builder(
      key: new GlobalKey(),
      builder: (BuildContext context) {
        ValueInherited v = context.inheritFromWidgetOfExactType(ValueInherited);
        log.add(v.value);
        return new Text('');
      }
    );

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 384 385 386 387 388 389 390
    int inheritedValue = -1;

    final Widget inner = new Container(
      key: new GlobalKey(),
      child: new Builder(
        builder: (BuildContext context) {
          ValueInherited widget = context.inheritFromWidgetOfExactType(ValueInherited);
          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 468 469 470 471 472 473 474

  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;

    TestInherited parent = new TestInherited(child: new ExpectFail(() {
      exceptionCaught = true;
    }));
    await tester.pumpWidget(parent);

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