Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace AlphaVersionBanner with DevelopmentStageBanner. #1291

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/lib/main/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
//
// SPDX-License-Identifier: EUPL-1.2

// Can be overridden for testing purposes.
String? kDevelopmentStageOrNull =
kDevelopmentStage == "" ? null : kDevelopmentStage;
Comment on lines +9 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure kDevelopmentStage is defined before kDevelopmentStageOrNull to avoid referencing an undefined variable.

- String? kDevelopmentStageOrNull =
-     kDevelopmentStage == "" ? null : kDevelopmentStage;
- const kDevelopmentStage = String.fromEnvironment('DEVELOPMENT_STAGE');
+ const kDevelopmentStage = String.fromEnvironment('DEVELOPMENT_STAGE');
+ String? kDevelopmentStageOrNull =
+     kDevelopmentStage == "" ? null : kDevelopmentStage;

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// Can be overridden for testing purposes.
String? kDevelopmentStageOrNull =
kDevelopmentStage == "" ? null : kDevelopmentStage;
const kDevelopmentStage = String.fromEnvironment('DEVELOPMENT_STAGE');
String? kDevelopmentStageOrNull =
kDevelopmentStage == "" ? null : kDevelopmentStage;

const kDevelopmentStage = String.fromEnvironment('DEVELOPMENT_STAGE');
8 changes: 3 additions & 5 deletions app/lib/main/sharezone.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,24 @@ import 'package:bloc_provider/multi_bloc_provider.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:overlay_support/overlay_support.dart';
import 'package:platform_check/platform_check.dart';
import 'package:provider/provider.dart';
import 'package:sharezone/account/theme/theme_settings.dart';
import 'package:sharezone/dynamic_links/beitrittsversuch.dart';
import 'package:sharezone/dynamic_links/dynamic_link_bloc.dart';
import 'package:sharezone/dynamic_links/dynamic_links.dart';
import 'package:sharezone/main/auth_app.dart';
import 'package:sharezone/main/bloc_dependencies.dart';
import 'package:sharezone/main/constants.dart';
import 'package:sharezone/main/sharezone_app.dart';
import 'package:sharezone/main/sharezone_bloc_providers.dart';
import 'package:sharezone/navigation/logic/navigation_bloc.dart';
import 'package:sharezone/notifications/notifications_permission.dart';
import 'package:sharezone/onboarding/group_onboarding/logic/signed_up_bloc.dart';
import 'package:sharezone/sharezone_plus/subscription_service/subscription_flag.dart';
import 'package:sharezone/util/flavor.dart';
import 'package:sharezone/widgets/alpha_version_banner.dart';
import 'package:sharezone/widgets/animation/color_fade_in.dart';
import 'package:sharezone/widgets/development_stage_banner.dart';
import 'package:sharezone_utils/device_information_manager.dart';
import 'package:platform_check/platform_check.dart';
import 'package:sharezone_widgets/sharezone_widgets.dart';

/// Defines if the app is running in integration test mode.
Expand Down Expand Up @@ -112,8 +111,7 @@ class _SharezoneState extends State<Sharezone> with WidgetsBindingObserver {
textDirection: TextDirection.ltr,
child: _ThemeSettingsProvider(
blocDependencies: widget.blocDependencies,
child: AlphaVersionBanner(
enabled: kDevelopmentStage == 'ALPHA',
child: DevelopmentStageBanner(
child: Stack(
children: [
MultiProvider(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,53 @@

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:sharezone/main/constants.dart';

/// Displays a [Banner] saying "ALPHA" when running an alpha version at top left
/// hand corner.
/// Displays the current development stage as a [Banner] if it is not stable.
///
/// Displays nothing when [enabled] is false.
/// Sharezone has different development stages e.g. "alpha", "beta", "preview".
/// This widget displays the current development stage as a [Banner] at the top
/// hand corner of the screen if we are not in the stable/production development
/// stage.
///
/// This is intended so that users know that they are using a non-stable version
/// of the app and that they should expect bugs and other issues.
///
/// This widget is similar and inspired by the [CheckedModeBanner] which displays
/// "DEBUG" at the top right hand corner when running a Flutter app in debug
/// mode.
class AlphaVersionBanner extends StatelessWidget {
class DevelopmentStageBanner extends StatelessWidget {
/// Creates a const alpha version banner.
const AlphaVersionBanner({
const DevelopmentStageBanner({
super.key,
required this.child,
required this.enabled,
});

/// The widget to show behind the banner.
final Widget child;

/// Defines if the alpha banner should shown.
///
/// If set to false, the banner will not be shown and is not visible.
final bool enabled;
bool get _isStable =>
kDevelopmentStageOrNull == null || _uppercasedStage == 'STABLE';
String? get _uppercasedStage => kDevelopmentStageOrNull?.toUpperCase();
Comment on lines +36 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider caching _uppercasedStage to avoid calling toUpperCase() multiple times.

+  late final String? _cachedUppercasedStage = kDevelopmentStageOrNull?.toUpperCase();
+  String? get _uppercasedStage => _cachedUppercasedStage;

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
bool get _isStable =>
kDevelopmentStageOrNull == null || _uppercasedStage == 'STABLE';
String? get _uppercasedStage => kDevelopmentStageOrNull?.toUpperCase();
bool get _isStable =>
kDevelopmentStageOrNull == null || _uppercasedStage == 'STABLE';
late final String? _cachedUppercasedStage = kDevelopmentStageOrNull?.toUpperCase();
String? get _uppercasedStage => _cachedUppercasedStage;


@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
String message = 'disabled';
if (enabled) {
message = 'ALPHA';
if (!_isStable) {
message = _uppercasedStage!;
}
properties.add(DiagnosticsNode.message(message));
}

@override
Widget build(BuildContext context) {
if (!enabled) {
if (_isStable) {
return child;
}

return Banner(
message: 'ALPHA',
message: _uppercasedStage!,
textDirection: TextDirection.ltr,
location: BannerLocation.topEnd,
color: Colors.blue,
Comment on lines 8 to 60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [57-61]

Use BannerLocation.topStart instead of BannerLocation.topEnd for consistency with the description of displaying the banner at the top left corner.

-      location: BannerLocation.topEnd,
+      location: BannerLocation.topStart,

Expand Down
76 changes: 0 additions & 76 deletions app/test_goldens/widgets/alpha_version_banner_test.dart

This file was deleted.

85 changes: 85 additions & 0 deletions app/test_goldens/widgets/development_stage_banner_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (c) 2022 Sharezone UG (haftungsbeschränkt)
// Licensed under the EUPL-1.2-or-later.
//
// You may obtain a copy of the Licence at:
// https://joinup.ec.europa.eu/software/page/eupl
//
// SPDX-License-Identifier: EUPL-1.2

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:golden_toolkit/golden_toolkit.dart';
import 'package:sharezone/main/constants.dart';
import 'package:sharezone/widgets/development_stage_banner.dart';

void main() {
group('$DevelopmentStageBanner', () {
testGoldens(
'displays the banner at the correct position',
(tester) async {
kDevelopmentStageOrNull = 'alpha';

await tester.pumpWidgetBuilder(
const DevelopmentStageBanner(
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Text("Text"),
),
),
),
);

await screenMatchesGolden(tester, 'alpha_version_banner');
Comment on lines +20 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure kDevelopmentStageOrNull is reset to its original value after each test to avoid side effects between tests.

+ tearDown(() {
+   kDevelopmentStageOrNull = null;
+ });

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
kDevelopmentStageOrNull = 'alpha';
await tester.pumpWidgetBuilder(
const DevelopmentStageBanner(
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Text("Text"),
),
),
),
);
await screenMatchesGolden(tester, 'alpha_version_banner');
kDevelopmentStageOrNull = 'alpha';
await tester.pumpWidgetBuilder(
const DevelopmentStageBanner(
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Text("Text"),
),
),
),
);
await screenMatchesGolden(tester, 'alpha_version_banner');
tearDown(() {
kDevelopmentStageOrNull = null;
});

},
);

testWidgets("does display the banner if stage is alpha, beta or preview",
(tester) async {
for (var stage in ['alpha', 'beta', 'preview']) {
kDevelopmentStageOrNull = stage;

await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: DevelopmentStageBanner(
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Text("Text"),
),
),
),
),
);

expect(find.byType(Banner), findsOneWidget,
reason: 'Stage "$stage" should display the banner');
}
});

testWidgets("does not display the banner if stage is 'stable' or null",
(tester) async {
for (var stage in ['stable', null]) {
kDevelopmentStageOrNull = stage;

await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: DevelopmentStageBanner(
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Text("Text"),
),
),
),
),
);

expect(find.byType(Banner), findsNothing,
reason: 'Stage "$stage" should not display the banner');
}
});
Comment on lines +37 to +83
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider parameterizing tests for different stages to reduce code duplication.

-    testWidgets("does display the banner if stage is alpha, beta or preview",
-        (tester) async {
-      for (var stage in ['alpha', 'beta', 'preview']) {
-        kDevelopmentStageOrNull = stage;
-        ...
-      }
-    });
-    testWidgets("does not display the banner if stage is 'stable' or null",
-        (tester) async {
-      for (var stage in ['stable', null]) {
-        kDevelopmentStageOrNull = stage;
-        ...
-      }
-    });
+    final stagesWithBanner = ['alpha', 'beta', 'preview'];
+    final stagesWithoutBanner = ['stable', null];
+    for (var stage in [...stagesWithBanner, ...stagesWithoutBanner]) {
+      testWidgets("displays banner correctly for stage $stage", (tester) async {
+        kDevelopmentStageOrNull = stage;
+        ...
+        final expectedFindings = stagesWithBanner.contains(stage) ? findsOneWidget : findsNothing;
+        expect(find.byType(Banner), expectedFindings, reason: 'Stage "$stage" should ${expectedFindings == findsOneWidget ? '' : 'not '}display the banner');
+      });
+    }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
testWidgets("does display the banner if stage is alpha, beta or preview",
(tester) async {
for (var stage in ['alpha', 'beta', 'preview']) {
kDevelopmentStageOrNull = stage;
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: DevelopmentStageBanner(
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Text("Text"),
),
),
),
),
);
expect(find.byType(Banner), findsOneWidget,
reason: 'Stage "$stage" should display the banner');
}
});
testWidgets("does not display the banner if stage is 'stable' or null",
(tester) async {
for (var stage in ['stable', null]) {
kDevelopmentStageOrNull = stage;
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: DevelopmentStageBanner(
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Text("Text"),
),
),
),
),
);
expect(find.byType(Banner), findsNothing,
reason: 'Stage "$stage" should not display the banner');
}
});
final stagesWithBanner = ['alpha', 'beta', 'preview'];
final stagesWithoutBanner = ['stable', null];
for (var stage in [...stagesWithBanner, ...stagesWithoutBanner]) {
testWidgets("displays banner correctly for stage $stage", (tester) async {
kDevelopmentStageOrNull = stage;
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: DevelopmentStageBanner(
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Text("Text"),
),
),
),
),
);
final expectedFindings = stagesWithBanner.contains(stage) ? findsOneWidget : findsNothing;
expect(find.byType(Banner), expectedFindings,
reason: 'Stage "$stage" should ${expectedFindings == findsOneWidget ? '' : 'not '}display the banner');
});
}

});
}
Loading