4
%% Copyright Ericsson AB 2008-2009. All Rights Reserved.
6
%% The contents of this file are subject to the Erlang Public License,
7
%% Version 1.1, (the "License"); you may not use this file except in
8
%% compliance with the License. You should have received a copy of the
9
%% Erlang Public License along with this software. If not, it can be
10
%% retrieved online at http://www.erlang.org/.
12
%% Software distributed under the License is distributed on an "AS IS"
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
%% the License for the specific language governing rights and limitations
18
%%%-------------------------------------------------------------------
19
%%% File : wx_test_lib.erl
20
%%% Author : Dan Gudmundsson <dan.gudmundsson@ericsson.com>
21
%%% Description : Library for testing wxerlang.
23
%%% Created : 30 Oct 2008 by Dan Gudmundsson <dan.gudmundsson@ericsson.com>
24
%%%-------------------------------------------------------------------
28
-include("wx_test_lib.hrl").
30
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31
init_per_suite(Config) ->
35
exit("Can not test on MacOSX");
37
io:format("DISPLAY ~s~n", [os:getenv("DISPLAY")]),
38
case proplists:get_value(xserver, Config, none) of
41
os:putenv("DISPLAY", Server)
50
{skipped, "No wx compiled for this platform"};
52
{skipped, lists:flatten(io_lib:format("Start wx failed: ~p", [Reason]))}
55
end_per_suite(_Config) ->
58
init_per_testcase(_Func, Config) ->
59
global:register_name(wx_global_logger, group_leader()),
62
end_per_testcase(_Func, Config) ->
63
global:unregister_name(wx_global_logger),
66
%% Backwards compatible with test_server
70
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72
%% Use ?log(Format, Args) as wrapper
73
log(Format, Args, LongFile, Line) ->
74
File = filename:basename(LongFile),
75
Format2 = lists:concat([File, "(", Line, ")", ": ", Format]),
79
case global:whereis_name(wx_global_logger) of
81
io:format(user, Format, Args);
83
io:format(Pid, Format, Args)
86
verbose(Format, Args, File, Line) ->
87
Arg = wx_test_verbose,
92
log(Format, Args, File, Line);
94
case init:get_argument(Arg) of
95
{ok, List} when list(List) ->
96
case lists:last(List) of
99
log(Format, Args, File, Line);
110
error(Format, Args, File, Line) ->
111
global:send(wx_global_logger, {failed, File, Line}),
112
Fail = {filename:basename(File),Line,Args},
113
case global:whereis_name(wx_test_case_sup) of
116
%% global:send(wx_test_case_sup, Fail),
118
log("<ERROR>~n" ++ Format, Args, File, Line).
124
after 4000 -> timeout
127
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130
user_available(Config) ->
131
false /= proplists:get_value(user, Config, false).
134
wx_destroy(Frame, Config) ->
135
case proplists:get_value(user, Config, false) of
138
?m(ok, wxFrame:destroy(Frame)),
139
?m(ok, wx:destroy());
142
?m(ok, wxFrame:destroy(Frame)),
143
?m(ok, wx:destroy());
144
step -> %% Wait for user to close window
145
?m(ok, wxEvtHandler:connect(Frame, close_window, [{skip,true}])),
151
#wx{event=#wxClose{}} ->
152
?log("Got close~n",[]),
153
?m(ok, wx:destroy());
154
#wx{obj=Obj, event=Event} ->
156
Name = wxTopLevelWindow:getTitle(Obj),
157
?log("~p Event: ~p~n", [Name, Event])
159
?log("Event: ~p~n", [Event])
163
?log("Unexpected: ~p~n", [Other]),
169
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170
%% A small test server, which can be run standalone in a shell
172
run_test(Test = {_,_},Config) ->
173
run_test([Test],Config);
174
run_test([{Module, TC}|Rest], Config) ->
175
[run_test(Module, TC, Config) |
176
run_test(Rest, Config)];
177
run_test([], _Config) -> [].
179
run_test(Module, all, Config) ->
180
All = [{Module, Test} || Test <- Module:all()],
181
run_test(All, Config);
182
run_test(Module, TestCase, Config) ->
183
log("Eval test case: ~w~n", [{Module, TestCase}]),
184
Sec = timer:seconds(1) * 1000,
186
timer:tc(?MODULE, eval_test_case, [Module, TestCase, Config]),
187
log("Tested ~w in ~w sec~n", [TestCase, T div Sec]),
190
eval_test_case(Mod, Fun, Config) ->
192
global:register_name(wx_test_case_sup, self()),
193
Flag = process_flag(trap_exit, true),
194
Pid = spawn_link(?MODULE, test_case_evaluator, [Mod, Fun, [Config]]),
195
R = wait_for_evaluator(Pid, Mod, Fun, Config),
196
global:unregister_name(wx_test_case_sup),
197
process_flag(trap_exit, Flag),
200
test_case_evaluator(Mod, Fun, [Config]) ->
201
NewConfig = Mod:init_per_testcase(Fun, Config),
202
R = apply(Mod, Fun, [NewConfig]),
203
Mod:fin_per_testcase(Fun, NewConfig),
204
exit({test_case_ok, R}).
206
wait_for_evaluator(Pid, Mod, Fun, Config) ->
208
{'EXIT', Pid, {test_case_ok, _PidRes}} ->
215
{Res, {Mod, Fun}, Errors};
216
{'EXIT', Pid, {skipped, Reason}} ->
217
log("<WARNING> Test case ~w skipped, because ~p~n",
218
[{Mod, Fun}, Reason]),
219
Mod:fin_per_testcase(Fun, Config),
220
{skip, {Mod, Fun}, Reason};
221
{'EXIT', Pid, Reason} ->
222
log("<ERROR> Eval process ~w exited, because ~p~n",
223
[{Mod, Fun}, Reason]),
224
Mod:fin_per_testcase(Fun, Config),
225
{crash, {Mod, Fun}, Reason}
229
receive Msg -> [Msg | flush()]
234
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%