inherited_test.dart 11.5 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
void main() {
33
  testWidgets('Inherited notifies dependents', (WidgetTester tester) async {
34 35 36 37 38 39 40 41
    List<TestInherited> log = <TestInherited>[];

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

43
    TestInherited first = new TestInherited(child: builder);
44
    await tester.pumpWidget(first);
45

46
    expect(log, equals(<TestInherited>[first]));
47

48
    TestInherited second = new TestInherited(child: builder, shouldNotify: false);
49
    await tester.pumpWidget(second);
50

51
    expect(log, equals(<TestInherited>[first]));
52

53
    TestInherited third = new TestInherited(child: builder, shouldNotify: true);
54
    await tester.pumpWidget(third);
55

56
    expect(log, equals(<TestInherited>[first, third]));
57 58
  });

59
  testWidgets('Update inherited when reparenting state', (WidgetTester tester) async {
60 61 62 63 64 65 66 67 68 69 70 71 72
    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();
            }
73
          )
74 75 76
        )
      );
    }
77

78
    TestInherited first = build();
79
    await tester.pumpWidget(first);
80

81
    expect(log, equals(<TestInherited>[first]));
82

83
    TestInherited second = build();
84
    await tester.pumpWidget(second);
85

86
    expect(log, equals(<TestInherited>[first, second]));
87
  });
88

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

92
    await tester.pumpWidget(
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 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
      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();

142
    await tester.pump();
143 144 145 146 147

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

    flipStatefulWidget(tester);
148
    await tester.pump();
149 150 151 152 153

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

    flipStatefulWidget(tester);
154
    await tester.pump();
155 156 157 158 159

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

160
  testWidgets('Update inherited when removing node and child has global key', (WidgetTester tester) async {
161 162 163 164 165

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

    Key key = new GlobalKey();

166
    await tester.pumpWidget(
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 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
      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();

218
    await tester.pump();
219 220 221 222 223

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

    flipStatefulWidget(tester);
224
    await tester.pump();
225 226 227 228 229

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

    flipStatefulWidget(tester);
230
    await tester.pump();
231 232 233 234 235

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

236
  testWidgets('Update inherited when removing node and child has global key with constant child', (WidgetTester tester) async {
237 238 239 240 241 242 243 244 245 246 247 248
    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('');
      }
    );

249
    await tester.pumpWidget(
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
      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();

289
    await tester.pump();
290 291 292 293 294

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

    flipStatefulWidget(tester);
295
    await tester.pump();
296 297 298 299 300

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

    flipStatefulWidget(tester);
301
    await tester.pump();
302 303 304 305 306

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

307
  testWidgets('Update inherited when removing node and child has global key with constant child, minimised', (WidgetTester tester) async {
308 309 310 311 312 313 314 315 316 317 318 319

    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('');
      }
    );

320
    await tester.pumpWidget(
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
      new ValueInherited(
        value: 2,
        child: new FlipWidget(
          left: new ValueInherited(
            value: 3,
            child: child
          ),
          right: child
        )
      )
    );

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

336
    await tester.pump();
337 338 339 340 341

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

    flipStatefulWidget(tester);
342
    await tester.pump();
343 344 345 346 347

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

    flipStatefulWidget(tester);
348
    await tester.pump();
349 350 351 352 353

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

354
  testWidgets('Inherited widget notifies descendants when descendant previously failed to find a match', (WidgetTester tester) async {
355 356 357 358 359 360 361 362 363 364 365 366 367
    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();
        }
      )
    );

368
    await tester.pumpWidget(
369 370 371 372 373
      inner
    );
    expect(inheritedValue, isNull);

    inheritedValue = -2;
374
    await tester.pumpWidget(
375 376 377 378 379 380 381 382
      new ValueInherited(
        value: 3,
        child: inner
      )
    );
    expect(inheritedValue, equals(3));
  });

383
  testWidgets('Inherited widget doesn\'t notify descendants when descendant did not previously fail to find a match and had no dependencies', (WidgetTester tester) async {
384 385 386 387 388 389 390 391 392 393 394 395
    int buildCount = 0;

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

396
    await tester.pumpWidget(
397 398 399 400
      inner
    );
    expect(buildCount, equals(1));

401
    await tester.pumpWidget(
402 403 404 405 406 407 408 409
      new ValueInherited(
        value: 3,
        child: inner
      )
    );
    expect(buildCount, equals(1));
  });

410
  testWidgets('Inherited widget does notify descendants when descendant did not previously fail to find a match but did have other dependencies', (WidgetTester tester) async {
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    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();
          }
        )
      )
    );

427
    await tester.pumpWidget(
428 429 430 431
      inner
    );
    expect(buildCount, equals(1));

432
    await tester.pumpWidget(
433 434 435 436 437 438 439
      new ValueInherited(
        value: 3,
        child: inner
      )
    );
    expect(buildCount, equals(2));
  });
440
}