wxMaxima
Loading...
Searching...
No Matches
Data Structures | Macros | Functions
test_WorksheetClipboard.cpp File Reference

Safety net for the multi-format clipboard / drag-and-drop payload of Worksheet. More...

#include <wx/app.h>
#include <wx/bitmap.h>
#include <wx/dataobj.h>
#include <wx/dcmemory.h>
#include <wx/dir.h>
#include <wx/filename.h>
#include <wx/frame.h>
#include <wx/image.h>
#include <wx/log.h>
#include <wx/mstream.h>
#include <wx/wfstream.h>
#include <wx/zipstrm.h>
#include <wx/xml/xml.h>
#include "Configuration.h"
#include "Dirstructure.h"
#include "MathParser.h"
#include "worksheet/ClipboardContents.h"
#include "worksheet/Worksheet.h"
#include "worksheet/WorksheetExport.h"
#include "cells/GroupCell.h"
#include "cells/CellList.h"
#include "cells/ImgCell.h"
#include "AttachmentSource.h"
#include "WXMXformat.h"
#include <cstdlib>
#include <string>
#include <vector>
#include <unistd.h>
#include <catch2/catch.hpp>
Include dependency graph for test_WorksheetClipboard.cpp:

Data Structures

class  TestApp
 

Macros

#define WXM_CORPUS_DIR   "."
 
#define CATCH_CONFIG_RUNNER
 

Functions

 SCENARIO ("The whole-cell (cut/copy-cells) clipboard object is well-formed")
 
 SCENARIO ("The selection (copy-as-output) clipboard object is well-formed")
 
 SCENARIO ("Clipboard formats are only rendered when they are asked for " "(GH #2030)")
 
 SCENARIO ("What was copied is pasted, even if the worksheet has changed since " "(GH #2030)")
 
 SCENARIO ("The data handed over when the worksheet closes no longer needs it " "(GH #2030)")
 
 SCENARIO ("Copy as HTML renders a self-contained document (GH #2265/#2266/#2267)")
 
 SCENARIO ("SelectionToSelfContainedHTML rejects a null range (GH #2265)")
 
 wxDECLARE_APP (TestApp)
 
 SCENARIO ("The bitmap is also offered as PNG bytes (wxQt)")
 GH #2444 follow-up: a bitmap inside a composite data object reaches nobody on wxQt.
 
 SCENARIO ("Copied cells round-trip as a self-contained XML document (GH #2474)")
 
 SCENARIO ("The clipboard's XML flavour carries the copied cells (GH #2474)")
 
 SCENARIO ("AttachmentSource reads files from memory")
 
 SCENARIO ("AttachmentSource reads files from a .wxmx archive")
 
 SCENARIO ("AttachmentSource reads a file bigger than one read chunk")
 
int main (int argc, char **argv)
 

Detailed Description

Safety net for the multi-format clipboard / drag-and-drop payload of Worksheet.

When wxMaxima copies (or cuts, or drag-exports) a selection it advertises the same content under many flavors at once - the .wxm batch code, MathML (under two mime types), RTF (under two mime types), plain text, and a bitmap. Those live inside one wxDataObjectComposite / CompositeDataObject. A composite whose children collide on a wxDataFormat, or whose children carry no retrievable data, is a latent clipboard bug (and trips a wxWidgets assertion when it is handed to the real clipboard).

Worksheet::CreateSelectionDataObject() and Worksheet::CreateCellsDataObject() are the exact builders Copy()/CopyCells() use, factored out so the composite can be inspected without opening the (headless-unfriendly) system clipboard. This test builds them over a real document and pins the invariants:

GH #2030: only the .wxm and the plain-text flavors are made when the copy is made. Everything else (RTF, MathML, the bitmap, the SVG, ...) is rendered from a private copy of the selection only when a program asks for it, which is why every flavor is offered now instead of only the ones a config setting asked for. That is pinned here too: offering renders nothing, a payload offered under several names renders once, a copy survives the worksheet changing under it, and the data handed over when the worksheet closes refers to neither the cells nor the configuration.

GH #2264: "Copy as RTF" pasted into MS Word was silently ignored. RTF was only advertised under the MIME-style names "application/rtf"/"text/rtf" - what GTK/Linux word processors look for - but Windows registers CF_RTF under the literal name "Rich Text Format" (via RegisterClipboardFormat), which is the name Word's clipboard handler actually looks up. Neither MIME name matches it, so on Windows Word found no RTF data on the clipboard at all. Fixed by additionally advertising RTF under a third wxDataFormat with that exact name (RtfDataObject3 / Worksheet::m_rtfFormat3) and making it the preferred flavor everywhere RTF is offered; this test pins its presence and that it is now the winning "preferred" format.

GH #2265/#2266/#2267: "Copy as HTML" – WorksheetExport:: SelectionToSelfContainedHTML() renders a GroupCell range into one self-contained HTML document (inline <style>, no external file references), for Worksheet::CopyHTML()'s dedicated context-menu command. It can't itself be exercised via the real system clipboard here (same headless-unfriendly reason as the rest of this file), but it is a pure function of the cell tree, so it is tested directly: the returned document must carry its own stylesheet inline and never point at a file outside itself, and it must clean up whatever private scratch directory it used to render into.

GH #2474: copied cells are also offered as one self-contained XML document (Format::CellsToXMLDocument()) – the XML of a .wxmx, with the images that a .wxmx keeps beside it in the zip carried inside, base64 encoded. It keeps what the .wxm loses: text formatting, output, images. Pinned here: it is offered, it round-trips through Format::TreeFromXMLDocument(), and making it doesn't disturb a .wxmx being saved at the same time.

Function Documentation

◆ SCENARIO()

SCENARIO ( "The bitmap is also offered as PNG bytes (wxQt)"  )

GH #2444 follow-up: a bitmap inside a composite data object reaches nobody on wxQt.

wxQt only turns a wxBitmap into clipboard image data in wxBitmapDataObject::QtAddDataTo(), which runs for the whole clipboard contents, never for one child of a composite: there every format is asked for its bytes, and a bitmap has none. Confirmed against a real wxQt build and a plain wxDataObjectComposite holding nothing but a text and a bitmap object – so this predates the lazily rendered clipboard and is not specific to wxMaxima. LazyPngDataObject is what gives wxQt an image it can hand over, so the invariants that matter are that it encodes the same bitmap as PNG and that it offers nothing when there is no bitmap.