Initial cucumber test.
[dcpomatic.git] / features / step_definitions / dcpomatic.cc
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "wx/cucumber_bridge.h"
22 #include "lib/nanomsg.h"
23 #include <boost/test/unit_test.hpp>
24 #include <boost/process.hpp>
25 #include <iostream>
26
27 #include <cucumber-cpp/autodetect.hpp>
28
29
30 using std::getline;
31 using std::string;
32 using std::stringstream;
33 using std::vector;
34 using boost::optional;
35 using cucumber::ScenarioScope;
36
37
38 struct DoMCtx
39 {
40         DoMCtx() {
41                 _nanomsg = new Nanomsg (true);
42                 _dom = new boost::process::child ("/home/carl/src/dcpomatic/build/src/tools/dcpomatic2", "--cucumber");
43                 /* Await "ready" message */
44                 _nanomsg->receive (-1);
45         }
46
47         void click_button (string id)
48         {
49                 general_delay ();
50                 send (CUCUMBER_BRIDGE_CLICK_BUTTON);
51                 send (id);
52         }
53
54         void select_menu (string id)
55         {
56                 general_delay ();
57                 send (CUCUMBER_BRIDGE_SELECT_MENU);
58                 send (id);
59         }
60
61         void type (std::string text)
62         {
63                 general_delay ();
64                 send (CUCUMBER_BRIDGE_TYPE);
65                 send (text);
66         }
67
68         void add_content_file (string filename)
69         {
70                 general_delay ();
71                 send (CUCUMBER_BRIDGE_ADD_CONTENT_FILE);
72                 send (filename);
73         }
74
75         vector<string> content_list ()
76         {
77                 send (CUCUMBER_BRIDGE_GET_CONTENT_LIST);
78                 stringstream all(receive());
79                 vector<string> split;
80                 string one;
81                 while (getline(all, one, '\n')) {
82                         split.push_back(one);
83                 }
84                 return split;
85         }
86
87 private:
88         void send (string m)
89         {
90                 _nanomsg->send(m + "\n", -1);
91         }
92
93         std::string receive ()
94         {
95                 return _nanomsg->receive(-1).get();
96         }
97
98         void general_delay ()
99         {
100                 sleep (2);
101         }
102
103         Nanomsg* _nanomsg;
104         boost::process::child* _dom;
105 };
106
107
108 GIVEN("^I have created a new film$") {
109         ScenarioScope<DoMCtx> context;
110         context->select_menu (CUCUMBER_MENU_FILE_NEW);
111         context->type ("test");
112 }
113
114
115 WHEN("^I add file (.+) as content$") {
116         REGEX_PARAM(string, filename);
117         ScenarioScope<DoMCtx> context;
118         /* I could not find a way to reliably enter text into a open file dialogue, so
119          * there's a custom method here.
120          */
121         context->add_content_file (filename);
122 }
123
124 THEN("^the result should be only (.+) in the content list$") {
125         REGEX_PARAM(string, entry);
126         ScenarioScope<DoMCtx> context;
127         vector<string> content = context->content_list ();
128         BOOST_CHECK_EQUAL (content.size(), 1);
129         BOOST_CHECK_EQUAL (content.front(), entry);
130 }
131