Skip to content

Commit

Permalink
Add tests for launching learn more link
Browse files Browse the repository at this point in the history
Also modifies the existing test for the SubscribeNow page's
launcher to unify the launcher mocks.
  • Loading branch information
ashuntu committed Dec 5, 2024
1 parent 3a8a4d0 commit 3ae1121
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:ubuntupro/pages/landscape_skip/landscape_skip_page.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
import 'package:wizard_router/wizard_router.dart';
import 'package:yaru/yaru.dart';
import 'package:yaru_test/yaru_test.dart';

import '../../utils/build_multiprovider_app.dart';
import '../../utils/url_launcher_mock.dart';

void main() {
final launcher = FakeUrlLauncher();
UrlLauncherPlatform.instance = launcher;

testWidgets('default state', (tester) async {
final app = buildApp();
await tester.pumpWidget(app);
Expand Down Expand Up @@ -81,6 +86,18 @@ void main() {
isTrue,
);
});

testWidgets('launch web page', (tester) async {
final app = buildApp();
await tester.pumpWidget(app);
final context = tester.element(find.byType(LandscapeSkipPage));
final lang = AppLocalizations.of(context);

expect(launcher.launched, isFalse);
await tester.tapOnText(find.textRange.ofSubstring(lang.learnMore));
await tester.pump();
expect(launcher.launched, isTrue);
});
}

Widget buildApp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import 'package:ubuntu_service/ubuntu_service.dart';
import 'package:ubuntupro/core/agent_api_client.dart';
import 'package:ubuntupro/pages/subscribe_now/subscribe_now_model.dart';
import 'package:ubuntupro/pages/subscribe_now/subscribe_now_page.dart';
import 'package:url_launcher_platform_interface/link.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
import 'package:wizard_router/wizard_router.dart';

import '../../utils/build_multiprovider_app.dart';
import '../../utils/url_launcher_mock.dart';
import 'subscribe_now_page_test.mocks.dart';

@GenerateMocks([SubscribeNowModel])
Expand All @@ -36,9 +36,11 @@ void main() {

final app = buildApp(model, onSubscribeNoop);
await tester.pumpWidget(app);
final context = tester.element(find.byType(SubscribeNowPage));
final lang = AppLocalizations.of(context);

expect(launcher.launched, isFalse);
await tester.tapOnText(find.textRange.ofSubstring('Learn more'));
await tester.tapOnText(find.textRange.ofSubstring(lang.learnMore));
await tester.pump();
expect(launcher.launched, isTrue);
});
Expand Down Expand Up @@ -177,49 +179,3 @@ Widget buildApp(
void onSubscribeNoop(SubscriptionInfo _) {}

class FakeAgentApiClient extends Fake implements AgentApiClient {}

class FakeUrlLauncher extends UrlLauncherPlatform {
bool launched = false;

@override
Future<bool> canLaunch(String url) async {
return true;
}

@override
Future<void> closeWebView() async {}

@override
Future<bool> launchUrl(String url, LaunchOptions options) async {
launched = true;
return true;
}

@override
Future<bool> supportsCloseForMode(PreferredLaunchMode mode) async {
return true;
}

@override
Future<bool> supportsMode(PreferredLaunchMode mode) async {
return true;
}

@override
Future<bool> launch(
String url, {
required bool useSafariVC,
required bool useWebView,
required bool enableJavaScript,
required bool enableDomStorage,
required bool universalLinksOnly,
required Map<String, String> headers,
String? webOnlyWindowName,
}) async {
launched = true;
return true;
}

@override
LinkDelegate? get linkDelegate => null;
}
48 changes: 48 additions & 0 deletions gui/packages/ubuntupro/test/utils/url_launcher_mock.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:url_launcher_platform_interface/link.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

class FakeUrlLauncher extends UrlLauncherPlatform {
bool launched = false;

@override
Future<bool> canLaunch(String url) async {
return true;
}

@override
Future<void> closeWebView() async {}

@override
Future<bool> launchUrl(String url, LaunchOptions options) async {
launched = true;
return true;
}

@override
Future<bool> supportsCloseForMode(PreferredLaunchMode mode) async {
return true;
}

@override
Future<bool> supportsMode(PreferredLaunchMode mode) async {
return true;
}

@override
Future<bool> launch(
String url, {
required bool useSafariVC,
required bool useWebView,
required bool enableJavaScript,
required bool enableDomStorage,
required bool universalLinksOnly,
required Map<String, String> headers,
String? webOnlyWindowName,
}) async {
launched = true;
return true;
}

@override
LinkDelegate? get linkDelegate => null;
}

0 comments on commit 3ae1121

Please sign in to comment.