Lumiera  0.pre.03
»edit your freedom«
ui-location-solver-test.cpp
Go to the documentation of this file.
1 /*
2  UILocationSolver(Test) - verify mechanics of a DSL to configure view allocation
3 
4  Copyright (C)
5  2018, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
19 #include "lib/test/run.hpp"
20 #include "lib/test/test-helper.hpp"
24 #include "lib/format-cout.hpp"
25 
26 #include <string>
27 
28 
29 using std::string;
30 using lib::diff::MakeRec;
31 using lib::diff::Rec;
32 
33 using util::isnil;
34 
35 
36 namespace stage {
37 namespace interact {
38 namespace test {
39 
40 
41 
42  /******************************************************************************/
57  class UILocationSolver_test : public Test
58  {
59 
60  virtual void
61  run (Arg)
62  {
66  }
67 
68 
70  void
72  {
73  //-------------------------------------------------------------Test-Fixture
74  // a Test dummy placeholder for the real UI structure
75  Rec dummyUiStructure = MakeRec()
76  .set("window-1"
77  , MakeRec()
78  .type("perspective")
79  .set("exclusivePanel", MakeRec())
80  );
81  // helper to answer "location queries" backed by this structure
82  GenNodeLocationQuery locationQuery{dummyUiStructure};
83  //--------------------------------------------------------------(End)Test-Fixture
84 
85 
86  // our test subject....
87  UILocationSolver solver{locationQuery};
88 
89  // a rule to probe (meaning: attach it at the "shoddy" panel)
90  LocationRule rule{UICoord().panel("shoddy")};
91 
92  // Now ask for a location to attach a view named "worldview" at the "shoddy" panel
93  // No solution can be found, since there is no "shoddy" panel
94  CHECK (isnil (solver.solve (rule, UIC_VIEW, "worldview")));
95 
96  // add second location clause to the rule
97  // (meaning: accept any path leading down to an "exclusivePanel")
98  rule.append(UICoord().panel("exclusivePanel"));
99 
100  // and now we get a solution, since the second rule can be wildcard-matched
101  UICoord location = solver.solve (rule, UIC_VIEW, "worldview");
102  CHECK (not isnil (location));
103 
104  // the full solution filled in the missing parts and added the new view on top
105  CHECK ("UI:window-1[perspective]-exclusivePanel.worldview" == string(location));
106 
107  // NOTE: the new view does not (yet) exist, but the preceding part can be "covered"
108  // To verify this, we attach a coordinate resolver (likewise backed by our dummy UI)
109  UICoordResolver resolver{location, locationQuery};
110  CHECK (resolver.isCoveredPartially());
111  CHECK (not resolver.isCoveredTotally());
112  CHECK (UIC_VIEW == resolver.coverDepth()); // covered up to VIEW level
113  } // (the view itself is not covered)
114 
115 
116 
129  void
131  {
132  //-------------------------------------------------------------Test-Fixture
133  GenNodeLocationQuery tree{MakeRec()
134  .set("win"
135  , MakeRec()
136  .type("A")
137  .set ("thePanel"
138  , MakeRec()
139  .set ("theView"
140  , MakeRec()
141  .set ("#5"
142  , MakeRec()
143  .set ("up", MakeRec())
144  .set ("down"
145  , MakeRec()
146  .set ("the"
147  , MakeRec()
148  .set ("kitchen"
149  , MakeRec()
150  .set ("sink", MakeRec())
151  )
152  )
153  )
154  )
155  )
156  )
157  )};
158  UILocationSolver solver{tree};
159  //--------------------------------------------------------------(End)Test-Fixture
160 
161 
162  /* === empty clause === */
163  LocationRule r1{UICoord()};
164  CHECK (isnil (solver.solve (r1, UIC_PATH, "to/salvation")));
165  CHECK (isnil (solver.solve (r1, UIC_WINDOW, "redemption")));
166 
167  /* === empty clause is neutral === */
168  r1.append (UICoord().path("down/to").create());
169  auto s1 = solver.solve(r1, UIC_PATH+2, "hell");
170  CHECK ("UI:win[A]-thePanel.theView.#5/down/to/hell" == string{s1});
171 
172 
173  /* === clause too short === */
174  LocationRule r2{UICoord().path("down/the")};
175  CHECK ( isnil (solver.solve (r2, UIC_PATH+3, "sink")));
176 
177  /* === clause too long === */
178  CHECK ( isnil (solver.solve (r2, UIC_VIEW, "theView")));
179 
180  CHECK (not isnil (solver.solve (r2, UIC_PATH+1, "any")));
181  CHECK (not isnil (solver.solve (r2, UIC_PATH+2, "kitchen")));
182 
183 
184 
185  /* === query on existing window === */
186  LocationRule r31{UICoord::window("win")};
187  CHECK ("UI:win" == string{solver.solve (r31, UIC_WINDOW, "wigwam")});
188 
189  /* === query on generic window spec === */
191  CHECK ("UI:win" == string{solver.solve (r32, UIC_WINDOW, "wigwam")});
192 
193  /* === query on non existing window === */
194  LocationRule r33{UICoord::window("lindows")};
195  CHECK (isnil (solver.solve (r33, UIC_WINDOW, "wigwam")));
196 
197  /* === query on existing window with create clause === */
198  LocationRule r34{UICoord::window("win").create()};
199  CHECK ("UI:win" == string{solver.solve (r34, UIC_WINDOW, "wigwam")});
200 
201  /* === query on non existing window with create clause === */
202  LocationRule r35{UICoord::window("windux").create()};
203  CHECK ("UI:windux" == string{solver.solve (r35, UIC_WINDOW, "wigwam")});
204 
205 
206  /* === query on existing perspective === */
207  LocationRule r41{UICoord().persp("A")};
208  CHECK ("UI:win[A]" == string{solver.solve (r41, UIC_PERSP, "x")});
209  CHECK ("UI:win[A]-x" == string{solver.solve (r41, UIC_PANEL, "x")});
210 
211  /* === query on elided perspective ("just any existing") === */
213  CHECK ("UI:win[A]" == string{solver.solve (r42, UIC_PERSP, "x")});
214  CHECK ("UI:win[A]-x" == string{solver.solve (r42, UIC_PANEL, "x")});
215 
216  /* === query on non existing perspective === */
218  CHECK (isnil (solver.solve (r43, UIC_PERSP, "x")));
219  CHECK (isnil (solver.solve (r43, UIC_PANEL, "x")));
220 
221  /* === query on non existing perspective with create clause === */
223  CHECK ("UI:win[Ω]" == string{solver.solve (r44, UIC_PERSP, "x")});
224  CHECK ("UI:win[Ω]-x" == string{solver.solve (r44, UIC_PANEL, "x")});
225 
226 
227  /* === query on deep path covered === */
228  LocationRule r51{UICoord("firstWindow","A","thePanel","theView","#5","down","the","kitchen")};
229  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen" == string{solver.solve (r51, UIC_PATH+2, "drain")});
230  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen/drain" == string{solver.solve (r51, UIC_PATH+3, "drain")});
231 
232  /* === query on deep path covered with create clause === */
233  LocationRule r52{UICoord::firstWindow().append("A/thePanel/theView/#5/down/the/kitchen").create()};
234  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen" == string{solver.solve (r52, UIC_PATH+2, "drain")});
235  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen/drain" == string{solver.solve (r52, UIC_PATH+3, "drain")});
236 
237  /* === query on deep path partially covered === */
238  LocationRule r53{UICoord::firstWindow().append("A/thePanel/theView/#5/down/the/drain")};
239  CHECK (isnil (solver.solve (r53, UIC_PATH+2, "drain")));
240  CHECK (isnil (solver.solve (r53, UIC_PATH+3, "drain")));
241 
242  /* === query on deep path partially covered with create clause === */
243  LocationRule r54{UICoord::firstWindow().append("A/thePanel/theView/#5/down/the/drain").create()};
244  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/drain" == string{solver.solve (r54, UIC_PATH+2, "drain")});
245  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/drain/drain" == string{solver.solve (r54, UIC_PATH+3, "drain")});
246 
247  /* === query on deep path uncovered === */
248  LocationRule r55{UICoord("rearWindow","A","thePanel","theView","#5","down","the","kitchen")};
249  CHECK (isnil (solver.solve (r55, UIC_PATH+2, "floor")));
250  CHECK (isnil (solver.solve (r55, UIC_PATH+3, "floor")));
251 
252  /* === query on deep path uncovered with create clause === */
253  LocationRule r56{UICoord("rearWindow","A","thePanel","theView","#5","down","the","kitchen").rebuild().create()};
254  CHECK ("UI:rearWindow[A]-thePanel.theView.#5/down/the/kitchen" == string{solver.solve (r56, UIC_PATH+2, "floor")});
255  CHECK ("UI:rearWindow[A]-thePanel.theView.#5/down/the/kitchen/floor" == string{solver.solve (r56, UIC_PATH+3, "floor")});
256 
257 
258  /* === clause with wildcard covered === */
259  LocationRule r61{UICoord().path("//kitchen")};
260  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen" == string{solver.solve (r61, UIC_PATH+2, "drain")});
261 
262  /* === clause with wildcard covered without final element === */
263  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen/drain" == string{solver.solve (r61, UIC_PATH+3, "drain")});
264 
265  /* === create clause with wildcard completely covered === */
266  LocationRule r62{UICoord().path("//kitchen").create()};
267  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen" == string{solver.solve (r62, UIC_PATH+2, "window")});
268 
269  /* === create clause with wildcard covered without final element === */
270  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen/window" == string{solver.solve (r62, UIC_PATH+3, "window")});
271 
272  /* === clause with wildcard partially covered === */
273  LocationRule r63{UICoord().path("/the/road")};
274  CHECK (isnil (solver.solve (r63, UIC_PATH+2, "kitchen"))); //NOTE: .../down/the/kitchen would match, but actually .../down/the/road is tested, which fails
275 
276  /* === create clause with wildcard partially covered === */
277  LocationRule r64{UICoord().path("/the/road").create()};
278  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/road" == string{solver.solve (r64, UIC_PATH+2, "drain")});
279 
280  /* === clause with wildcard uncovered === */
281  LocationRule r65{UICoord().path("//road")};
282  CHECK (isnil (solver.solve (r65, UIC_PATH+2, "kitchen")));
283 
284  /* === create clause with wildcard uncovered === */
285  LocationRule r66{UICoord().path("//road").create()};
286  CHECK (isnil (solver.solve (r66, UIC_PATH+2, "kitchen")));
287 
288 
289  /* === two clauses both satisfied === */
290  LocationRule r71{UICoord().path("down")};
291  r71.append (UICoord().path("up"));
292  CHECK ("UI:win[A]-thePanel.theView.#5/down/time" == string{solver.solve (r71, UIC_PATH+1, "time")});
293 
294  /* === two clauses first one unsatisfied === */
295  LocationRule r72{UICoord().path("up/the")};
296  r72.append (UICoord().path("down/"));
297  CHECK ("UI:win[A]-thePanel.theView.#5/down/time" == string{solver.solve (r72, UIC_PATH+1, "time")});
298 
299  /* === create clause first and satisfied === */
300  LocationRule r73{UICoord().path("up/link").create()};
301  r73.append (UICoord().path("down/"));
302  CHECK ("UI:win[A]-thePanel.theView.#5/up/link" == string{solver.solve (r73, UIC_PATH+1, "time")});
303 
304  /* === create clause first and unsatisfied === */
305  LocationRule r74{UICoord().path("cross/link").create()};
306  r74.append (UICoord().path("down/"));
307  CHECK ("UI:win[A]-thePanel.theView.#5/down/time" == string{solver.solve (r74, UIC_PATH+1, "time")});
308 
309  /* === create clause second but first clause satisfied === */
310  LocationRule r75{UICoord().path("up/")};
311  r75.append (UICoord().path("down/link").create());
312  CHECK ("UI:win[A]-thePanel.theView.#5/up/time" == string{solver.solve (r75, UIC_PATH+1, "time")});
313 
314  /* === create clause second and satisfied === */
315  LocationRule r76{UICoord().path("up/link")};
316  r76.append (UICoord().path("down/link").create());
317  CHECK ("UI:win[A]-thePanel.theView.#5/down/link" == string{solver.solve (r76, UIC_PATH+1, "time")});
318 
319  /* === create clause second and both unsatisfied === */
320  LocationRule r77{UICoord().path("up/link")};
321  r77.append (UICoord().path("town/link").create());
322  CHECK (isnil (solver.solve (r77, UIC_PATH+1, "time")));
323 
324  CHECK (string{r77} == "=~ .. UI:?/up/link"
325  "\n OR UI:?/town/link create!");
326  }
327 
328 
338  void
340  {
341  // Test Fixture: a solver which always queries the current state of a (simulated) uiTree
342  Rec uiTree;
343  std::unique_ptr<GenNodeLocationQuery> query;
344  UILocationSolver solver{[&]() -> GenNodeLocationQuery&
345  {
346  query.reset (new GenNodeLocationQuery(uiTree));
347  return *query;
348  }};
349 
350  // Test Fixture: common set of location clauses
351  LocationRule location{UICoord().persp("edit").panel("viewer")};
352  location.append (UICoord::currentWindow().panel("viewer"));
353  location.append (UICoord().panel("viewer"));
354 // location.append (UICoord().tab("assetType()")); //////////////////////TICKET #1130 : do we want to support match based on invocation context (here: the type of the asset to be displayed)
355  location.append (UICoord().persp("asset").view("asset"));
356  location.append (UICoord().panel("asset").view("asset").create());
357  location.append (UICoord::currentWindow().panel("viewer").create()); //Note: especially for this kind of rule, .persp(UIC_ELIDED) is injected automatically
358  location.append (UICoord::window("meta").persp("config").panel("infobox").view("inspect").create());
359 
360  cout << location << endl;
361 
362 
363  /* === match by perspective + panel === */
364  uiTree = MakeRec()
365  .set("win"
366  , MakeRec()
367  .type("edit")
368  .set ("viewer", MakeRec()));
369  CHECK ("UI:win[edit]-viewer.video" == string{solver.solve (location, UIC_VIEW, "video")});
370 
371  /* === match by generic window + panel === */
372  uiTree = MakeRec()
373  .set("win"
374  , MakeRec()
375  .type("murky")
376  .set ("viewer", MakeRec()))
377  .set("woe"
378  , MakeRec()
379  .type("gloomy")
380  .set ("viewer", MakeRec()));
381  CHECK ("UI:woe[gloomy]-viewer.video" == string{solver.solve (location, UIC_VIEW, "video")}); //Note: first rule does not match due to perspective
382 
383  /* === match by panel alone === */
384  uiTree = MakeRec()
385  .set("win"
386  , MakeRec()
387  .type("murky")
388  .set ("viewer", MakeRec()))
389  .set("woe"
390  , MakeRec()
391  .type("gloomy")
392  .set ("timeline", MakeRec()));
393  CHECK ("UI:win[murky]-viewer.video" == string{solver.solve (location, UIC_VIEW, "video")}); //Note: current window (==last one) has no "viewer"-panel
394 
395 
396 
397  /* === wildcard match on explicit existing view === */
398  uiTree = MakeRec()
399  .set("win"
400  , MakeRec()
401  .type("shady")
402  .set("timeline", MakeRec()))
403  .set("woe"
404  , MakeRec()
405  .type("asset")
406  .set ("panel"
407  , MakeRec()
408  .set ("asset", MakeRec())
409  ));
410  CHECK ("UI:woe[asset]-panel.asset" == string{solver.solve (location, UIC_VIEW, "video")}); //Note: the 4th Rule matches on existing view "asset",
411  // in spite of our query demanding a view "video"
412  /* === wildcard match based on the type of entity to be displaced === */
413 #if false
414 // uiTree = MakeRec()
415 // .set("win"
416 // , MakeRec()
417 // .type("shady")
418 // .set ("special"
419 // , MakeRec()
420 // .set ("asset",
421 // MakeRec()
422 // .set ("specialAsset", MakeRec())
423 // )
424 // ))
425 // .set("woe"
426 // , MakeRec()
427 // .type("asset")
428 // .set ("panel"
429 // , MakeRec()
430 // .set ("asset", MakeRec())
431 // ));
432 // CHECK ("UI:win[shady]-special.asset.specialAsset" == string{solver.solve (location, UIC_TAB, "specialAsset")});
433 // //Note: the next rule would match on the general asset panel
434 // // but this special rule allows to re-use a tab dedicated to specialAsset
435 #endif
436 
437 
438 
439  /* === create clause to build on a specific anchor point === */
440  uiTree = MakeRec()
441  .set("win"
442  , MakeRec()
443  .type("shady")
444  .set ("asset", MakeRec())
445  );
446  auto solution = solver.solve (location, UIC_TAB, "video"); //Note: here the first "create"-rule is triggered: UI:?-asset.asset
447  CHECK ("UI:win[shady]-asset.asset.video" == string{solution}); // It requires a panel("asset") to exist, but creates the rest;
448  CHECK ( 3 == UICoordResolver(solution, *query) // indeed only the part up to the panel is detected as covered.
449  .coverDepth());
450  //Note: the following test cases can not trigger this rule, since it
451  /* === match on create clause with generic window spec and panel === */ // contains leading wildcards and thus requires panel("asset")
452  uiTree = MakeRec()
453  .set("win"
454  , MakeRec()
455  .type("shady")
456  .set("timeline", MakeRec()))
457  .set("woe"
458  , MakeRec()
459  .type("shoddy"));
460  solution = solver.solve (location, UIC_VIEW, "video");
461  CHECK ("UI:woe[shoddy]-viewer.video" == string{solution});
462  CHECK ( 2 == UICoordResolver(solution, *query) //Note: only window and perspective are covered, the rest is to be created
463  .coverDepth());
464 
465  /* === completely uncovered create-from-scratch === */
466  solution = solver.solve (location, UIC_TAB, "engine"); //Note: same UI-tree, but this time we ask for a tab, so the previous rule
467  CHECK ("UI:meta[config]-infobox.inspect.engine" == string{solution}); // is too short and thus the last catch-all rule gets triggered;
468  CHECK ( 0 == UICoordResolver(solution, *query) //Note: result is indeed entirely uncovered (-> create from scratch)
469  .coverDepth());
470  }
471  };
472 
473 
475  LAUNCHER (UILocationSolver_test, "unit stage");
476 
477 
478 }}} // namespace stage::interact::test
LocationClause create()
interprets the current (inline) builder contents as create clause, which has the meaning "create a ne...
Test/Diagnostics: implementation of the LocationQuery-API based on a abstract topological structure g...
Describe a location within the UI through structural/topological coordinates.
Definition: ui-coord.hpp:129
Service to determine the location of an UI component view.
Builder persp(Literal perspectiveID) const
Definition: ui-coord.hpp:695
Automatically use custom string conversion in C++ stream output.
Implementation of the stage::interact::LocationQuery interface to work on a GenNode tree...
Definition: run.hpp:40
Builder path(Literal pathDefinition) const
convenience builder function so set a full path definition
Definition: ui-coord.hpp:736
Builder && persp(Literal perspectiveID)
augment UI coordinates to mandate a specific perspective to be active within the window ...
Definition: ui-coord.hpp:538
Builder && append(Literal elm)
augment UI coordinates by appending a further component at the end.
Definition: ui-coord.hpp:591
const Symbol UIC_ELIDED
indicate that a component is elided or irrelevant here
Builder && panel(Literal panelID)
augment UI coordinates to indicate a specific view to be used
Definition: ui-coord.hpp:546
Simplistic test class runner.
Lumiera GTK UI implementation root.
Definition: guifacade.cpp:37
A topological addressing scheme to designate structural locations within the UI.
Query and mutate UICoord specifications in relation to actual UI topology.
A collection of frequently used helper functions to support unit testing.
A solver to match incomplete coordinate specifications against the actual UI topology.
A rule to determine some location by matching against the UI-tree.
static Builder window(Literal windowID)
Builder: start definition of UI-Coordinates rooted in given window.
Definition: ui-coord.hpp:682
object-like record of data.
Definition: record.hpp:141
static Builder firstWindow()
Builder: start definition of UI-Coordinates rooted in the firstWindow
Definition: ui-coord.hpp:675
static Builder currentWindow()
Builder: start definition of UI-Coordinates rooted in the currentWindow
Definition: ui-coord.hpp:669