3398da3823
lib/theme/light.dart: dark at 15 lux or less, bright from 60, held 5 s; Settings shows the reading. The Redmi Pad SE refuses apps its only light sensor (Xiaomi's, from the front camera), so there Auto keeps following Android's dark theme. DASH-NEXT.md records the other lux sources. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
74 lines
2.8 KiB
Dart
74 lines
2.8 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:display/theme/light.dart';
|
|
import 'package:display/theme/palette.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
final dispatcher = TestWidgetsFlutterBinding.instance.platformDispatcher;
|
|
tearDown(dispatcher.clearPlatformBrightnessTestValue);
|
|
|
|
test(
|
|
'auto follows Android, the quick switch holds one, and it is kept',
|
|
() async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final prefs = await SharedPreferences.getInstance();
|
|
Themes make() =>
|
|
Themes('t', Palette.fallbackDark, Palette.fallbackBright, prefs);
|
|
final t = make();
|
|
var notified = 0;
|
|
t.addListener(() => notified++);
|
|
|
|
expect(t.mode, VariantMode.auto);
|
|
dispatcher.platformBrightnessTestValue = Brightness.light;
|
|
expect(t.isDark, isFalse);
|
|
expect(notified, 1); // the binding tells its observers
|
|
|
|
t.toggle(); // bright showing: hold dark
|
|
expect((t.mode, t.isDark), (VariantMode.dark, true));
|
|
dispatcher.platformBrightnessTestValue = Brightness.dark;
|
|
dispatcher.platformBrightnessTestValue = Brightness.light;
|
|
expect(notified, 2); // fixed: Android's changes are ignored
|
|
|
|
expect(make().mode, VariantMode.dark); // kept for the next start
|
|
},
|
|
);
|
|
|
|
test('the light switch has a gap and a hold', () {
|
|
final s = LightSwitch();
|
|
final t0 = DateTime(2026);
|
|
DateTime at(int sec) => t0.add(Duration(seconds: sec));
|
|
expect(s.feed(30, at(0)), isNull); // in the gap: no verdict yet
|
|
expect(s.feed(500, at(1)), isFalse); // the first clear reading decides
|
|
expect(s.feed(30, at(2)), isFalse); // the gap keeps it
|
|
expect(s.feed(5, at(3)), isFalse); // dark, but not for long enough
|
|
expect(s.pending, isTrue);
|
|
expect(s.feed(200, at(4)), isFalse); // back: the hold restarts
|
|
expect(s.pending, isFalse);
|
|
s.feed(5, at(10));
|
|
expect(s.feed(5, at(14)), isFalse);
|
|
expect(s.feed(5, at(15)), isTrue);
|
|
});
|
|
|
|
testWidgets('auto takes the light sensor over Android', (tester) async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final prefs = await SharedPreferences.getInstance();
|
|
tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;
|
|
final t = Themes('t', Palette.fallbackDark, Palette.fallbackBright, prefs);
|
|
final levels = StreamController<double>();
|
|
t.followLight(levels.stream);
|
|
expect(t.isDark, isTrue); // no reading yet: Android's
|
|
levels.add(800);
|
|
await tester.pump();
|
|
expect((t.isDark, t.lux.value), (false, 800));
|
|
levels.add(2);
|
|
await tester.pump(
|
|
const Duration(seconds: 6),
|
|
); // no new reading: the timer ends the hold
|
|
expect(t.isDark, isTrue);
|
|
});
|
|
}
|