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) Lumiera.org
5  2018, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
33 #include "lib/format-cout.hpp"
34 
35 #include <string>
36 
37 
38 using std::string;
39 using lib::diff::MakeRec;
40 using lib::diff::Rec;
41 
42 using util::isnil;
43 
44 
45 namespace stage {
46 namespace interact {
47 namespace test {
48 
49 
50 
51  /******************************************************************************/
66  class UILocationSolver_test : public Test
67  {
68 
69  virtual void
70  run (Arg)
71  {
75  }
76 
77 
79  void
81  {
82  //-------------------------------------------------------------Test-Fixture
83  // a Test dummy placeholder for the real UI structure
84  Rec dummyUiStructure = MakeRec()
85  .set("window-1"
86  , MakeRec()
87  .type("perspective")
88  .set("exclusivePanel", MakeRec())
89  );
90  // helper to answer "location queries" backed by this structure
91  GenNodeLocationQuery locationQuery{dummyUiStructure};
92  //--------------------------------------------------------------(End)Test-Fixture
93 
94 
95  // our test subject....
96  UILocationSolver solver{locationQuery};
97 
98  // a rule to probe (meaning: attach it at the "shoddy" panel)
99  LocationRule rule{UICoord().panel("shoddy")};
100 
101  // Now ask for a location to attach a view named "worldview" at the "shoddy" panel
102  // No solution can be found, since there is no "shoddy" panel
103  CHECK (isnil (solver.solve (rule, UIC_VIEW, "worldview")));
104 
105  // add second location clause to the rule
106  // (meaning: accept any path leading down to an "exclusivePanel")
107  rule.append(UICoord().panel("exclusivePanel"));
108 
109  // and now we get a solution, since the second rule can be wildcard-matched
110  UICoord location = solver.solve (rule, UIC_VIEW, "worldview");
111  CHECK (not isnil (location));
112 
113  // the full solution filled in the missing parts and added the new view on top
114  CHECK ("UI:window-1[perspective]-exclusivePanel.worldview" == string(location));
115 
116  // NOTE: the new view does not (yet) exist, but the preceding part can be "covered"
117  // To verify this, we attach a coordinate resolver (likewise backed by our dummy UI)
118  UICoordResolver resolver{location, locationQuery};
119  CHECK (resolver.isCoveredPartially());
120  CHECK (not resolver.isCoveredTotally());
121  CHECK (UIC_VIEW == resolver.coverDepth()); // covered up to VIEW level
122  } // (the view itself is not covered)
123 
124 
125 
138  void
140  {
141  //-------------------------------------------------------------Test-Fixture
142  GenNodeLocationQuery tree{MakeRec()
143  .set("win"
144  , MakeRec()
145  .type("A")
146  .set ("thePanel"
147  , MakeRec()
148  .set ("theView"
149  , MakeRec()
150  .set ("#5"
151  , MakeRec()
152  .set ("up", MakeRec())
153  .set ("down"
154  , MakeRec()
155  .set ("the"
156  , MakeRec()
157  .set ("kitchen"
158  , MakeRec()
159  .set ("sink", MakeRec())
160  )
161  )
162  )
163  )
164  )
165  )
166  )};
167  UILocationSolver solver{tree};
168  //--------------------------------------------------------------(End)Test-Fixture
169 
170 
171  /* === empty clause === */
172  LocationRule r1{UICoord()};
173  CHECK (isnil (solver.solve (r1, UIC_PATH, "to/salvation")));
174  CHECK (isnil (solver.solve (r1, UIC_WINDOW, "redemption")));
175 
176  /* === empty clause is neutral === */
177  r1.append (UICoord().path("down/to").create());
178  auto s1 = solver.solve(r1, UIC_PATH+2, "hell");
179  CHECK ("UI:win[A]-thePanel.theView.#5/down/to/hell" == string{s1});
180 
181 
182  /* === clause too short === */
183  LocationRule r2{UICoord().path("down/the")};
184  CHECK ( isnil (solver.solve (r2, UIC_PATH+3, "sink")));
185 
186  /* === clause too long === */
187  CHECK ( isnil (solver.solve (r2, UIC_VIEW, "theView")));
188 
189  CHECK (not isnil (solver.solve (r2, UIC_PATH+1, "any")));
190  CHECK (not isnil (solver.solve (r2, UIC_PATH+2, "kitchen")));
191 
192 
193 
194  /* === query on existing window === */
195  LocationRule r31{UICoord::window("win")};
196  CHECK ("UI:win" == string{solver.solve (r31, UIC_WINDOW, "wigwam")});
197 
198  /* === query on generic window spec === */
200  CHECK ("UI:win" == string{solver.solve (r32, UIC_WINDOW, "wigwam")});
201 
202  /* === query on non existing window === */
203  LocationRule r33{UICoord::window("lindows")};
204  CHECK (isnil (solver.solve (r33, UIC_WINDOW, "wigwam")));
205 
206  /* === query on existing window with create clause === */
207  LocationRule r34{UICoord::window("win").create()};
208  CHECK ("UI:win" == string{solver.solve (r34, UIC_WINDOW, "wigwam")});
209 
210  /* === query on non existing window with create clause === */
211  LocationRule r35{UICoord::window("windux").create()};
212  CHECK ("UI:windux" == string{solver.solve (r35, UIC_WINDOW, "wigwam")});
213 
214 
215  /* === query on existing perspective === */
216  LocationRule r41{UICoord().persp("A")};
217  CHECK ("UI:win[A]" == string{solver.solve (r41, UIC_PERSP, "x")});
218  CHECK ("UI:win[A]-x" == string{solver.solve (r41, UIC_PANEL, "x")});
219 
220  /* === query on elided perspective ("just any existing") === */
222  CHECK ("UI:win[A]" == string{solver.solve (r42, UIC_PERSP, "x")});
223  CHECK ("UI:win[A]-x" == string{solver.solve (r42, UIC_PANEL, "x")});
224 
225  /* === query on non existing perspective === */
227  CHECK (isnil (solver.solve (r43, UIC_PERSP, "x")));
228  CHECK (isnil (solver.solve (r43, UIC_PANEL, "x")));
229 
230  /* === query on non existing perspective with create clause === */
232  CHECK ("UI:win[Ω]" == string{solver.solve (r44, UIC_PERSP, "x")});
233  CHECK ("UI:win[Ω]-x" == string{solver.solve (r44, UIC_PANEL, "x")});
234 
235 
236  /* === query on deep path covered === */
237  LocationRule r51{UICoord("firstWindow","A","thePanel","theView","#5","down","the","kitchen")};
238  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen" == string{solver.solve (r51, UIC_PATH+2, "drain")});
239  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen/drain" == string{solver.solve (r51, UIC_PATH+3, "drain")});
240 
241  /* === query on deep path covered with create clause === */
242  LocationRule r52{UICoord::firstWindow().append("A/thePanel/theView/#5/down/the/kitchen").create()};
243  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen" == string{solver.solve (r52, UIC_PATH+2, "drain")});
244  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen/drain" == string{solver.solve (r52, UIC_PATH+3, "drain")});
245 
246  /* === query on deep path partially covered === */
247  LocationRule r53{UICoord::firstWindow().append("A/thePanel/theView/#5/down/the/drain")};
248  CHECK (isnil (solver.solve (r53, UIC_PATH+2, "drain")));
249  CHECK (isnil (solver.solve (r53, UIC_PATH+3, "drain")));
250 
251  /* === query on deep path partially covered with create clause === */
252  LocationRule r54{UICoord::firstWindow().append("A/thePanel/theView/#5/down/the/drain").create()};
253  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/drain" == string{solver.solve (r54, UIC_PATH+2, "drain")});
254  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/drain/drain" == string{solver.solve (r54, UIC_PATH+3, "drain")});
255 
256  /* === query on deep path uncovered === */
257  LocationRule r55{UICoord("rearWindow","A","thePanel","theView","#5","down","the","kitchen")};
258  CHECK (isnil (solver.solve (r55, UIC_PATH+2, "floor")));
259  CHECK (isnil (solver.solve (r55, UIC_PATH+3, "floor")));
260 
261  /* === query on deep path uncovered with create clause === */
262  LocationRule r56{UICoord("rearWindow","A","thePanel","theView","#5","down","the","kitchen").rebuild().create()};
263  CHECK ("UI:rearWindow[A]-thePanel.theView.#5/down/the/kitchen" == string{solver.solve (r56, UIC_PATH+2, "floor")});
264  CHECK ("UI:rearWindow[A]-thePanel.theView.#5/down/the/kitchen/floor" == string{solver.solve (r56, UIC_PATH+3, "floor")});
265 
266 
267  /* === clause with wildcard covered === */
268  LocationRule r61{UICoord().path("//kitchen")};
269  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen" == string{solver.solve (r61, UIC_PATH+2, "drain")});
270 
271  /* === clause with wildcard covered without final element === */
272  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen/drain" == string{solver.solve (r61, UIC_PATH+3, "drain")});
273 
274  /* === create clause with wildcard completely covered === */
275  LocationRule r62{UICoord().path("//kitchen").create()};
276  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen" == string{solver.solve (r62, UIC_PATH+2, "window")});
277 
278  /* === create clause with wildcard covered without final element === */
279  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/kitchen/window" == string{solver.solve (r62, UIC_PATH+3, "window")});
280 
281  /* === clause with wildcard partially covered === */
282  LocationRule r63{UICoord().path("/the/road")};
283  CHECK (isnil (solver.solve (r63, UIC_PATH+2, "kitchen"))); //NOTE: .../down/the/kitchen would match, but actually .../down/the/road is tested, which fails
284 
285  /* === create clause with wildcard partially covered === */
286  LocationRule r64{UICoord().path("/the/road").create()};
287  CHECK ("UI:win[A]-thePanel.theView.#5/down/the/road" == string{solver.solve (r64, UIC_PATH+2, "drain")});
288 
289  /* === clause with wildcard uncovered === */
290  LocationRule r65{UICoord().path("//road")};
291  CHECK (isnil (solver.solve (r65, UIC_PATH+2, "kitchen")));
292 
293  /* === create clause with wildcard uncovered === */
294  LocationRule r66{UICoord().path("//road").create()};
295  CHECK (isnil (solver.solve (r66, UIC_PATH+2, "kitchen")));
296 
297 
298  /* === two clauses both satisfied === */
299  LocationRule r71{UICoord().path("down")};
300  r71.append (UICoord().path("up"));
301  CHECK ("UI:win[A]-thePanel.theView.#5/down/time" == string{solver.solve (r71, UIC_PATH+1, "time")});
302 
303  /* === two clauses first one unsatisfied === */
304  LocationRule r72{UICoord().path("up/the")};
305  r72.append (UICoord().path("down/"));
306  CHECK ("UI:win[A]-thePanel.theView.#5/down/time" == string{solver.solve (r72, UIC_PATH+1, "time")});
307 
308  /* === create clause first and satisfied === */
309  LocationRule r73{UICoord().path("up/link").create()};
310  r73.append (UICoord().path("down/"));
311  CHECK ("UI:win[A]-thePanel.theView.#5/up/link" == string{solver.solve (r73, UIC_PATH+1, "time")});
312 
313  /* === create clause first and unsatisfied === */
314  LocationRule r74{UICoord().path("cross/link").create()};
315  r74.append (UICoord().path("down/"));
316  CHECK ("UI:win[A]-thePanel.theView.#5/down/time" == string{solver.solve (r74, UIC_PATH+1, "time")});
317 
318  /* === create clause second but first clause satisfied === */
319  LocationRule r75{UICoord().path("up/")};
320  r75.append (UICoord().path("down/link").create());
321  CHECK ("UI:win[A]-thePanel.theView.#5/up/time" == string{solver.solve (r75, UIC_PATH+1, "time")});
322 
323  /* === create clause second and satisfied === */
324  LocationRule r76{UICoord().path("up/link")};
325  r76.append (UICoord().path("down/link").create());
326  CHECK ("UI:win[A]-thePanel.theView.#5/down/link" == string{solver.solve (r76, UIC_PATH+1, "time")});
327 
328  /* === create clause second and both unsatisfied === */
329  LocationRule r77{UICoord().path("up/link")};
330  r77.append (UICoord().path("town/link").create());
331  CHECK (isnil (solver.solve (r77, UIC_PATH+1, "time")));
332 
333  CHECK (string{r77} == "=~ .. UI:?/up/link"
334  "\n OR UI:?/town/link create!");
335  }
336 
337 
347  void
349  {
350  // Test Fixture: a solver which always queries the current state of a (simulated) uiTree
351  Rec uiTree;
352  std::unique_ptr<GenNodeLocationQuery> query;
353  UILocationSolver solver{[&]() -> GenNodeLocationQuery&
354  {
355  query.reset (new GenNodeLocationQuery(uiTree));
356  return *query;
357  }};
358 
359  // Test Fixture: common set of location clauses
360  LocationRule location{UICoord().persp("edit").panel("viewer")};
361  location.append (UICoord::currentWindow().panel("viewer"));
362  location.append (UICoord().panel("viewer"));
363 // 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)
364  location.append (UICoord().persp("asset").view("asset"));
365  location.append (UICoord().panel("asset").view("asset").create());
366  location.append (UICoord::currentWindow().panel("viewer").create()); //Note: especially for this kind of rule, .persp(UIC_ELIDED) is injected automatically
367  location.append (UICoord::window("meta").persp("config").panel("infobox").view("inspect").create());
368 
369  cout << location << endl;
370 
371 
372  /* === match by perspective + panel === */
373  uiTree = MakeRec()
374  .set("win"
375  , MakeRec()
376  .type("edit")
377  .set ("viewer", MakeRec()));
378  CHECK ("UI:win[edit]-viewer.video" == string{solver.solve (location, UIC_VIEW, "video")});
379 
380  /* === match by generic window + panel === */
381  uiTree = MakeRec()
382  .set("win"
383  , MakeRec()
384  .type("murky")
385  .set ("viewer", MakeRec()))
386  .set("woe"
387  , MakeRec()
388  .type("gloomy")
389  .set ("viewer", MakeRec()));
390  CHECK ("UI:woe[gloomy]-viewer.video" == string{solver.solve (location, UIC_VIEW, "video")}); //Note: first rule does not match due to perspective
391 
392  /* === match by panel alone === */
393  uiTree = MakeRec()
394  .set("win"
395  , MakeRec()
396  .type("murky")
397  .set ("viewer", MakeRec()))
398  .set("woe"
399  , MakeRec()
400  .type("gloomy")
401  .set ("timeline", MakeRec()));
402  CHECK ("UI:win[murky]-viewer.video" == string{solver.solve (location, UIC_VIEW, "video")}); //Note: current window (==last one) has no "viewer"-panel
403 
404 
405 
406  /* === wildcard match on explicit existing view === */
407  uiTree = MakeRec()
408  .set("win"
409  , MakeRec()
410  .type("shady")
411  .set("timeline", MakeRec()))
412  .set("woe"
413  , MakeRec()
414  .type("asset")
415  .set ("panel"
416  , MakeRec()
417  .set ("asset", MakeRec())
418  ));
419  CHECK ("UI:woe[asset]-panel.asset" == string{solver.solve (location, UIC_VIEW, "video")}); //Note: the 4th Rule matches on existing view "asset",
420  // in spite of our query demanding a view "video"
421  /* === wildcard match based on the type of entity to be displaced === */
422 #if false
423 // uiTree = MakeRec()
424 // .set("win"
425 // , MakeRec()
426 // .type("shady")
427 // .set ("special"
428 // , MakeRec()
429 // .set ("asset",
430 // MakeRec()
431 // .set ("specialAsset", MakeRec())
432 // )
433 // ))
434 // .set("woe"
435 // , MakeRec()
436 // .type("asset")
437 // .set ("panel"
438 // , MakeRec()
439 // .set ("asset", MakeRec())
440 // ));
441 // CHECK ("UI:win[shady]-special.asset.specialAsset" == string{solver.solve (location, UIC_TAB, "specialAsset")});
442 // //Note: the next rule would match on the general asset panel
443 // // but this special rule allows to re-use a tab dedicated to specialAsset
444 #endif
445 
446 
447 
448  /* === create clause to build on a specific anchor point === */
449  uiTree = MakeRec()
450  .set("win"
451  , MakeRec()
452  .type("shady")
453  .set ("asset", MakeRec())
454  );
455  auto solution = solver.solve (location, UIC_TAB, "video"); //Note: here the first "create"-rule is triggered: UI:?-asset.asset
456  CHECK ("UI:win[shady]-asset.asset.video" == string{solution}); // It requires a panel("asset") to exist, but creates the rest;
457  CHECK ( 3 == UICoordResolver(solution, *query) // indeed only the part up to the panel is detected as covered.
458  .coverDepth());
459  //Note: the following test cases can not trigger this rule, since it
460  /* === match on create clause with generic window spec and panel === */ // contains leading wildcards and thus requires panel("asset")
461  uiTree = MakeRec()
462  .set("win"
463  , MakeRec()
464  .type("shady")
465  .set("timeline", MakeRec()))
466  .set("woe"
467  , MakeRec()
468  .type("shoddy"));
469  solution = solver.solve (location, UIC_VIEW, "video");
470  CHECK ("UI:woe[shoddy]-viewer.video" == string{solution});
471  CHECK ( 2 == UICoordResolver(solution, *query) //Note: only window and perspective are covered, the rest is to be created
472  .coverDepth());
473 
474  /* === completely uncovered create-from-scratch === */
475  solution = solver.solve (location, UIC_TAB, "engine"); //Note: same UI-tree, but this time we ask for a tab, so the previous rule
476  CHECK ("UI:meta[config]-infobox.inspect.engine" == string{solution}); // is too short and thus the last catch-all rule gets triggered;
477  CHECK ( 0 == UICoordResolver(solution, *query) //Note: result is indeed entirely uncovered (-> create from scratch)
478  .coverDepth());
479  }
480  };
481 
482 
484  LAUNCHER (UILocationSolver_test, "unit stage");
485 
486 
487 }}} // 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:138
Service to determine the location of an UI component view.
Builder persp(Literal perspectiveID) const
Definition: ui-coord.hpp:704
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:49
Builder path(Literal pathDefinition) const
convenience builder function so set a full path definition
Definition: ui-coord.hpp:745
Builder && persp(Literal perspectiveID)
augment UI coordinates to mandate a specific perspective to be active within the window ...
Definition: ui-coord.hpp:547
Builder && append(Literal elm)
augment UI coordinates by appending a further component at the end.
Definition: ui-coord.hpp:600
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:555
Simple test class runner.
Lumiera GTK UI implementation root.
Definition: guifacade.cpp:46
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:691
object-like record of data.
Definition: record.hpp:150
static Builder firstWindow()
Builder: start definition of UI-Coordinates rooted in the firstWindow
Definition: ui-coord.hpp:684
static Builder currentWindow()
Builder: start definition of UI-Coordinates rooted in the currentWindow
Definition: ui-coord.hpp:678