diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c4b120..933bfe1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ All notable, user-facing changes to View Source Vibe are documented here. - Settings → Behavior: **Load lazy content** (off by default) scrolls the page once after it loads — and back to the top — so lazy images and embeds are actually fetched and measured; a banner above the Browser tab shows while this happens. Plus **Disable browser cache** and **Always clean cookies**. - Settings → Behavior: **Low resource mode** (off by default) for older devices. The Browser and Reader tabs each run their own WebView, and both stay alive at once; in this mode the WebView of a hidden tab is paused (and its media stopped) until its tab is selected again, media in the Browser tab only plays after a tap, Android marks hidden pages as waived renderer priority so the system can reclaim their memory, and both WebViews are suspended while the app is in the background. +- **Structure** tab (web pages in the browser): a wireframe of the rendered page in page proportions, coloured by owner (page, own frame, third party, advertising, social) or by element kind, with the heading outline. Tap a box for its size, owner and source line. + ### Improved - **Size** tab: download size first (Lighthouse-style total byte weight), cache hits counted, and resources that load late (lazy content, ads after scrolling) included. While "Load lazy content" is off, a note explains that totals may be underestimated because lazy resources were never fetched. - Keyboard shortcuts (Cmd/Ctrl+L focuses the location bar, Cmd/Ctrl+arrow keys switch tabs) and mouse-wheel tab switching on desktop. diff --git a/README.md b/README.md index bad90ac..d78fa67 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ Each page opens into a set of tabs: - **📱 Read** - **Source:** HTML, CSS, JS, XML and JSON with syntax highlighting, search, word wrap and line numbers. + - **Structure:** A wireframe of the rendered page, coloured by who owns each box (page, own frames, third parties, ads), with the heading outline. - **DOM Tree:** Navigate the document structure. - **Browser:** Built-in browser with tabs ("Surf & View Source"), plus Reader Mode and RSS/Atom feed support. diff --git a/lib/models/app_tab.dart b/lib/models/app_tab.dart index 0acbbd2..13de4d6 100644 --- a/lib/models/app_tab.dart +++ b/lib/models/app_tab.dart @@ -19,6 +19,7 @@ enum AppTab { media(TabGroup.page, 'Media', Icons.perm_media_outlined, 'media'), services(TabGroup.page, 'Services', Icons.layers_outlined, 'services'), reader(TabGroup.page, 'Reader', Icons.chrome_reader_mode, 'reader'), + structure(TabGroup.code, 'Structure', Icons.dashboard_outlined, 'structure'), domTree(TabGroup.code, 'DOM Tree', Icons.account_tree_outlined, 'dom-tree'), source(TabGroup.code, 'Source', Icons.code, 'source'), size(TabGroup.network, 'Size', Icons.scale_outlined, 'page-size'), @@ -61,6 +62,7 @@ List visibleTabsFor({ AppTab.privacy => metadata, AppTab.reader => isHtml, + AppTab.structure => browser && isHtml, AppTab.domTree => isHtmlOrXml, AppTab.source => true, AppTab.headers || diff --git a/lib/models/inspection/page_inspection.dart b/lib/models/inspection/page_inspection.dart index f5a6fd1..32cfabd 100644 --- a/lib/models/inspection/page_inspection.dart +++ b/lib/models/inspection/page_inspection.dart @@ -3,6 +3,7 @@ import 'dart:typed_data'; import 'browser_snapshot.dart'; import 'file_snapshot.dart'; import 'observed.dart'; +import 'page_layout.dart'; import 'permission_request.dart'; import 'server_snapshot.dart'; @@ -14,6 +15,10 @@ const kFreshLoadNotMeasured = Unavailable( const kNoScreenshot = Unavailable(UnavailableReason.notApplicable, 'no screenshot'); +/// [PageInspection.layout] before the browser measured it. +const kLayoutNotMeasured = + Unavailable(UnavailableReason.notApplicable, 'not measured yet'); + /// What is being inspected. sealed class InspectionTarget { const InspectionTarget(); @@ -76,6 +81,9 @@ class PageInspection { final Observed liveScreenshot; final Observed freshScreenshot; + /// The boxes of the live page (Structure tab). + final Observed layout; + /// Native permission requests the page made (all denied), oldest first. final List permissionRequests; @@ -94,6 +102,7 @@ class PageInspection { this.freshEstimates = const {}, this.liveScreenshot = kNoScreenshot, this.freshScreenshot = kNoScreenshot, + this.layout = kLayoutNotMeasured, this.permissionRequests = const [], }); @@ -116,6 +125,7 @@ class PageInspection { freshEstimates = const {}, liveScreenshot = kNoScreenshot, freshScreenshot = kNoScreenshot, + layout = kLayoutNotMeasured, permissionRequests = const []; PageInspection copyWith({ @@ -131,6 +141,7 @@ class PageInspection { Map>? freshEstimates, Observed? liveScreenshot, Observed? freshScreenshot, + Observed? layout, List? permissionRequests, }) => PageInspection( @@ -150,6 +161,7 @@ class PageInspection { freshEstimates: freshEstimates ?? this.freshEstimates, liveScreenshot: liveScreenshot ?? this.liveScreenshot, freshScreenshot: freshScreenshot ?? this.freshScreenshot, + layout: layout ?? this.layout, permissionRequests: permissionRequests ?? this.permissionRequests, ); diff --git a/lib/models/inspection/page_layout.dart b/lib/models/inspection/page_layout.dart new file mode 100644 index 0000000..a94f7c2 --- /dev/null +++ b/lib/models/inspection/page_layout.dart @@ -0,0 +1,54 @@ +enum LayoutKind { landmark, frame, heading, media, block } + +/// One measured box of the page, in CSS pixels in page space. +class LayoutBlock { + final String tag, role, id, classes, src, text; + final double x, y, width, height; + final int depth, descendants; + final LayoutKind kind; + + /// position: fixed or sticky (overlaps the content). + final bool fixed; + + const LayoutBlock({ + required this.tag, + required this.role, + required this.id, + required this.classes, + required this.src, + required this.text, + required this.x, + required this.y, + required this.width, + required this.height, + required this.depth, + required this.descendants, + required this.kind, + required this.fixed, + }); + + double get area => width * height; +} + +/// The page's measured layout: the viewport, the whole document and the +/// first qualifying boxes ([totalBlocks] counts all of them). +class PageLayout { + final double viewportWidth, viewportHeight, width, height; + final int totalBlocks; + final List blocks; + + /// The page had more elements than the script scanned. + final bool scanCapped; + + bool get truncated => totalBlocks > blocks.length; + + const PageLayout({ + required this.viewportWidth, + required this.viewportHeight, + required this.width, + required this.height, + required this.totalBlocks, + required this.blocks, + this.scanCapped = false, + }); +} diff --git a/lib/screens/about_screen.dart b/lib/screens/about_screen.dart index 54445e1..77412d4 100644 --- a/lib/screens/about_screen.dart +++ b/lib/screens/about_screen.dart @@ -16,6 +16,10 @@ class AboutScreen extends StatelessWidget { Icons.code, 'Source viewer: HTML, CSS, JS, XML, JSON with syntax highlighting, search, word wrap and beautify' ), + ( + Icons.dashboard_outlined, + 'Structure: a wireframe of the rendered page, coloured by who owns each box' + ), (Icons.account_tree_outlined, 'DOM Tree explorer'), (Icons.chrome_reader_mode, 'Reader mode and RSS/Atom feeds'), ( diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart index f36f6f9..a44ce28 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -23,6 +23,7 @@ import 'package:view_source_vibe/widgets/permissions_view.dart'; import 'package:view_source_vibe/widgets/request_timeline_view.dart'; import 'package:view_source_vibe/widgets/sovereignty_view.dart'; import 'package:view_source_vibe/widgets/dom_tree_view.dart'; +import 'package:view_source_vibe/widgets/structure_view.dart'; import 'package:view_source_vibe/widgets/keep_alive_wrapper.dart'; import 'package:view_source_vibe/widgets/browser_view.dart'; import 'package:view_source_vibe/widgets/reader_view.dart'; @@ -421,6 +422,7 @@ class HomeScreenState extends State with TickerProviderStateMixin { AppTab.media => page(tab, const MediaView()), AppTab.services => page(tab, const ServicesView()), AppTab.reader => readerView, + AppTab.structure => page(tab, const StructureView()), AppTab.domTree => page(tab, const DomTreeView()), AppTab.source => sourceView, AppTab.size => page(tab, const PageSizeView()), diff --git a/lib/services/collectors/collector_world.dart b/lib/services/collectors/collector_world.dart index 1400070..a46474c 100644 --- a/lib/services/collectors/collector_world.dart +++ b/lib/services/collectors/collector_world.dart @@ -2,6 +2,7 @@ import 'package:flutter/foundation.dart' show TargetPlatform; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'vsv_collect_js.dart'; +import 'vsv_layout_js.dart'; /// The JavaScript world the collector runs in. iOS and macOS (WKWebView) have /// isolated worlds: the page cannot see or alter the collector there. Android @@ -11,14 +12,14 @@ ContentWorld? collectorWorld(TargetPlatform platform) => ? ContentWorld.world(name: 'vsv') : null; -/// Bootstrap + collector + scroll-through as one document-start user script, -/// in every frame. In an isolated world the fetch/XHR patch is skipped +/// Bootstrap + collector + scroll-through + layout measurement as one +/// document-start user script, in every frame. In an isolated world the fetch/XHR patch is skipped /// (`__vsv_isolated`): it would only patch the isolated copy of those objects. UserScript collectorUserScript(TargetPlatform platform) { final world = collectorWorld(platform); return UserScript( source: '${world != null ? 'window.__vsv_isolated = true;\n' : ''}' - '$vsvResourceBootstrapJs\n$vsvCollectJs\n$vsvScrollThroughJs', + '$vsvResourceBootstrapJs\n$vsvCollectJs\n$vsvScrollThroughJs\n$vsvLayoutJs', injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START, // Also in iframes: they post their resources (ads, embeds) to the top // frame, so the page size counts them. diff --git a/lib/services/collectors/layout_payload.dart b/lib/services/collectors/layout_payload.dart new file mode 100644 index 0000000..09443f9 --- /dev/null +++ b/lib/services/collectors/layout_payload.dart @@ -0,0 +1,101 @@ +import 'dart:convert'; + +import '../../models/inspection/observed.dart'; +import '../../models/inspection/page_layout.dart'; + +const _noLayout = Unavailable( + UnavailableReason.notApplicable, 'the page has no layout'); + +/// Parses the JSON string returned by `__vsv_layout()`. A non-String result +/// is [Unavailable]; an unparsable one [Failed]; a page without layout +/// (`empty`, or a zero-size document) is [Unavailable] with a reason. A page +/// with a size but no qualifying boxes is [Present] with no blocks. +Observed parseLayoutPayload(Object? raw, {required Uri pageUrl}) { + if (raw is! String) { + return Unavailable(UnavailableReason.unsupportedPlatform, + 'evaluateJavascript returned ${raw == null ? 'null' : raw.runtimeType}, not a String'); + } + try { + final m = jsonDecode(raw); + if (m is! Map) return const Failed('layout payload is not an object'); + if (m['empty'] == true) return _noLayout; + final blocks = []; + final list = m['blocks']; + if (list is List) { + for (final b in list) { + final block = b is Map ? _block(b, pageUrl) : null; + if (block != null) blocks.add(block); + } + } + final total = _int(m['total']) ?? blocks.length; + final width = _num(m['w']), height = _num(m['h']); + final vw = _num(m['vw']), vh = _num(m['vh']); + if (width == null || height == null || vw == null || vh == null) { + return const Failed('layout payload has no document or viewport size'); + } + if (width <= 0 || height <= 0) return _noLayout; + return Present( + PageLayout( + viewportWidth: vw, + viewportHeight: vh, + width: width, + height: height, + totalBlocks: total, + blocks: blocks, + scanCapped: m['scanCapped'] == 1 || m['scanCapped'] == true, + ), + source: DataSource.browser); + } catch (e) { + return Failed('$e'); + } +} + +double? _num(Object? v) => v is num ? v.toDouble() : null; +int? _int(Object? v) => v is num ? v.toInt() : null; + +String _str(Object? v) => v is String ? v : ''; + +LayoutBlock? _block(Map b, Uri pageUrl) { + final kind = switch (b['k']) { + 'landmark' => LayoutKind.landmark, + 'frame' => LayoutKind.frame, + 'heading' => LayoutKind.heading, + 'media' => LayoutKind.media, + 'block' => LayoutKind.block, + _ => null, + }; + final x = _num(b['x']), y = _num(b['y']); + final w = _num(b['w']), h = _num(b['h']); + final depth = _int(b['d']), descendants = _int(b['n']); + if (kind == null || + x == null || + y == null || + w == null || + h == null || + depth == null || + descendants == null) { + return null; + } + var src = _str(b['s']); + if (src.isNotEmpty) { + try { + src = pageUrl.resolve(src).toString(); + } catch (_) {} + } + return LayoutBlock( + tag: _str(b['t']), + role: _str(b['r']), + id: _str(b['i']), + classes: _str(b['c']), + src: src, + text: _str(b['tx']), + x: x, + y: y, + width: w, + height: h, + depth: depth, + descendants: descendants, + kind: kind, + fixed: b['f'] == 1 || b['f'] == true, + ); +} diff --git a/lib/services/collectors/vsv_layout_js.dart b/lib/services/collectors/vsv_layout_js.dart new file mode 100644 index 0000000..2174ca7 --- /dev/null +++ b/lib/services/collectors/vsv_layout_js.dart @@ -0,0 +1,95 @@ +// Browser-side JavaScript that measures the page's boxes. Read-only: it reads +// layout (getBoundingClientRect, getComputedStyle) and never scrolls, resizes +// or writes to the page. Keep this ES5-ish (no arrow functions, no let/const). + +/// Defines `window.__vsv_layout()`, which returns a JSON string: +/// `{v, vw, vh, w, h, total, scanCapped, blocks: [{t, r, i, c, x, y, w, h, d, n, k, s, +/// f, tx}]}` with coordinates in page space, or `{v: 1, empty: true}` when the +/// document has no layout. `blocks` holds the first 400 qualifying elements +/// in document order; `total` counts all of them. `h` grows to the lowest +/// block (a page whose body scrolls); blocks right of the document width +/// (carousel slides) are skipped; `scanCapped` is 1 when the page had more +/// elements than were scanned. +const String vsvLayoutJs = r''' +(function() { + var MAX_BLOCKS = 400, MAX_SCAN = 20000, MAX_DEPTH = 12; + var LANDMARK_TAGS = {header: 1, nav: 1, main: 1, article: 1, section: 1, + aside: 1, footer: 1, form: 1, dialog: 1}; + var LANDMARK_ROLES = {banner: 1, navigation: 1, main: 1, complementary: 1, + contentinfo: 1, search: 1, form: 1, region: 1, dialog: 1}; + + function kindOf(tag, role, w, h, vArea) { + if (tag === 'iframe') return 'frame'; + if (/^h[1-6]$/.test(tag)) return 'heading'; + if (LANDMARK_TAGS[tag] === 1 || (role && LANDMARK_ROLES[role] === 1)) return 'landmark'; + if (tag === 'video' || tag === 'canvas') return 'media'; + if (tag === 'img' && w * h >= 10000) return 'media'; + return w * h >= 0.15 * vArea ? 'maybe' : ''; + } + + function depthOf(el, body) { + var d = 0, p = el; + while (p && p !== body && d < 100) { p = p.parentNode; d++; } + return d; + } + + function sourceOf(el, tag) { + var s = ''; + if (tag === 'iframe') s = el.src || ''; + else if (tag === 'img' || tag === 'video') s = el.currentSrc || el.src || ''; + return /^data:/i.test(s) ? '' : s.slice(0, 2000); + } + + window.__vsv_layout = function() { + var body = document.body; + var se = document.scrollingElement || document.documentElement; + var docW = se ? se.scrollWidth : 0, docH = se ? se.scrollHeight : 0; + if (!body || !docW || !docH) return JSON.stringify({v: 1, empty: true}); + var sx = window.pageXOffset || window.scrollX || 0; + var sy = window.pageYOffset || window.scrollY || 0; + var vw = window.innerWidth || 0, vh = window.innerHeight || 0; + var vArea = vw * vh; + var all = body.getElementsByTagName('*'); + var limit = Math.min(all.length, MAX_SCAN); + var blocks = [], total = 0; + for (var i = 0; i < limit; i++) { + var el = all[i]; + var tag = String(el.tagName || '').toLowerCase(); + var rect = el.getBoundingClientRect(); + if (rect.width < 1 || rect.height < 1) continue; + var x = rect.left + sx, y = rect.top + sy; + if (x + rect.width <= 0 || y + rect.height <= 0) continue; + if (x >= docW) continue; + var role = el.getAttribute('role') || ''; + var kind = kindOf(tag, role, rect.width, rect.height, vArea); + if (!kind) continue; + var depth = depthOf(el, body); + var n = el.getElementsByTagName('*').length; + if (kind === 'maybe') { + if (n < 20 || depth > MAX_DEPTH) continue; + kind = 'block'; + } + var cs = getComputedStyle(el); + if (cs.visibility === 'hidden') continue; + total++; + if (y + rect.height > docH) docH = Math.ceil(y + rect.height); + if (blocks.length >= MAX_BLOCKS) continue; + blocks.push({ + t: tag, r: role, + i: String(el.id || '').slice(0, 60), + c: String(el.className && el.className.baseVal !== undefined ? el.className.baseVal : (el.className || '')) + .replace(/^\s+|\s+$/g, '').split(/\s+/).slice(0, 2).join(' ').slice(0, 60), + x: Math.round(x), y: Math.round(y), + w: Math.round(rect.width), h: Math.round(rect.height), + d: depth, n: n, k: kind, s: sourceOf(el, tag), + f: cs.position === 'fixed' || cs.position === 'sticky' ? 1 : 0, + tx: kind === 'heading' + ? String(el.textContent || '').replace(/\s+/g, ' ').replace(/^ | $/g, '').slice(0, 80) + : '' + }); + } + return JSON.stringify({v: 1, vw: vw, vh: vh, w: docW, h: docH, + total: total, scanCapped: all.length > MAX_SCAN ? 1 : 0, blocks: blocks}); + }; +})(); +'''; diff --git a/lib/services/derive/structure_view.dart b/lib/services/derive/structure_view.dart new file mode 100644 index 0000000..d744ada --- /dev/null +++ b/lib/services/derive/structure_view.dart @@ -0,0 +1,259 @@ +import 'dart:math' as math; + +import '../../models/inspection/browser_snapshot.dart'; +import '../../models/inspection/observed.dart'; +import '../../models/inspection/page_inspection.dart'; +import '../../models/inspection/page_layout.dart'; +import '../../utils/site.dart'; +import '../detection/engine.dart'; + +enum BlockOwner { page, ownFrame, thirdParty, advertising, social } + +class StructureBlock { + final LayoutBlock block; + final BlockOwner owner; + + /// 'header#top.site-header', 'iframe · ads.test', 'h2'. + final String label; + + /// Catalogue name when the source belongs to one. + final String? service; + + const StructureBlock( + {required this.block, + required this.owner, + required this.label, + this.service}); +} + +class OutlineItem { + final int level; + final String text; + const OutlineItem(this.level, this.text); +} + +class StructureModel { + final PageLayout layout; + + /// Paint order: by area descending, fixed and sticky last. + final List blocks; + + /// Document order. + final List headings; + + /// Which of header, nav, main, footer exist. + final List landmarks; + final List notes; + + /// Which viewport the layout was measured at. + final String measuredAt; + + /// 0..1 of the page area under third-party frames. + final double thirdPartyShare; + + const StructureModel({ + required this.layout, + required this.blocks, + required this.headings, + required this.landmarks, + required this.notes, + required this.measuredAt, + required this.thirdPartyShare, + }); +} + +const double kMaxWireframeHeight = 6000; + +/// Scale from page pixels to logical pixels: fits [availableWidth], and +/// never lets the drawing exceed [kMaxWireframeHeight]. +double wireframeScale(PageLayout l, double availableWidth) { + if (l.width <= 0 || l.height <= 0) return 1; + return math.min(availableWidth / l.width, kMaxWireframeHeight / l.height); +} + +/// The smallest block that contains the page point; null when none does. +StructureBlock? blockAt(StructureModel m, double pageX, double pageY) { + StructureBlock? best; + for (final b in m.blocks) { + final k = b.block; + if (pageX < k.x || pageY < k.y) continue; + if (pageX > k.x + k.width || pageY > k.y + k.height) continue; + if (best == null || k.area <= best.block.area) best = b; + } + return best; +} + +/// 1-based line of the first `id=""` or `id=''` in [source]; null +/// when absent or when [id] is empty. +int? lineOfId(String source, String id) { + if (id.isEmpty) return null; + final m = RegExp( + '\\bid\\s*=\\s*(?:"${RegExp.escape(id)}"|\'${RegExp.escape(id)}\')') + .firstMatch(source); + if (m == null) return null; + return '\n'.allMatches(source.substring(0, m.start)).length + 1; +} + +const _notInBrowser = Unavailable( + UnavailableReason.notApplicable, 'only for pages loaded in the browser'); + +Observed deriveStructure(PageInspection p) { + if (p.target is FileTarget) return _notInBrowser; + final layoutState = p.layout; + switch (layoutState) { + case Pending(:final detail): + return Pending(detail); + case Failed(:final error): + return Failed(error); + case Unavailable(:final reason, :final detail): + return Unavailable(reason, detail); + case Present(:final value, :final source): + return Present(_derive(value, _pageUrl(p)), source: source); + } +} + +String _pageUrl(PageInspection p) { + final t = p.target; + if (t is UrlTarget) return t.url.toString(); + final b = p.browser; + return b is Present ? b.value.url.toString() : ''; +} + +const _landmarkNames = { + 'header': 'header', + 'banner': 'header', + 'nav': 'nav', + 'navigation': 'nav', + 'main': 'main', + 'footer': 'footer', + 'contentinfo': 'footer', +}; + +String _hostOf(String url) => Uri.tryParse(url)?.host ?? ''; + +String _labelOf(LayoutBlock b) { + if (b.kind == LayoutKind.frame) { + final host = _hostOf(b.src); + return host.isEmpty ? b.tag : '${b.tag} · $host'; + } + final firstClass = b.classes + .split(RegExp(r'\s+')) + .firstWhere((c) => c.isNotEmpty, orElse: () => ''); + return '${b.tag}${b.id.isEmpty ? '' : '#${b.id}'}' + '${firstClass.isEmpty ? '' : '.$firstClass'}'; +} + +StructureBlock _classify(LayoutBlock b, String pageUrl) { + var owner = BlockOwner.page; + String? service; + final hasSource = + (b.kind == LayoutKind.frame || b.kind == LayoutKind.media) && + b.src.isNotEmpty; + if (hasSource) { + final entities = externalServicesForUrl(b.src, pageUrl); + final ad = + entities.where((e) => e.category == DetectionCategory.advertising); + final social = + entities.where((e) => e.category == DetectionCategory.social); + final pageHost = _hostOf(pageUrl); + final host = _hostOf(b.src); + if (ad.isNotEmpty) { + owner = BlockOwner.advertising; + service = ad.first.name; + } else if (social.isNotEmpty) { + owner = BlockOwner.social; + service = social.first.name; + } else if (host.isNotEmpty && !sameSite(host, pageHost)) { + owner = BlockOwner.thirdParty; + if (entities.isNotEmpty) service = entities.first.name; + } else if (b.kind == LayoutKind.frame) { + owner = BlockOwner.ownFrame; + } + } + return StructureBlock( + block: b, owner: owner, label: _labelOf(b), service: service); +} + +final _headingTag = RegExp(r'^h([1-6])$'); + +int _headingLevel(String tag) => + int.parse(_headingTag.firstMatch(tag)?.group(1) ?? '1'); + +String _n(double v) => v.round().toString(); + +StructureModel _derive(PageLayout l, String pageUrl) { + final all = [for (final b in l.blocks) _classify(b, pageUrl)]; + + final painted = [...all]..sort((a, b) { + if (a.block.fixed != b.block.fixed) return a.block.fixed ? 1 : -1; + return b.block.area.compareTo(a.block.area); + }); + + final headings = [ + for (final b in l.blocks) + if (b.kind == LayoutKind.heading) + OutlineItem(_headingLevel(b.tag), b.text), + ]; + + final found = { + for (final b in l.blocks) + if (b.kind == LayoutKind.landmark) + if ((_landmarkNames[b.tag] ?? _landmarkNames[b.role]) case final name?) + name, + }; + final landmarks = [ + for (final n in const ['header', 'nav', 'main', 'footer']) + if (found.contains(n)) n + ]; + + var thirdPartyArea = 0.0; + for (final s in all) { + final b = s.block; + if (b.kind != LayoutKind.frame || + s.owner == BlockOwner.page || + s.owner == BlockOwner.ownFrame) { + continue; + } + final w = math.min(b.x + b.width, l.width) - math.max(b.x, 0); + final h = math.min(b.y + b.height, l.height) - math.max(b.y, 0); + if (w > 0 && h > 0) thirdPartyArea += w * h; + } + final pageArea = l.width * l.height; + final share = pageArea <= 0 ? 0.0 : math.min(1.0, thirdPartyArea / pageArea); + + final notes = [ + if (l.truncated) + 'Only the first ${l.blocks.length} of ${l.totalBlocks} blocks are drawn.', + if (l.scanCapped) + 'Only the first 20000 elements were measured; landmark and heading ' + 'notes may be incomplete.', + if (l.totalBlocks == 0 && l.blocks.isEmpty) + 'Nothing on this page is large or marked up enough to draw: no ' + 'landmarks, headings, frames, media or large blocks.', + if (!landmarks.contains('main')) 'No main landmark.', + ]; + final h1s = headings.where((h) => h.level == 1).length; + if (h1s != 1) notes.add('$h1s h1 headings (one is usual).'); + for (var i = 1; i < headings.length; i++) { + final from = headings[i - 1].level, to = headings[i].level; + if (to > from + 1) { + notes.add('Heading levels skip from h$from to h$to.'); + break; + } + } + if (share >= 0.05) { + notes + .add('Third-party frames cover ${(share * 100).round()}% of the page.'); + } + + return StructureModel( + layout: l, + blocks: painted, + headings: headings, + landmarks: landmarks, + notes: notes, + measuredAt: + 'Measured at ${_n(l.viewportWidth)}\u00d7${_n(l.viewportHeight)}', + thirdPartyShare: share, + ); +} diff --git a/lib/services/html_service.dart b/lib/services/html_service.dart index 0f2226d..5f7c6d1 100644 --- a/lib/services/html_service.dart +++ b/lib/services/html_service.dart @@ -25,11 +25,13 @@ import 'package:xml/xml.dart' as xml; import 'package:view_source_vibe/models/inspection/file_snapshot.dart'; import 'package:view_source_vibe/models/inspection/observed.dart'; import 'package:view_source_vibe/models/inspection/page_inspection.dart'; +import 'package:view_source_vibe/models/inspection/page_layout.dart'; import 'package:view_source_vibe/models/inspection/permission_request.dart'; import 'package:view_source_vibe/models/inspection/server_snapshot.dart'; import 'package:view_source_vibe/services/collectors/browser_loader.dart'; import 'package:view_source_vibe/services/collectors/collector_world.dart'; import 'package:view_source_vibe/services/collectors/file_loader.dart'; +import 'package:view_source_vibe/services/collectors/layout_payload.dart'; import 'package:view_source_vibe/services/collectors/server_fetcher.dart'; import 'package:view_source_vibe/services/derive/cookies_view.dart'; import 'package:view_source_vibe/services/derive/detections.dart'; @@ -53,6 +55,7 @@ import 'package:view_source_vibe/services/derive/page_size.dart'; import 'package:view_source_vibe/services/derive/dom_tree.dart'; import 'package:view_source_vibe/services/derive/resource_matching.dart'; import 'package:view_source_vibe/services/derive/source_view.dart'; +import 'package:view_source_vibe/services/derive/structure_view.dart'; import 'package:view_source_vibe/services/inspection_store.dart'; import 'package:view_source_vibe/services/legacy_browser_adapter.dart'; import 'package:view_source_vibe/services/reader_service.dart'; @@ -108,6 +111,7 @@ class HtmlService extends ChangeNotifier { late final BrowserLoader _browserLoader; final MetadataExtractor _metadataExtractor; final BrowserCollect? _browserCollectOverride; + final BrowserCollect? _layoutCollectOverride; final NativeCookieReader _nativeCookies; /// Deletes every cookie (the "Always clean cookies" setting); a seam for @@ -942,6 +946,37 @@ class HtmlService extends ChangeNotifier { return _cookiesView!; } + Observed? _structure; + ({ + InspectionTarget target, + Observed browser, + Observed layout + })? _structureFor; + + /// The Structure tab: the measured layout as a wireframe model, or why + /// there is none. Cached on the snapshots it reads, not on the + /// PageInspection object. + Observed get structureView { + final current = store.current; + final key = ( + target: current.target, + browser: current.browser, + layout: current.layout + ); + if (_structure == null || _structureFor != key) { + _structureFor = key; + _structure = deriveStructure(current); + } + return _structure!; + } + + /// 1-based line of the element with [id] in the Source tab's document; + /// null when the document is unknown or has no such id. + int? sourceLineOfId(String id) { + final text = sourceView.text; + return text is Present ? lineOfId(text.value, id) : null; + } + Observed? _timelineModel; ({ InspectionTarget target, @@ -1512,6 +1547,12 @@ class HtmlService extends ChangeNotifier { triggerBrowserReload(); } + // Structure tab: the page may have changed since it was measured. + if (index == tabIndexOf(AppTab.structure) && + store.current.browserSettled) { + collectLayout().ignore(); + } + // Source tab without content yet: the server body may have arrived. if (index == sourceTabIndex) { _adoptServerBodyIfEmpty(store.current.generation).ignore(); @@ -1663,6 +1704,7 @@ class HtmlService extends ChangeNotifier { SovereigntyAnalyzer? sovereigntyAnalyzer, Duration sovereigntyDebounce = const Duration(milliseconds: 500), BrowserCollect? browserCollect, + BrowserCollect? layoutCollect, NativeCookieReader? nativeCookies, ResourceSizeLookup? sizeLookup, Future Function()? deleteAllCookies, @@ -1681,6 +1723,7 @@ class HtmlService extends ChangeNotifier { _domTreeBuilder = domTreeBuilder, _domTreeIsolateThreshold = domTreeIsolateThreshold, _browserCollectOverride = browserCollect, + _layoutCollectOverride = layoutCollect, _nativeCookies = nativeCookies ?? _nativeCookiesFromManager, _deleteAllCookies = deleteAllCookies ?? (() => CookieManager.instance().deleteAllCookies()), @@ -4277,6 +4320,40 @@ Technical details: $message'''; store.applyLiveScreenshot(gen, _screenshotOf(bytes)); } + /// Measures the live page's boxes for the Structure tab. Dropped when the + /// user navigates meanwhile. + Future collectLayout() async { + final target = store.current.target; + if (target is! UrlTarget) return; + final gen = store.current.generation; + bool stale() => _disposed || gen != store.current.generation; + final override = _layoutCollectOverride; + final controller = activeWebViewController; + if (override == null && controller == null) { + store.applyLayout( + gen, + const Unavailable( + UnavailableReason.notApplicable, 'needs the browser')); + return; + } + // A re-measure keeps the old drawing until the new result replaces it. + if (store.current.layout is! Present) { + store.applyLayout(gen, const Pending()); + } + Observed result; + try { + final raw = override != null + ? await override(target.url.toString()) + : await controller!.evaluateJavascript( + source: 'window.__vsv_layout && window.__vsv_layout()', + contentWorld: collectorWorld(defaultTargetPlatform)); + result = parseLayoutPayload(raw, pageUrl: target.url); + } catch (e) { + result = Failed('Layout measurement failed: $e'); + } + if (!stale()) store.applyLayout(gen, result); + } + /// Loads the page once more like Lighthouse (empty cache, phone screen, /// quiet network) in an off-screen browser, then sizes what it could not /// see with the app's own lookups. Only on the user's request: it is a @@ -4536,6 +4613,9 @@ Technical details: $message'''; store.markBrowserSettled(gen); return; } + if (store.current.layout is! Present) { + store.applyLayout(gen, const Pending('waiting for the page to settle')); + } bool stale() => _disposed || gen != store.current.generation; final clock = Stopwatch()..start(); var lastChange = Duration.zero; @@ -4577,6 +4657,7 @@ Technical details: $message'''; } store.markBrowserSettled(gen); _captureLiveScreenshot(gen).ignore(); + collectLayout().ignore(); } else { next(); } diff --git a/lib/services/inspection_export_data.dart b/lib/services/inspection_export_data.dart index 9ecb052..44851cd 100644 --- a/lib/services/inspection_export_data.dart +++ b/lib/services/inspection_export_data.dart @@ -10,6 +10,7 @@ import 'derive/page_weight.dart'; import 'derive/probe_general.dart'; import 'derive/permissions_view.dart'; import 'derive/security_audit.dart'; +import 'derive/structure_view.dart'; import 'observed_json.dart'; /// The data files of an inspection package, built from the store's @@ -259,6 +260,47 @@ class InspectionExportData { ); } + /// analysis/structure.json + static Map structure(Observed o) => + observedJson( + o, + (m) => { + 'viewport': { + 'w': m.layout.viewportWidth, + 'h': m.layout.viewportHeight + }, + 'page': {'w': m.layout.width, 'h': m.layout.height}, + 'totalBlocks': m.layout.totalBlocks, + 'thirdPartyShare': m.thirdPartyShare, + 'landmarks': m.landmarks, + 'notes': m.notes, + 'headings': [ + for (final h in m.headings) {'level': h.level, 'text': h.text} + ], + 'blocks': [ + for (final s in m.blocks) + { + 'tag': s.block.tag, + 'role': s.block.role, + 'id': s.block.id, + 'classes': s.block.classes, + 'x': s.block.x, + 'y': s.block.y, + 'w': s.block.width, + 'h': s.block.height, + 'depth': s.block.depth, + 'descendants': s.block.descendants, + 'kind': s.block.kind.name, + 'owner': s.owner.name, + 'service': s.service, + 'src': s.block.src, + 'fixed': s.block.fixed, + } + ], + }, + source: DataSource.browser, + provenance: Provenance.derived); + /// analysis/page-weight.json static Map pageWeight(PageInspection p) { final w = derivePageWeight(p); diff --git a/lib/services/inspection_export_service.dart b/lib/services/inspection_export_service.dart index 2fec014..bc1c29b 100644 --- a/lib/services/inspection_export_service.dart +++ b/lib/services/inspection_export_service.dart @@ -181,6 +181,8 @@ class InspectionExportService { 'checked': detections.sources.line, 'value': [for (final d in detections.detections) d.toMap()], }); + final structureJson = unlessBlocked( + InspectionExportData.structure(htmlService.structureView)); final sovereigntyJson = observedJson(sovereignty, (r) => r.toJson(), source: DataSource.server, provenance: Provenance.derived); @@ -239,6 +241,7 @@ class InspectionExportService { metadata, (m) => m.data['structuredData'] ?? const [], source: DataSource.browser))); addJson('analysis/page-weight.json', weightJson); + addJson('analysis/structure.json', structureJson); // The raw collector payload of the last sync, so real-device fixtures can // be recorded later. Cookie and storage values are redacted (R22). diff --git a/lib/services/inspection_store.dart b/lib/services/inspection_store.dart index e4a9b2a..5c711bc 100644 --- a/lib/services/inspection_store.dart +++ b/lib/services/inspection_store.dart @@ -7,6 +7,7 @@ import '../models/inspection/browser_snapshot.dart'; import '../models/inspection/file_snapshot.dart'; import '../models/inspection/observed.dart'; import '../models/inspection/page_inspection.dart'; +import '../models/inspection/page_layout.dart'; import '../models/inspection/permission_request.dart'; import '../models/inspection/server_snapshot.dart'; import 'collectors/browser_payload.dart'; @@ -186,6 +187,12 @@ class InspectionStore extends ChangeNotifier { _set(_current.copyWith(liveScreenshot: shot)); } + /// The measured layout of the live page. + void applyLayout(int gen, Observed layout) { + if (!_accepts(gen)) return; + _set(_current.copyWith(layout: layout)); + } + /// Own size lookups for the fresh load, merged like [applyEstimates]. void applyFreshEstimates(int gen, Map> estimates) { if (!_accepts(gen)) return; diff --git a/lib/widgets/structure_painter.dart b/lib/widgets/structure_painter.dart new file mode 100644 index 0000000..3f4ae2b --- /dev/null +++ b/lib/widgets/structure_painter.dart @@ -0,0 +1,153 @@ +import 'dart:ui' as ui; + +import 'package:material_ui/material_ui.dart'; +import 'package:view_source_vibe/models/inspection/page_layout.dart'; +import 'package:view_source_vibe/services/derive/structure_view.dart'; + +/// What the wireframe's colours stand for. +enum StructureColoring { owner, element } + +/// Legend entries and fill colours of the two colourings. Colours come from +/// the theme; the owner tones match the Privacy tab's categories. +class StructurePalette { + final ColorScheme scheme; + const StructurePalette(this.scheme); + + Color owner(BlockOwner o) => switch (o) { + BlockOwner.page => scheme.surfaceContainerHighest, + BlockOwner.ownFrame => scheme.tertiary, + BlockOwner.thirdParty => Colors.orange, + BlockOwner.advertising => Colors.red, + BlockOwner.social => Colors.green, + }; + + Color kind(LayoutKind k) => switch (k) { + LayoutKind.landmark => scheme.primary, + LayoutKind.frame => scheme.tertiary, + LayoutKind.heading => scheme.secondary, + LayoutKind.media => scheme.error, + LayoutKind.block => scheme.surfaceContainerHighest, + }; + + Color of(StructureBlock b, StructureColoring mode) => + mode == StructureColoring.owner ? owner(b.owner) : kind(b.block.kind); + + List<(String, Color)> legend(StructureColoring mode) => + mode == StructureColoring.owner + ? [ + ('Page', owner(BlockOwner.page)), + ('Own frame', owner(BlockOwner.ownFrame)), + ('Third party', owner(BlockOwner.thirdParty)), + ('Advertising', owner(BlockOwner.advertising)), + ('Social', owner(BlockOwner.social)), + ] + : [ + ('Landmark', kind(LayoutKind.landmark)), + ('Frame', kind(LayoutKind.frame)), + ('Heading', kind(LayoutKind.heading)), + ('Media', kind(LayoutKind.media)), + ('Block', kind(LayoutKind.block)), + ]; +} + +/// Draws the page's blocks in page proportions: [scale] logical px per CSS px. +class StructurePainter extends CustomPainter { + final StructureModel model; + final double scale; + final StructureColoring coloring; + final ColorScheme scheme; + + StructurePainter({ + required this.model, + required this.scale, + required this.coloring, + required this.scheme, + }); + + static const double minLabelWidth = 60; + static const double minLabelHeight = 16; + + @override + void paint(Canvas canvas, Size size) { + final palette = StructurePalette(scheme); + canvas.clipRect(Offset.zero & size); + canvas.drawRect(Offset.zero & size, Paint()..color = scheme.surface); + canvas.drawRect( + Offset.zero & size, + Paint() + ..style = PaintingStyle.stroke + ..color = scheme.outlineVariant); + + final fill = Paint()..style = PaintingStyle.fill; + final outline = Paint() + ..style = PaintingStyle.stroke + ..strokeWidth = 1 + ..color = scheme.outline; + + for (final s in model.blocks) { + final b = s.block; + final rect = Rect.fromLTWH( + b.x * scale, b.y * scale, b.width * scale, b.height * scale); + fill.color = palette.of(s, coloring).withValues(alpha: 0.55); + canvas.drawRect(rect, fill); + if (b.fixed) { + _dashedRect(canvas, rect, outline); + } else { + canvas.drawRect(rect, outline); + } + if (rect.width >= minLabelWidth && rect.height >= minLabelHeight) { + _label(canvas, s.label, rect); + } + } + + final fold = model.layout.viewportHeight * scale; + if (fold > 0 && fold < size.height) { + final line = Paint() + ..style = PaintingStyle.stroke + ..strokeWidth = 1 + ..color = scheme.primary; + _dashedLine(canvas, Offset(0, fold), Offset(size.width, fold), line); + _text(canvas, 'first screen', Offset(4, fold + 2), size.width - 8, + scheme.primary); + } + } + + void _label(Canvas canvas, String text, Rect rect) => _text(canvas, text, + rect.topLeft + const Offset(3, 1), rect.width - 6, scheme.onSurface); + + void _text( + Canvas canvas, String text, Offset at, double maxWidth, Color color) { + final p = TextPainter( + text: TextSpan(text: text, style: TextStyle(fontSize: 10, color: color)), + textDirection: ui.TextDirection.ltr, + maxLines: 1, + ellipsis: '…', + )..layout(maxWidth: maxWidth < 1 ? 1 : maxWidth); + p.paint(canvas, at); + } + + void _dashedLine(Canvas canvas, Offset a, Offset b, Paint paint) { + final path = Path() + ..moveTo(a.dx, a.dy) + ..lineTo(b.dx, b.dy); + _dashed(canvas, path, paint); + } + + void _dashedRect(Canvas canvas, Rect r, Paint paint) => + _dashed(canvas, Path()..addRect(r), paint); + + void _dashed(Canvas canvas, Path path, Paint paint) { + for (final metric in path.computeMetrics()) { + for (var d = 0.0; d < metric.length; d += 8) { + canvas.drawPath(metric.extractPath(d, d + 4), paint); + } + } + } + + @override + bool shouldRepaint(StructurePainter old) => + !identical(old.model, model) || + old.scale != scale || + old.coloring != coloring || + old.scheme != scheme; +} diff --git a/lib/widgets/structure_view.dart b/lib/widgets/structure_view.dart new file mode 100644 index 0000000..22bc117 --- /dev/null +++ b/lib/widgets/structure_view.dart @@ -0,0 +1,227 @@ +import 'package:material_ui/material_ui.dart'; +import 'package:provider/provider.dart'; +import 'package:view_source_vibe/models/inspection/observed.dart'; +import 'package:view_source_vibe/models/inspection/page_inspection.dart'; +import 'package:view_source_vibe/services/derive/observed_text.dart'; +import 'package:view_source_vibe/services/derive/structure_view.dart'; +import 'package:view_source_vibe/services/html_service.dart'; +import 'package:view_source_vibe/widgets/structure_painter.dart'; + +/// The Structure tab: a wireframe of the rendered page, coloured by who owns +/// each box or by what it is, plus the heading outline. Everything comes from +/// [HtmlService.structureView]. +class StructureView extends StatefulWidget { + const StructureView({super.key}); + + @override + State createState() => _StructureViewState(); +} + +class _StructureViewState extends State { + StructureColoring _coloring = StructureColoring.owner; + + @override + Widget build(BuildContext context) { + final state = context + .select>((s) => s.structureView); + final hasBrowser = context + .select((s) => s.store.current.target is UrlTarget); + return switch (state) { + Pending(:final detail) => Center( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + const CircularProgressIndicator(), + const SizedBox(height: 12), + Text(detail ?? 'Measuring the page…', + style: TextStyle( + color: Theme.of(context).colorScheme.onSurfaceVariant)), + ], + ), + ), + Present(:final value) => _model(context, value), + _ => _unknown(context, state, hasBrowser), + }; + } + + Widget _unknown( + BuildContext context, Observed state, bool hasBrowser) { + final scheme = Theme.of(context).colorScheme; + return Center( + child: Padding( + padding: const EdgeInsets.all(24), + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Icon(Icons.dashboard_outlined, + size: 64, color: scheme.outlineVariant), + const SizedBox(height: 16), + Text(unknownText(state), + textAlign: TextAlign.center, + style: TextStyle( + color: state is Failed ? scheme.error : scheme.outline)), + if (hasBrowser) ...[ + const SizedBox(height: 12), + OutlinedButton( + onPressed: () => context.read().collectLayout(), + child: const Text('Try again'), + ), + ], + ], + ), + ), + ); + } + + Widget _model(BuildContext context, StructureModel model) { + final scheme = Theme.of(context).colorScheme; + final palette = StructurePalette(scheme); + return SingleChildScrollView( + physics: const AlwaysScrollableScrollPhysics(), + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Padding( + padding: const EdgeInsets.only(bottom: 4), + child: Text(model.measuredAt, + style: Theme.of(context).textTheme.bodySmall), + ), + for (final note in model.notes) + Padding( + padding: const EdgeInsets.only(bottom: 4), + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Icon(Icons.info_outline, size: 16, color: scheme.outline), + const SizedBox(width: 8), + Expanded(child: Text(note)), + ], + ), + ), + const SizedBox(height: 8), + Row( + children: [ + Expanded( + child: SegmentedButton( + showSelectedIcon: false, + segments: const [ + ButtonSegment( + value: StructureColoring.owner, label: Text('Owner')), + ButtonSegment( + value: StructureColoring.element, + label: Text('Element')), + ], + selected: {_coloring}, + onSelectionChanged: (s) => + setState(() => _coloring = s.first), + ), + ), + IconButton( + tooltip: 'Measure again', + icon: const Icon(Icons.refresh), + onPressed: () => context.read().collectLayout(), + ), + ], + ), + const SizedBox(height: 8), + Wrap( + spacing: 12, + runSpacing: 4, + children: [ + for (final (label, color) in palette.legend(_coloring)) + Row(mainAxisSize: MainAxisSize.min, children: [ + Container( + width: 12, + height: 12, + decoration: BoxDecoration( + color: color.withValues(alpha: 0.55), + border: Border.all(color: scheme.outline), + ), + ), + const SizedBox(width: 4), + Text(label, style: Theme.of(context).textTheme.bodySmall), + ]), + ], + ), + const SizedBox(height: 12), + LayoutBuilder(builder: (context, constraints) { + final scale = wireframeScale(model.layout, constraints.maxWidth); + return GestureDetector( + onTapUp: (d) => _openDetails( + context, + blockAt(model, d.localPosition.dx / scale, + d.localPosition.dy / scale)), + child: Semantics( + label: 'Page wireframe, ${model.blocks.length} blocks', + child: CustomPaint( + key: const Key('structure-wireframe'), + size: Size( + model.layout.width * scale, model.layout.height * scale), + painter: StructurePainter( + model: model, + scale: scale, + coloring: _coloring, + scheme: scheme), + ), + ), + ); + }), + const SizedBox(height: 16), + Text('Headings', style: Theme.of(context).textTheme.titleMedium), + const SizedBox(height: 4), + if (model.headings.isEmpty) + const Text('No headings.') + else + for (final h in model.headings) + Padding( + padding: EdgeInsets.only(left: (h.level - 1) * 16.0, top: 2), + child: Text(h.text.isEmpty ? '(empty h${h.level})' : h.text), + ), + ], + ), + ); + } + + void _openDetails(BuildContext context, StructureBlock? s) { + if (s == null) return; + final service = context.read(); + final b = s.block; + final line = b.id.isEmpty ? null : service.sourceLineOfId(b.id); + String n(double v) => v.round().toString(); + showModalBottomSheet( + context: context, + builder: (sheet) => SafeArea( + child: Padding( + padding: const EdgeInsets.all(16), + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text(s.label, style: Theme.of(sheet).textTheme.titleMedium), + const SizedBox(height: 8), + Text('${n(b.width)} × ${n(b.height)} px at ' + '(${n(b.x)}, ${n(b.y)})'), + Text('Kind: ${b.kind.name}'), + Text('Owner: ${s.owner.name}'), + if (s.service != null) Text('Service: ${s.service}'), + if (b.src.isNotEmpty) Text('Source: ${b.src}'), + Text('Contains ${b.descendants} elements'), + if (b.fixed) const Text('fixed'), + if (line != null) ...[ + const SizedBox(height: 8), + TextButton( + onPressed: () { + Navigator.of(sheet).pop(); + service.revealSourceLine(line); + }, + child: const Text('Show in source'), + ), + ], + ], + ), + ), + ), + ); + } +} diff --git a/test/collectors/collector_world_test.dart b/test/collectors/collector_world_test.dart index 6a5961e..df2bf48 100644 --- a/test/collectors/collector_world_test.dart +++ b/test/collectors/collector_world_test.dart @@ -8,6 +8,7 @@ void main() { final s = collectorUserScript(TargetPlatform.iOS); expect(s.contentWorld.name, 'vsv'); expect(s.source, startsWith('window.__vsv_isolated = true;')); + expect(s.source, contains('window.__vsv_layout')); expect(collectorWorld(TargetPlatform.macOS)?.name, 'vsv'); }); diff --git a/test/collectors/layout_payload_test.dart b/test/collectors/layout_payload_test.dart new file mode 100644 index 0000000..a82a425 --- /dev/null +++ b/test/collectors/layout_payload_test.dart @@ -0,0 +1,132 @@ +import 'dart:convert'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:view_source_vibe/models/inspection/observed.dart'; +import 'package:view_source_vibe/models/inspection/page_layout.dart'; +import 'package:view_source_vibe/services/collectors/layout_payload.dart'; + +final u = Uri.parse('https://a.test/x'); + +String payload( + {List>? blocks, + int total = 57, + Map extra = const {}}) => + jsonEncode({ + ...extra, + 'v': 1, + 'vw': 390, + 'vh': 800, + 'w': 390, + 'h': 5210, + 'total': total, + 'blocks': blocks ?? + [ + { + 't': 'header', + 'r': 'banner', + 'i': 'top', + 'c': 'bar wide', + 'x': 0, + 'y': 0, + 'w': 390, + 'h': 64, + 'd': 1, + 'n': 12, + 'k': 'landmark', + 's': '', + 'f': 1, + 'tx': '' + } + ], + }); + +PageLayout present(Observed o) => (o as Present).value; + +void main() { + test('the payload of Task 1 parses', () { + final l = present(parseLayoutPayload(payload(), pageUrl: u)); + expect(l.height, 5210); + expect(l.viewportWidth, 390); + expect(l.blocks.single.kind, LayoutKind.landmark); + expect(l.blocks.single.tag, 'header'); + expect(l.blocks.single.fixed, isTrue); + expect(l.blocks.single.area, 390 * 64); + expect(l.truncated, isTrue); // total 57, one block + }); + + test('an empty document is unavailable with the reason', () { + final o = parseLayoutPayload('{"v":1,"empty":true}', pageUrl: u); + expect((o as Unavailable).detail, 'the page has no layout'); + }); + + test('a sized page without qualifying boxes is present with no blocks', () { + final l = present(parseLayoutPayload(payload(blocks: [], total: 0), + pageUrl: u)); + expect(l.blocks, isEmpty); + expect(l.height, 5210); + }); + + test('a zero-size document is "no layout"', () { + for (final k in ['w', 'h']) { + final m = jsonDecode(payload(blocks: [], total: 0)) + as Map + ..[k] = 0; + final o = parseLayoutPayload(jsonEncode(m), pageUrl: u); + expect((o as Unavailable).detail, 'the page has no layout', + reason: k); + } + }); + + test('bad blocks are skipped, bad payloads fail', () { + final l = present(parseLayoutPayload( + payload(blocks: [ + {'k': 'weird', 'x': 0, 'y': 0, 'w': 5, 'h': 5}, + {'k': 'frame', 'x': 'a', 'y': 0, 'w': 5, 'h': 5}, + ], total: 2), + pageUrl: u)); + expect(l.blocks, isEmpty); + expect(parseLayoutPayload('not json', pageUrl: u), isA()); + final o = parseLayoutPayload(42, pageUrl: u) as Unavailable; + expect(o.reason, UnavailableReason.unsupportedPlatform); + }); + + test('a missing viewport or document size is Failed, not 0', () { + for (final key in ['vw', 'vh', 'w', 'h']) { + final m = jsonDecode(payload()) as Map..remove(key); + expect(parseLayoutPayload(jsonEncode(m), pageUrl: u), isA(), + reason: key); + } + }); + + test('a block without depth or descendants is dropped', () { + Map b(Map o) => + {'k': 'frame', 'x': 0, 'y': 0, 'w': 5, 'h': 5, 'd': 1, 'n': 2, ...o}; + final l = present(parseLayoutPayload( + payload(blocks: [ + b({}), + b({'d': null}), + b({'n': 'x'}), + ], total: 3), + pageUrl: u)); + expect(l.blocks.length, 1); + }); + + test('scanCapped is carried', () { + expect( + present(parseLayoutPayload(payload(extra: {'scanCapped': 1}), + pageUrl: u)) + .scanCapped, + isTrue); + expect(present(parseLayoutPayload(payload(), pageUrl: u)).scanCapped, + isFalse); + }); + + test('a relative frame source is resolved', () { + final l = present(parseLayoutPayload( + payload(blocks: [ + {'k': 'frame', 'x': 0, 'y': 0, 'w': 5, 'h': 5, 'd': 1, 'n': 0, 's': '/embed'}, + ], total: 1), + pageUrl: u)); + expect(l.blocks.single.src, 'https://a.test/embed'); + }); +} diff --git a/test/collectors/vsv_layout_js_test.dart b/test/collectors/vsv_layout_js_test.dart new file mode 100644 index 0000000..f8eb2f2 --- /dev/null +++ b/test/collectors/vsv_layout_js_test.dart @@ -0,0 +1,172 @@ +import 'dart:convert'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:view_source_vibe/services/collectors/vsv_layout_js.dart'; + +import '../support/collector_node.dart'; + +const _prelude = r''' +var __scrollX = 0, __scrollY = 0, __docW = 412, __docH = 5210; +function mk(tag, o) { + o = o || {}; + var kids = []; + for (var i = 0; i < (o.kids || 0); i++) kids.push({}); + return { + tagName: tag.toUpperCase(), id: o.id || '', className: o.cls || '', + src: o.src || '', currentSrc: o.currentSrc || '', textContent: o.text || '', + pos: o.pos || 'static', vis: o.vis || 'visible', + getAttribute: function(n) { return n === 'role' ? (o.role || null) : null; }, + getBoundingClientRect: function() { + var r = o.rect || [0, 0, 100, 100]; + return { left: r[0], top: r[1], width: r[2], height: r[3], + right: r[0] + r[2], bottom: r[1] + r[3] }; + }, + getElementsByTagName: function() { return kids; }, + parentNode: null + }; +} +var body = mk('body'); +var __els = []; +function add(e) { e.parentNode = body; __els.push(e); return e; } +var window = { scrollX: 0, scrollY: 0, pageXOffset: 0, pageYOffset: 0, + innerWidth: 412, innerHeight: 823 }; +var document = { + body: body, + scrollingElement: { scrollWidth: 412, scrollHeight: 5210 }, + documentElement: { scrollWidth: 412, scrollHeight: 5210 } +}; +body.getElementsByTagName = function() { return __els; }; +function getComputedStyle(e) { return { position: e.pos, visibility: e.vis }; } +'''; + +Future?> layout(String dom) async { + final out = await runJs([ + _prelude, + dom, + vsvLayoutJs, + 'console.log(window.__vsv_layout());', + ].join('\n')); + return out == null ? null : jsonDecode(out) as Map; +} + +void main() { + test('landmarks, frames, headings and big blocks are measured', () async { + final r = await layout(r''' +add(mk('header', {id: 'top', cls: 'site-header dark extra', role: 'banner', rect: [0, 0, 412, 64], kids: 31})); +add(mk('h1', {text: ' Welcome to the\n shop ', rect: [0, 70, 412, 40]})); +add(mk('iframe', {src: 'https://ads.test/slot', rect: [0, 120, 300, 250]})); +add(mk('div', {rect: [0, 400, 50, 50], kids: 3})); +add(mk('div', {rect: [0, 500, 412, 900], kids: 30})); +add(mk('nav', {vis: 'hidden', rect: [0, 2000, 412, 50]})); +'''); + if (r == null) return markTestSkipped('node not installed'); + expect((r['blocks'] as List).map((b) => '${b['t']}:${b['k']}'), + ['header:landmark', 'h1:heading', 'iframe:frame', 'div:block']); + expect(r['blocks'][0]['r'], 'banner'); + expect(r['blocks'][0]['i'], 'top'); + expect(r['blocks'][0]['c'], 'site-header dark'); + expect(r['blocks'][0]['n'], 31); + expect(r['blocks'][0]['d'], 1); + expect(r['blocks'][1]['tx'], 'Welcome to the shop'); + expect(r['blocks'][2]['s'], 'https://ads.test/slot'); + expect(r['total'], 4); + expect([r['vw'], r['vh'], r['w'], r['h']], [412, 823, 412, 5210]); + }); + + test('coordinates are page coordinates', () async { + final r = await layout(r''' +window.scrollY = 500; window.pageYOffset = 500; +add(mk('section', {rect: [10, -100, 200, 300]})); +'''); + if (r == null) return markTestSkipped('node not installed'); + expect(r['blocks'][0]['x'], 10); + expect(r['blocks'][0]['y'], 400); + expect(r['blocks'][0]['h'], 300); + }); + + test('fixed elements are flagged and data: sources dropped', () async { + final r = await layout(r''' +add(mk('div', {pos: 'fixed', role: 'dialog', rect: [0, 0, 412, 200]})); +add(mk('img', {src: 'data:image/png;base64,AAAA', rect: [0, 300, 200, 200]})); +add(mk('img', {currentSrc: 'https://cdn.test/a.jpg', rect: [0, 600, 200, 200]})); +add(mk('img', {src: 'https://cdn.test/small.png', rect: [0, 900, 20, 20]})); +'''); + if (r == null) return markTestSkipped('node not installed'); + final b = r['blocks'] as List; + expect(b[0]['f'], 1); + expect(b[1]['f'], 0); + expect(b[1]['s'], ''); + expect(b[2]['s'], 'https://cdn.test/a.jpg'); + expect(b.length, 3); + }); + + test('more than 400 qualifying elements are capped', () async { + final r = await layout(r''' +for (var i = 0; i < 450; i++) add(mk('section', {rect: [0, i * 10, 100, 10]})); +'''); + if (r == null) return markTestSkipped('node not installed'); + expect((r['blocks'] as List).length, 400); + expect(r['total'], 450); + }); + + test('blocks beyond the document width are skipped (carousel slides)', + () async { + final r = await layout(r''' +add(mk('section', {rect: [0, 100, 412, 300]})); +add(mk('section', {rect: [412, 100, 412, 300]})); +add(mk('section', {rect: [900, 100, 412, 300]})); +'''); + if (r == null) return markTestSkipped('node not installed'); + expect((r['blocks'] as List).length, 1); + expect(r['total'], 1); + }); + + test('the document height grows to the lowest block (body scrolls)', + () async { + final r = await layout(r''' +document.scrollingElement.scrollHeight = 800; +add(mk('section', {rect: [0, 100, 412, 300]})); +add(mk('section', {rect: [0, 5000, 412, 300]})); +'''); + if (r == null) return markTestSkipped('node not installed'); + expect(r['h'], 5300); + expect((r['blocks'] as List).length, 2); + }); + + test('scanCapped is set only when the element scan hit its limit', () async { + final few = await layout(r''' +add(mk('section', {rect: [0, 0, 100, 100]})); +'''); + if (few == null) return markTestSkipped('node not installed'); + expect(few['scanCapped'], 0); + final many = await layout(r''' +for (var i = 0; i < 20001; i++) add(mk('span', {rect: [0, 0, 1, 1]})); +'''); + expect(many!['scanCapped'], 1); + }); + + test('example.com: the h1 and the plain paragraphs are measured', () async { + final r = await layout(r''' +document.scrollingElement.scrollWidth = 1335; +document.scrollingElement.scrollHeight = 752; +window.innerWidth = 1352; window.innerHeight = 628; +add(mk('div', {rect: [376, 150, 600, 300], kids: 6})); +add(mk('h1', {text: 'Example Domain', rect: [376, 170, 600, 38]})); +add(mk('p', {rect: [376, 230, 600, 60]})); +add(mk('p', {rect: [376, 310, 600, 20], kids: 1})); +'''); + if (r == null) return markTestSkipped('node not installed'); + expect((r['blocks'] as List).map((b) => '${b['t']}:${b['k']}'), + ['h1:heading']); + expect(r['tx'] ?? r['blocks'][0]['tx'], 'Example Domain'); + }); + + test('a document without layout is empty', () async { + final r = await layout(r''' +document.scrollingElement.scrollHeight = 0; +add(mk('section')); +'''); + if (r == null) return markTestSkipped('node not installed'); + expect(r, {'v': 1, 'empty': true}); + }); +} diff --git a/test/derive/structure_view_test.dart b/test/derive/structure_view_test.dart new file mode 100644 index 0000000..b897e9c --- /dev/null +++ b/test/derive/structure_view_test.dart @@ -0,0 +1,234 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:view_source_vibe/models/inspection/observed.dart'; +import 'package:view_source_vibe/models/inspection/page_inspection.dart'; +import 'package:view_source_vibe/models/inspection/page_layout.dart'; +import 'package:view_source_vibe/services/derive/structure_view.dart'; + +import '../fixtures/inspection/local_file.dart'; + +LayoutBlock block( + String tag, { + LayoutKind kind = LayoutKind.block, + String role = '', + String id = '', + String classes = '', + String src = '', + double x = 0, + double y = 0, + double w = 100, + double h = 100, + bool fixed = false, +}) => + LayoutBlock( + tag: tag, + role: role, + id: id, + classes: classes, + src: src, + text: '', + x: x, + y: y, + width: w, + height: h, + depth: 1, + descendants: 0, + kind: kind, + fixed: fixed, + ); + +LayoutBlock frame(String src, {double w = 100, double h = 100}) => + block('iframe', kind: LayoutKind.frame, src: src, w: w, h: h); + +LayoutBlock heading(String tag) => block(tag, kind: LayoutKind.heading, h: 20); + +PageLayout layout( + {double w = 1000, + double h = 4000, + List blocks = const [], + int? total}) => + PageLayout( + viewportWidth: w, + viewportHeight: 800, + width: w, + height: h, + totalBlocks: total ?? blocks.length, + blocks: blocks); + +PageInspection pageWith({ + String url = 'https://shop.test/', + List blocks = const [], + double w = 1000, + double h = 4000, + int? total, +}) => + PageInspection( + generation: 1, + target: UrlTarget(Uri.parse(url)), + server: const Unavailable(UnavailableReason.notApplicable), + browser: const Unavailable(UnavailableReason.notApplicable), + file: const Unavailable(UnavailableReason.notApplicable), + layout: Present(layout(w: w, h: h, blocks: blocks, total: total), + source: DataSource.browser), + ); + +StructureModel present(Observed o) { + expect(o, isA>()); + return (o as Present).value; +} + +void main() { + test('frames are owned by who serves them', () { + final m = present(deriveStructure(pageWith(blocks: [ + frame('https://googleads.g.doubleclick.net/slot', w: 300, h: 250), + frame('https://widgets.shop.test/chat'), + frame('https://maps.other.test/embed'), + block('main', kind: LayoutKind.landmark), + ]))); + expect(m.blocks.map((b) => b.owner).toSet(), { + BlockOwner.advertising, + BlockOwner.ownFrame, + BlockOwner.thirdParty, + BlockOwner.page + }); + expect( + m.blocks.firstWhere((b) => b.owner == BlockOwner.advertising).service, + isNotNull); + }); + + test('labels', () { + final m = present(deriveStructure(pageWith(blocks: [ + block('header', + kind: LayoutKind.landmark, id: 'top', classes: 'site-header dark'), + frame('https://ads.other.test/x'), + heading('h2'), + ]))); + final labels = m.blocks.map((b) => b.label).toSet(); + expect(labels, containsAll(['header#top.site-header', 'h2'])); + expect(labels.any((l) => l == 'iframe · ads.other.test'), isTrue); + }); + + test('third-party share of the page area', () { + final m = present(deriveStructure(pageWith(blocks: [ + frame('https://maps.other.test/embed', w: 500, h: 800), + block('main', kind: LayoutKind.landmark), + ]))); + expect(m.thirdPartyShare, closeTo(0.1, 1e-9)); + expect(m.notes, contains('Third-party frames cover 10% of the page.')); + }); + + test('outline notes', () { + final m = present(deriveStructure(pageWith(blocks: [ + heading('h1'), + heading('h1'), + heading('h2'), + heading('h4'), + ]))); + expect( + m.notes, + containsAll([ + 'No main landmark.', + '2 h1 headings (one is usual).', + 'Heading levels skip from h2 to h4.' + ])); + expect(m.headings.map((h) => h.level), [1, 1, 2, 4]); + expect(m.landmarks, isEmpty); + }); + + test('truncation note and landmarks', () { + final m = present(deriveStructure(pageWith(total: 500, blocks: [ + block('main', kind: LayoutKind.landmark), + block('div', kind: LayoutKind.landmark, role: 'navigation'), + heading('h1'), + ]))); + expect(m.notes, contains('Only the first 3 of 500 blocks are drawn.')); + expect(m.notes, isNot(contains('No main landmark.'))); + expect(m.landmarks, ['nav', 'main']); + }); + + test('paint order: area descending, fixed last', () { + final m = present(deriveStructure(pageWith(blocks: [ + block('div', w: 10, h: 10), + block('div', w: 500, h: 500), + block('div', w: 900, h: 900, fixed: true), + ]))); + expect(m.blocks.map((b) => b.block.area), [250000, 100, 810000]); + }); + + test('wireframeScale', () { + expect(wireframeScale(layout(w: 400, h: 2000), 400), 1.0); + expect(wireframeScale(layout(w: 1200, h: 2400), 400), closeTo(1 / 3, 1e-9)); + expect(wireframeScale(layout(w: 400, h: 100000), 400), closeTo(0.06, 1e-9)); + }); + + test('blockAt picks the smallest box under the point', () { + final m = present(deriveStructure(pageWith(w: 400, h: 2000, blocks: [ + block('main', kind: LayoutKind.landmark, w: 400, h: 2000), + block('article', y: 100, w: 400, h: 600), + block('div', w: 400, h: 60, fixed: true), + ]))); + expect(blockAt(m, 10, 30)?.block.tag, 'div'); + expect(blockAt(m, 10, 300)?.block.tag, 'article'); + expect(blockAt(m, 10, 1500)?.block.tag, 'main'); + expect(blockAt(m, 900, 10), isNull); + }); + + test('lineOfId', () { + expect(lineOfId('\n
\n', 'top'), 2); + expect(lineOfId("

", 'x'), 1); + expect(lineOfId('

', 'x'), isNull); + expect(lineOfId('

', ''), isNull); + }); + + test('a heading tag that is not h1..h6 does not throw', () { + final m = present(deriveStructure(pageWith(blocks: [ + block('', kind: LayoutKind.heading), + heading('h2'), + ]))); + expect(m.headings.map((h) => h.level), [1, 2]); + }); + + test('the measuring viewport is stated', () { + final m = present(deriveStructure(pageWith(w: 390))); + expect(m.measuredAt, 'Measured at 390\u00d7800'); + }); + + test('a page with nothing to draw says so', () { + final m = present(deriveStructure(pageWith())); + expect( + m.notes, + contains('Nothing on this page is large or marked up enough to draw: ' + 'no landmarks, headings, frames, media or large blocks.')); + expect(present(deriveStructure(pageWith(blocks: [heading('h1')]))).notes + .join(), isNot(contains('Nothing on this page'))); + }); + + test('a capped scan is noted', () { + final p = pageWith(blocks: [heading('h1')]); + final l = (p.layout as Present).value; + final capped = p.copyWith( + layout: Present( + PageLayout( + viewportWidth: l.viewportWidth, + viewportHeight: l.viewportHeight, + width: l.width, + height: l.height, + totalBlocks: l.totalBlocks, + blocks: l.blocks, + scanCapped: true), + source: DataSource.browser)); + expect(present(deriveStructure(capped)).notes, contains( + 'Only the first 20000 elements were measured; landmark and ' + 'heading notes may be incomplete.')); + expect(present(deriveStructure(p)).notes.join(), + isNot(contains('20000'))); + }); + + test('a local file has no structure view', () { + expect(deriveStructure(localFile()), isA>()); + }); + + test('other layout states pass through', () { + final p = pageWith().copyWith(layout: const Pending()); + expect(deriveStructure(p), isA>()); + }); +} diff --git a/test/inspection_export_service_test.dart b/test/inspection_export_service_test.dart index fa941ea..fdbe1e9 100644 --- a/test/inspection_export_service_test.dart +++ b/test/inspection_export_service_test.dart @@ -9,6 +9,8 @@ import 'package:view_source_vibe/services/derive/inspection_summary.dart'; import 'package:view_source_vibe/services/html_service.dart'; import 'package:view_source_vibe/services/inspection_store.dart'; import 'package:view_source_vibe/services/inspection_export_service.dart'; +import 'package:view_source_vibe/models/inspection/observed.dart'; +import 'package:view_source_vibe/models/inspection/page_layout.dart'; import 'fixtures/inspection/builders.dart' as fx; void main() { @@ -189,6 +191,69 @@ void main() { expect(text, contains('24')); }); + test('the export has the structure', () { + final store = InspectionStore(); + final gen = store.beginUrl(Uri.parse('https://example.com/')); + store.applyLayout( + gen, + const Present( + PageLayout( + viewportWidth: 400, + viewportHeight: 800, + width: 400, + height: 1200, + totalBlocks: 2, + blocks: [ + LayoutBlock( + tag: 'header', + role: '', + id: 'top', + classes: 'bar', + src: '', + text: '', + x: 0, + y: 0, + width: 400, + height: 80, + depth: 1, + descendants: 4, + kind: LayoutKind.landmark, + fixed: false), + LayoutBlock( + tag: 'h1', + role: '', + id: '', + classes: '', + src: '', + text: 'Hello', + x: 0, + y: 100, + width: 400, + height: 30, + depth: 2, + descendants: 0, + kind: LayoutKind.heading, + fixed: false), + ]), + source: DataSource.browser)); + final htmlService = HtmlService(store: store); + addTearDown(htmlService.dispose); + final archive = ZipDecoder().decodeBytes( + InspectionExportService().buildPackageBytes(htmlService)); + final json = jsonDecode(utf8.decode( + archive.findFile('analysis/structure.json')!.readBytes()!)) + as Map; + expect(json['status'], 'present'); + final v = json['value'] as Map; + expect(v['page'], {'w': 400.0, 'h': 1200.0}); + expect(v['totalBlocks'], 2); + expect((v['blocks'] as List).first['tag'], 'header'); + expect((v['blocks'] as List).first['owner'], 'page'); + expect(v['headings'], [ + {'level': 1, 'text': 'Hello'} + ]); + }); + group('source/diff.patch', () { Map manifestAndFiles( String? serverText, String? domText, Set names) { diff --git a/test/models/app_tab_test.dart b/test/models/app_tab_test.dart index 4c7ff56..0439ca9 100644 --- a/test/models/app_tab_test.dart +++ b/test/models/app_tab_test.dart @@ -13,7 +13,7 @@ void main() { server: true); expect(tabs.map((t) => t.label), [ 'Browser', 'Metadata', 'Media', 'Services', 'Reader', // - 'DOM Tree', 'Source', // + 'Structure', 'DOM Tree', 'Source', // 'Size', 'Headers', 'Timeline', 'Probe', // 'Privacy', 'Sovereignty', 'Security', 'Permissions', 'Cookies', ]); diff --git a/test/reader_service_test.dart b/test/reader_service_test.dart index df52bd8..0a9e990 100644 --- a/test/reader_service_test.dart +++ b/test/reader_service_test.dart @@ -219,11 +219,11 @@ void main() { await service.loadFile(_htmlUrlFile()); - // Page: Browser, Metadata, Media, Services, Reader; Code: DOM Tree, - // Source. + // Page: Browser, Metadata, Media, Services, Reader; Code: Structure, + // DOM Tree, Source. expect(service.browserTabIndex, 0); expect(service.readerTabIndex, 4); - expect(service.sourceTabIndex, 6); + expect(service.sourceTabIndex, 7); }); test('the layout does not depend on the browser-first setting', () async { @@ -235,7 +235,7 @@ void main() { expect(service.browserTabIndex, 0); expect(service.readerTabIndex, 4); - expect(service.sourceTabIndex, 6); + expect(service.sourceTabIndex, 7); }); test('non-HTML files have no Reader tab offset', () async { diff --git a/test/services/html_service_browser_sync_test.dart b/test/services/html_service_browser_sync_test.dart index 4b11972..12709e1 100644 --- a/test/services/html_service_browser_sync_test.dart +++ b/test/services/html_service_browser_sync_test.dart @@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart' show PermissionResourceType; import 'package:flutter_test/flutter_test.dart'; +import 'package:view_source_vibe/models/app_tab.dart'; import 'package:view_source_vibe/services/derive/network_quiet.dart'; import 'package:view_source_vibe/models/inspection/browser_snapshot.dart'; import 'package:view_source_vibe/models/inspection/observed.dart'; @@ -333,6 +334,72 @@ void main() { }); }); + group('structure layout timing', () { + const layoutJson = '{"v":1,"vw":1,"vh":1,"w":10,"h":20,"total":0,' + '"blocks":[{"k":"frame","x":0,"y":0,"w":5,"h":5,"d":1,"n":0}]}'; + + HtmlService makeWithLayout(NetworkQuietPolicy poll, List calls) { + final s = HtmlService( + store: store, + serverFetcher: fetcher, + browserLoader: loader, + metadataExtractor: (html, baseUrl, {headers}) async => {'title': html}, + browserCollect: (url) => collect(url), + layoutCollect: (url) async { + calls.add(url); + return layoutJson; + }, + nativeCookies: (url) => native(url), + browserPlatform: BrowserPlatform.macos, + browserPoll: poll, + lateCollectDelay: const Duration(milliseconds: 10), + spaSettleDelay: Duration.zero, + ); + s.setAppSettings(AppSettings()..useBrowserByDefault = true); + return s; + } + + test('the tab says it waits while the page settles', () async { + service.dispose(); + final calls = []; + service = makeWithLayout(once(60), calls); + await loadAndAnswer('https://a.test/'); + await service.syncWebViewState('https://a.test/'); + final l = store.current.layout as Pending; + expect(l.detail, 'waiting for the page to settle'); + await Future.delayed(const Duration(milliseconds: 150)); + await pumpEventQueue(); + expect(calls, hasLength(1)); + }); + + test('activating the Structure tab re-measures once settled', () async { + service.dispose(); + final calls = []; + service = makeWithLayout(once(20), calls); + await loadAndAnswer('https://a.test/'); + await service.syncWebViewState('https://a.test/'); + await Future.delayed(const Duration(milliseconds: 100)); + await pumpEventQueue(); + expect(store.current.browserSettled, isTrue); + expect(calls, hasLength(1)); + service.setActiveTabIndex(service.tabIndexOf(AppTab.structure)); + await pumpEventQueue(); + expect(calls, hasLength(2)); + }); + + test('activating the Structure tab before settling does not measure', + () async { + service.dispose(); + final calls = []; + service = makeWithLayout(once(1000), calls); + await loadAndAnswer('https://a.test/'); + await service.syncWebViewState('https://a.test/'); + service.setActiveTabIndex(service.tabIndexOf(AppTab.structure)); + await pumpEventQueue(); + expect(calls, isEmpty); + }); + }); + group('refreshes', () { test( 'a refresh merges: startTime/duration of the first sync survive ' diff --git a/test/services/html_service_layout_test.dart b/test/services/html_service_layout_test.dart new file mode 100644 index 0000000..6099724 --- /dev/null +++ b/test/services/html_service_layout_test.dart @@ -0,0 +1,93 @@ +import 'dart:async'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:view_source_vibe/models/inspection/observed.dart'; +import 'package:view_source_vibe/models/inspection/page_layout.dart'; +import 'package:view_source_vibe/services/html_service.dart'; +import 'package:view_source_vibe/services/inspection_store.dart'; + +import 'html_service_lifecycle_test.dart' show FakeBrowserLoader; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + HtmlService make(InspectionStore store, Future Function(String) c) => + HtmlService( + store: store, + browserLoader: FakeBrowserLoader(), + layoutCollect: c, + metadataExtractor: (html, baseUrl, {headers}) async => {}, + ); + + test('collectLayout stores the parsed layout', () async { + final store = InspectionStore()..beginUrl(Uri.parse('https://a.test/')); + final s = make( + store, + (u) async => + '{"v":1,"vw":1,"vh":1,"w":10,"h":20,"total":0,"blocks":' + '[{"k":"frame","x":0,"y":0,"w":5,"h":5,"d":1,"n":0}]}'); + await s.collectLayout(); + expect(store.current.layout, isA>()); + s.dispose(); + }); + + test('a throwing evaluation becomes Failed', () async { + final store = InspectionStore()..beginUrl(Uri.parse('https://a.test/')); + final s = make(store, (u) async => throw StateError('boom')); + await s.collectLayout(); + expect(store.current.layout, isA>()); + s.dispose(); + }); + + test('without a browser the layout needs the browser', () async { + final store = InspectionStore()..beginUrl(Uri.parse('https://a.test/')); + final s = HtmlService( + store: store, + browserLoader: FakeBrowserLoader(), + metadataExtractor: (html, baseUrl, {headers}) async => {}); + await s.collectLayout(); + final l = store.current.layout as Unavailable; + expect(l.detail, 'needs the browser'); + s.dispose(); + }); + + test('a result after navigating on is dropped', () async { + final store = InspectionStore()..beginUrl(Uri.parse('https://a.test/')); + final s = make(store, (u) async { + store.beginUrl(Uri.parse('https://b.test/')); + return '{"v":1,"empty":true}'; + }); + await s.collectLayout(); + expect((store.current.layout as Unavailable).detail, + 'not measured yet'); + s.dispose(); + }); + + const present1 = '{"v":1,"vw":1,"vh":1,"w":10,"h":20,"total":0,"blocks":' + '[{"k":"frame","x":0,"y":0,"w":5,"h":5,"d":1,"n":0}]}'; + + test('a re-measure keeps the old drawing instead of flashing Pending', + () async { + final store = InspectionStore()..beginUrl(Uri.parse('https://a.test/')); + final seen = []; + store.addListener(() => seen.add(store.current.layout.runtimeType)); + final s = make(store, (u) async => present1); + await s.collectLayout(); + seen.clear(); + await s.collectLayout(); + expect(seen.any((t) => t.toString().startsWith('Pending')), isFalse); + expect(store.current.layout, isA>()); + s.dispose(); + }); + + test('a first measure is Pending while it runs', () async { + final store = InspectionStore()..beginUrl(Uri.parse('https://a.test/')); + final gate = Completer(); + final s = make(store, (u) => gate.future); + final f = s.collectLayout(); + expect(store.current.layout, isA>()); + gate.complete(present1); + await f; + s.dispose(); + }); +} diff --git a/test/services/inspection_store_test.dart b/test/services/inspection_store_test.dart index 43ab16c..dab65fe 100644 --- a/test/services/inspection_store_test.dart +++ b/test/services/inspection_store_test.dart @@ -5,6 +5,7 @@ import 'package:view_source_vibe/models/inspection/browser_snapshot.dart'; import 'package:view_source_vibe/models/inspection/file_snapshot.dart'; import 'package:view_source_vibe/models/inspection/observed.dart'; import 'package:view_source_vibe/models/inspection/page_inspection.dart'; +import 'package:view_source_vibe/models/inspection/page_layout.dart'; import 'package:view_source_vibe/models/inspection/permission_request.dart'; import 'package:view_source_vibe/models/inspection/server_snapshot.dart'; import 'package:view_source_vibe/services/inspection_store.dart'; @@ -540,6 +541,24 @@ void main() { }); }); + test('a layout for an earlier navigation is dropped', () { + final store = InspectionStore(); + final gen1 = store.beginUrl(Uri.parse('https://a.test/')); + store.beginUrl(Uri.parse('https://b.test/')); + store.applyLayout( + gen1, + const Present( + PageLayout( + viewportWidth: 1, + viewportHeight: 1, + width: 1, + height: 1, + totalBlocks: 0, + blocks: []), + source: DataSource.browser)); + expect(store.current.layout, isA>()); + }); + group('frames and site storage', () { const gone = Unavailable( UnavailableReason.notReportedByPlatform, 'not collected'); diff --git a/test/support/collector_node.dart b/test/support/collector_node.dart index ccbd7c3..9b97747 100644 --- a/test/support/collector_node.dart +++ b/test/support/collector_node.dart @@ -56,3 +56,24 @@ Future?> runCollector({ dir.deleteSync(recursive: true); } } + +/// Runs [source] under Node and returns what it printed to stdout (trimmed). +/// Returns null when Node is not installed. +Future runJs(String source) async { + try { + await Process.run('node', ['--version']); + } on ProcessException { + return null; + } + final dir = Directory.systemTemp.createTempSync('vsv_js'); + try { + final file = File('${dir.path}/run.js')..writeAsStringSync(source); + final r = await Process.run('node', [file.path]); + if (r.exitCode != 0) { + throw StateError('js run failed: ${r.stderr}'); + } + return (r.stdout as String).trim(); + } finally { + dir.deleteSync(recursive: true); + } +} diff --git a/test/widgets/structure_data_test.dart b/test/widgets/structure_data_test.dart new file mode 100644 index 0000000..f975d76 --- /dev/null +++ b/test/widgets/structure_data_test.dart @@ -0,0 +1,111 @@ +import 'package:material_ui/material_ui.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:view_source_vibe/models/inspection/observed.dart'; +import 'package:view_source_vibe/models/inspection/page_inspection.dart'; +import 'package:view_source_vibe/models/inspection/page_layout.dart'; +import 'package:view_source_vibe/services/html_service.dart'; +import 'package:view_source_vibe/services/inspection_store.dart'; +import 'package:view_source_vibe/widgets/structure_view.dart'; + +import '../fixtures/inspection/full_page.dart'; +import '../fixtures/inspection/local_file.dart'; +import '../fixtures/inspection/pump.dart'; + +LayoutBlock _block(String tag, LayoutKind kind, + {String id = '', + String src = '', + String text = '', + double x = 0, + double y = 0, + double w = 1000, + double h = 100}) => + LayoutBlock( + tag: tag, + role: '', + id: id, + classes: '', + src: src, + text: text, + x: x, + y: y, + width: w, + height: h, + depth: 1, + descendants: 3, + kind: kind, + fixed: false); + +PageInspection pageWithLayout() => fullPage().copyWith( + layout: Present( + PageLayout( + viewportWidth: 1000, + viewportHeight: 800, + width: 1000, + height: 2000, + totalBlocks: 4, + blocks: [ + _block('header', LayoutKind.landmark, id: 'top'), + _block('h1', LayoutKind.heading, + text: 'Welcome to the shop', y: 120, h: 40), + _block('main', LayoutKind.landmark, y: 200, h: 500), + _block('iframe', LayoutKind.frame, + src: 'https://ads.doubleclick.net/x', + x: 200, + y: 800, + w: 600, + h: 400), + ]), + source: DataSource.browser, + )); + +void main() { + late HtmlService service; + + Future show(WidgetTester tester, PageInspection p) async { + final store = seedStore(InspectionStore(), p); + // seedStore does not carry the layout. + store.applyLayout(store.current.generation, p.layout); + service = makeService(store); + addTearDown(service.dispose); + await pumpWithProviders(tester, + service: service, child: const StructureView()); + await tester.pump(); + } + + Offset wireframeOrigin(WidgetTester tester) => + tester.getTopLeft(find.byKey(const Key('structure-wireframe'))); + + testWidgets('the tab draws the wireframe and the outline', (tester) async { + await show(tester, pageWithLayout()); + expect(find.byKey(const Key('structure-wireframe')), findsOneWidget); + expect(find.text('Owner'), findsOneWidget); + expect(find.text('Welcome to the shop'), findsOneWidget); + expect(find.textContaining('Third-party frames cover'), findsOneWidget); + }); + + testWidgets('a tap on a block opens its details', (tester) async { + await show(tester, pageWithLayout()); + await tester.tapAt(wireframeOrigin(tester) + const Offset(20, 20)); + await tester.pumpAndSettle(); + expect(find.textContaining('header#top'), findsOneWidget); + expect(find.textContaining('1000 × 100 px'), findsOneWidget); + }); + + testWidgets('without a browser the tab says why', (tester) async { + await show(tester, localFile()); + expect(find.textContaining('only for pages loaded in the browser'), + findsOneWidget); + expect(find.byKey(const Key('structure-wireframe')), findsNothing); + }); + + testWidgets('a measuring page shows progress', (tester) async { + await show(tester, fullPage().copyWith(layout: const Pending())); + expect(find.text('Measuring the page…'), findsOneWidget); + }); + + testWidgets('a page still loading says it waits', (tester) async { + await show(tester, + fullPage().copyWith(layout: const Pending('waiting for the page to settle'))); + expect(find.text('waiting for the page to settle'), findsOneWidget); + }); +}