|
| 1 | +import 'package:checks/checks.dart'; |
| 2 | +import 'package:flutter/foundation.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | +import 'package:url_launcher/url_launcher.dart'; |
| 6 | +import 'package:zulip/api/model/initial_snapshot.dart'; |
| 7 | +import 'package:zulip/api/model/model.dart'; |
| 8 | +import 'package:zulip/model/narrow.dart'; |
| 9 | +import 'package:zulip/widgets/content.dart'; |
| 10 | +import 'package:zulip/widgets/message_list.dart'; |
| 11 | +import 'package:zulip/widgets/page.dart'; |
| 12 | +import 'package:zulip/widgets/profile.dart'; |
| 13 | +import 'package:zulip/widgets/store.dart'; |
| 14 | + |
| 15 | +import '../example_data.dart' as eg; |
| 16 | +import '../model/binding.dart'; |
| 17 | +import '../model/test_store.dart'; |
| 18 | +import '../test_navigation.dart'; |
| 19 | +import 'message_list_checks.dart'; |
| 20 | +import 'page_checks.dart'; |
| 21 | +import 'profile_page_checks.dart'; |
| 22 | + |
| 23 | +Future<void> setupPage(WidgetTester tester, { |
| 24 | + required int pageUserId, |
| 25 | + List<User>? users, |
| 26 | + List<CustomProfileField>? customProfileFields, |
| 27 | + Map<String, RealmDefaultExternalAccount>? realmDefaultExternalAccounts, |
| 28 | + NavigatorObserver? navigatorObserver |
| 29 | +}) async { |
| 30 | + addTearDown(testBinding.reset); |
| 31 | + |
| 32 | + final initialSnapshot = eg.initialSnapshot( |
| 33 | + customProfileFields: customProfileFields, |
| 34 | + realmDefaultExternalAccounts: realmDefaultExternalAccounts); |
| 35 | + await testBinding.globalStore.add(eg.selfAccount, initialSnapshot); |
| 36 | + final store = await testBinding.globalStore.perAccount(eg.selfAccount.id); |
| 37 | + |
| 38 | + store.addUser(eg.selfUser); |
| 39 | + if (users != null) { |
| 40 | + store.addUsers(users); |
| 41 | + } |
| 42 | + |
| 43 | + await tester.pumpWidget( |
| 44 | + GlobalStoreWidget( |
| 45 | + child: MaterialApp( |
| 46 | + navigatorObservers: navigatorObserver != null ? [navigatorObserver] : [], |
| 47 | + home: PerAccountStoreWidget( |
| 48 | + accountId: eg.selfAccount.id, |
| 49 | + child: ProfilePage(userId: pageUserId))))); |
| 50 | + |
| 51 | + // global store, per-account store, and page get loaded |
| 52 | + await tester.pumpAndSettle(); |
| 53 | +} |
| 54 | + |
| 55 | +CustomProfileField mkCustomProfileField(int id, CustomProfileFieldType type, {int? order, bool? displayInProfileSummary, String? fieldData}) { |
| 56 | + return CustomProfileField( |
| 57 | + id: id, |
| 58 | + order: order ?? id, |
| 59 | + name: "field$id", |
| 60 | + hint: "hint$id", |
| 61 | + displayInProfileSummary: displayInProfileSummary ?? true, |
| 62 | + fieldData: fieldData ?? "", |
| 63 | + type: type, |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +void main() { |
| 68 | + TestZulipBinding.ensureInitialized(); |
| 69 | + |
| 70 | + group('ProfilePage', () { |
| 71 | + testWidgets('page builds; profile page renders', (WidgetTester tester) async { |
| 72 | + final user = eg.user(userId: 1, fullName: "test user"); |
| 73 | + |
| 74 | + await setupPage(tester, users:[user], pageUserId: user.userId); |
| 75 | + |
| 76 | + check(because: "find user avatar", find.byType(Avatar).evaluate()).length.equals(1); |
| 77 | + check(because: "find user name", find.text("test user").evaluate()).isNotEmpty(); |
| 78 | + }); |
| 79 | + |
| 80 | + testWidgets('page builds; profile page renders with profileData', (WidgetTester tester) async { |
| 81 | + await setupPage(tester, |
| 82 | + users: [ |
| 83 | + eg.user(userId: 1, profileData: { |
| 84 | + 0: ProfileFieldUserData(value: "shortTextValue"), |
| 85 | + 1: ProfileFieldUserData(value: "longTextValue"), |
| 86 | + 2: ProfileFieldUserData(value: "x"), |
| 87 | + 3: ProfileFieldUserData(value: "dateValue"), |
| 88 | + 4: ProfileFieldUserData(value: "http://example/linkValue"), |
| 89 | + 5: ProfileFieldUserData(value: "[2]"), |
| 90 | + 6: ProfileFieldUserData(value: "externalValue"), |
| 91 | + 7: ProfileFieldUserData(value: "pronounsValue"), |
| 92 | + }), |
| 93 | + eg.user(userId: 2), |
| 94 | + ], |
| 95 | + pageUserId: 1, |
| 96 | + customProfileFields: [ |
| 97 | + mkCustomProfileField(0, CustomProfileFieldType.shortText), |
| 98 | + mkCustomProfileField(1, CustomProfileFieldType.longText), |
| 99 | + mkCustomProfileField(2, CustomProfileFieldType.choice, fieldData: '{"x": {"text": "choiceValue", "order": "1"}}'), |
| 100 | + mkCustomProfileField(3, CustomProfileFieldType.date), |
| 101 | + mkCustomProfileField(4, CustomProfileFieldType.link), |
| 102 | + mkCustomProfileField(5, CustomProfileFieldType.user), |
| 103 | + mkCustomProfileField(6, CustomProfileFieldType.externalAccount, fieldData: '{"subtype": "external1"}'), |
| 104 | + mkCustomProfileField(7, CustomProfileFieldType.pronouns), |
| 105 | + ], realmDefaultExternalAccounts: { |
| 106 | + "external1": RealmDefaultExternalAccount( |
| 107 | + name: "external1", |
| 108 | + text: "", |
| 109 | + hint: "", |
| 110 | + urlPattern: "https://example/%(username)s")}); |
| 111 | + |
| 112 | + final testCases = [ |
| 113 | + (find.text("field0"), find.text("shortTextValue"), CustomProfileFieldType.shortText), |
| 114 | + (find.text("field1"), find.text("longTextValue"), CustomProfileFieldType.longText), |
| 115 | + (find.text("field2"), find.text("choiceValue"), CustomProfileFieldType.choice), |
| 116 | + (find.text("field3"), find.text("dateValue"), CustomProfileFieldType.date), |
| 117 | + (find.text("field4"), find.text("http://example/linkValue"), CustomProfileFieldType.link), |
| 118 | + (find.text("field5"), find.byType(Avatar), CustomProfileFieldType.user), |
| 119 | + (find.text("field6"), find.text("http://example/externalValue"), CustomProfileFieldType.externalAccount), |
| 120 | + (find.text("field7"), find.text("pronounsValue"), CustomProfileFieldType.pronouns), |
| 121 | + ]; |
| 122 | + final profileDataTable = find.byType(Column).last; |
| 123 | + testCases.map((testCase) { |
| 124 | + Finder labelFinder = testCase.$1; |
| 125 | + Finder fieldFinder = testCase.$2; |
| 126 | + CustomProfileFieldType testCaseType = testCase.$3; |
| 127 | + check( |
| 128 | + because: "find label for $testCaseType", |
| 129 | + find.descendant(of: profileDataTable, matching: labelFinder).evaluate().length |
| 130 | + ).equals(1); |
| 131 | + check( |
| 132 | + because: "find field for $testCaseType", |
| 133 | + find.descendant(of: profileDataTable, matching: fieldFinder).evaluate().length |
| 134 | + ).equals(1); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + testWidgets('page builds; error page shows up if data is missing', (WidgetTester tester) async { |
| 139 | + await setupPage(tester, pageUserId: eg.selfUser.userId + 1989); |
| 140 | + check(because: "find no user avatar", find.byType(Avatar).evaluate()).isEmpty(); |
| 141 | + check(because: "find error icon", find.byIcon(Icons.error).evaluate()).isNotEmpty(); |
| 142 | + }); |
| 143 | + |
| 144 | + testWidgets('page builds; link type will navigate', (WidgetTester tester) async { |
| 145 | + const testUrl = "http://example/url"; |
| 146 | + final user = eg.user(userId: 1, profileData: { |
| 147 | + 0: ProfileFieldUserData(value: testUrl), |
| 148 | + }); |
| 149 | + |
| 150 | + await setupPage(tester, |
| 151 | + users: [user], |
| 152 | + pageUserId: user.userId, |
| 153 | + customProfileFields: [mkCustomProfileField(0, CustomProfileFieldType.link)], |
| 154 | + ); |
| 155 | + |
| 156 | + await tester.tap(find.text(testUrl)); |
| 157 | + final expectedMode = defaultTargetPlatform == TargetPlatform.android ? |
| 158 | + LaunchMode.externalApplication : LaunchMode.platformDefault; |
| 159 | + check(testBinding.takeLaunchUrlCalls()) |
| 160 | + .single.equals((url: Uri.parse(testUrl), mode: expectedMode)); |
| 161 | + }); |
| 162 | + |
| 163 | + testWidgets('page builds; external link type navigates away', (WidgetTester tester) async { |
| 164 | + final user = eg.user(userId: 1, profileData: { |
| 165 | + 0: ProfileFieldUserData(value: "externalValue"), |
| 166 | + }); |
| 167 | + |
| 168 | + await setupPage(tester, |
| 169 | + users: [user], |
| 170 | + pageUserId: user.userId, |
| 171 | + customProfileFields: [mkCustomProfileField(0, CustomProfileFieldType.externalAccount, fieldData:'{"subtype": "external1"}')], |
| 172 | + realmDefaultExternalAccounts: { |
| 173 | + "external1": RealmDefaultExternalAccount( |
| 174 | + name: "external1", |
| 175 | + text: "", |
| 176 | + hint: "", |
| 177 | + urlPattern: "http://example/%(username)s")}, |
| 178 | + ); |
| 179 | + |
| 180 | + await tester.tap(find.text("externalValue")); |
| 181 | + final expectedMode = defaultTargetPlatform == TargetPlatform.android ? |
| 182 | + LaunchMode.externalApplication : LaunchMode.platformDefault; |
| 183 | + check(testBinding.takeLaunchUrlCalls()) |
| 184 | + .single.equals((url: Uri.parse("http://example/externalValue"), mode: expectedMode)); |
| 185 | + }); |
| 186 | + |
| 187 | + testWidgets('page builds; user links to profile', (WidgetTester tester) async { |
| 188 | + final users = [ |
| 189 | + eg.user(userId: 1, profileData: { |
| 190 | + 0: ProfileFieldUserData(value: "[2]"), |
| 191 | + }), |
| 192 | + eg.user(userId: 2, fullName: "test user"), |
| 193 | + ]; |
| 194 | + final pushedRoutes = <Route<dynamic>>[]; |
| 195 | + final testNavObserver = TestNavigatorObserver() |
| 196 | + ..onPushed = (route, prevRoute) => pushedRoutes.add(route); |
| 197 | + |
| 198 | + await setupPage(tester, |
| 199 | + users: users, |
| 200 | + pageUserId: 1, |
| 201 | + customProfileFields: [mkCustomProfileField(0, CustomProfileFieldType.user)], |
| 202 | + navigatorObserver: testNavObserver, |
| 203 | + ); |
| 204 | + |
| 205 | + final textFinder = find.text("test user"); |
| 206 | + check(textFinder.evaluate()).length.equals(1); |
| 207 | + final fieldContainer = find.ancestor(of: textFinder, matching: find.byType(Column)).first; |
| 208 | + final targetWidget = find.descendant(of: fieldContainer, matching:find.byType(Avatar)); |
| 209 | + await tester.tap(targetWidget, warnIfMissed: false); |
| 210 | + check(pushedRoutes).last.isA<WidgetRoute>().page.isA<ProfilePage>().userId.equals(2); |
| 211 | + }); |
| 212 | + |
| 213 | + testWidgets('page builds; dm links to correct narrow', (WidgetTester tester) async { |
| 214 | + final pushedRoutes = <Route<dynamic>>[]; |
| 215 | + final testNavObserver = TestNavigatorObserver() |
| 216 | + ..onPushed = (route, prevRoute) => pushedRoutes.add(route); |
| 217 | + final expectedNarrow = DmNarrow( |
| 218 | + allRecipientIds: [eg.selfUser.userId, 1]..sort(), |
| 219 | + selfUserId: eg.selfUser.userId); |
| 220 | + |
| 221 | + await setupPage(tester, |
| 222 | + users: [eg.user(userId: 1)], |
| 223 | + pageUserId: 1, |
| 224 | + navigatorObserver: testNavObserver, |
| 225 | + ); |
| 226 | + |
| 227 | + final targetWidget = find.byIcon(Icons.email); |
| 228 | + await tester.tap(targetWidget, warnIfMissed: false); |
| 229 | + check(pushedRoutes).last.isA<WidgetRoute>().page |
| 230 | + .isA<MessageListPage>() |
| 231 | + .narrow.equals(expectedNarrow); |
| 232 | + }); |
| 233 | + |
| 234 | + testWidgets('page builds; user links render multiple avatars', (WidgetTester tester) async { |
| 235 | + final users = [ |
| 236 | + eg.user(userId: 1, profileData: { |
| 237 | + 0: ProfileFieldUserData(value: "[2,3]"), |
| 238 | + }), |
| 239 | + eg.user(userId: 2, fullName: "test user1"), |
| 240 | + eg.user(userId: 3, fullName: "test user2"), |
| 241 | + ]; |
| 242 | + |
| 243 | + await setupPage(tester, |
| 244 | + users: users, |
| 245 | + pageUserId: 1, |
| 246 | + customProfileFields: [mkCustomProfileField(0, CustomProfileFieldType.user)], |
| 247 | + ); |
| 248 | + |
| 249 | + final findAvatars = find.byType(Avatar); |
| 250 | + check(findAvatars.evaluate()).length.equals(2 + 1); |
| 251 | + }); |
| 252 | + |
| 253 | + testWidgets('page builds; ensure long values do not overflow', (WidgetTester tester) async { |
| 254 | + final longString = List.filled(400, "X").join(); |
| 255 | + final user = eg.user(userId: 1, fullName: longString, profileData: { |
| 256 | + 0: ProfileFieldUserData(value: longString), |
| 257 | + 1: ProfileFieldUserData(value: "[1]"), |
| 258 | + }); |
| 259 | + |
| 260 | + await setupPage(tester, users:[user], pageUserId: user.userId, customProfileFields: [ |
| 261 | + mkCustomProfileField(0, CustomProfileFieldType.shortText), |
| 262 | + mkCustomProfileField(1, CustomProfileFieldType.user), |
| 263 | + ]); |
| 264 | + |
| 265 | + check(because: "find user name", find.text(longString).evaluate()).isNotEmpty(); |
| 266 | + }); |
| 267 | + |
| 268 | + }); |
| 269 | +} |
0 commit comments