1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2010-2017. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20
21-module(verify_config_cth).
22
23-include_lib("common_test/src/ct_util.hrl").
24
25%% CT Hooks
26-compile(export_all).
27
28-define(val(K, L), proplists:get_value(K, L)).
29
30id(Opts) ->
31    ?MODULE.
32
33init(Id, Opts) ->
34    {ok, State} = empty_cth:init(Id, Opts),
35    {ok, State}.
36
37pre_init_per_suite(Suite, Config, State) ->
38    ct_no_config_SUITE = ct:get_config(suite_cfg),
39    empty_cth:pre_init_per_suite(Suite,
40				 [{pre_init_per_suite,true} | Config],
41				 State).
42
43post_init_per_suite(Suite,Config,Return,State) ->
44    true = ?val(pre_init_per_suite, Return),
45    ct_no_config_SUITE = ct:get_config(suite_cfg),
46    empty_cth:post_init_per_suite(Suite,
47				  Config,
48				  [{post_init_per_suite,true} | Return],
49				  State).
50
51pre_end_per_suite(Suite,Config,State) ->
52    true = ?val(post_init_per_suite, Config),
53    ct_no_config_SUITE = ct:get_config(suite_cfg),
54    empty_cth:pre_end_per_suite(Suite,
55				[{pre_end_per_suite,true} | Config],
56				State).
57
58post_end_per_suite(Suite,Config,Return,State) ->
59    true = ?val(pre_end_per_suite, Config),
60    ct_no_config_SUITE = ct:get_config(suite_cfg),
61    empty_cth:post_end_per_suite(Suite,Config,Return,State).
62
63pre_init_per_group(Suite,Group,Config,State) ->
64    true = ?val(post_init_per_suite, Config),
65    ct_no_config_SUITE = ct:get_config(suite_cfg),
66    test_group = ct:get_config(group_cfg),
67    empty_cth:pre_init_per_group(Suite,Group,
68				 [{pre_init_per_group,true} | Config],
69				 State).
70
71post_init_per_group(Suite,Group,Config,Return,State) ->
72    true = ?val(pre_init_per_group, Return),
73    test_group = ct:get_config(group_cfg),
74    empty_cth:post_init_per_group(Suite,Group,
75				  Config,
76				  [{post_init_per_group,true} | Return],
77				  State).
78
79pre_end_per_group(Suite,Group,Config,State) ->
80    true = ?val(post_init_per_group, Config),
81    ct_no_config_SUITE = ct:get_config(suite_cfg),
82    test_group = ct:get_config(group_cfg),
83    empty_cth:pre_end_per_group(Suite,Group,
84				[{pre_end_per_group,true} | Config],
85				State).
86
87post_end_per_group(Suite,Group,Config,Return,State) ->
88    true = ?val(pre_end_per_group, Config),
89    ct_no_config_SUITE = ct:get_config(suite_cfg),
90    test_group = ct:get_config(group_cfg),
91    empty_cth:post_end_per_group(Suite,Group,Config,Return,State).
92
93pre_init_per_testcase(Suite,TC,Config,State) ->
94    true = ?val(post_init_per_suite, Config),
95    case ?val(name, ?val(tc_group_properties, Config)) of
96	undefined ->
97	    ok;
98	_ ->
99	    true = ?val(post_init_per_group, Config),
100	    test_group = ct:get_config(group_cfg)
101    end,
102    ct_no_config_SUITE = ct:get_config(suite_cfg),
103    CfgKey = list_to_atom(atom_to_list(TC) ++ "_cfg"),
104    TC = ct:get_config(CfgKey),
105    empty_cth:pre_init_per_testcase(Suite,TC,
106				    [{pre_init_per_testcase,true} | Config],
107				    State).
108
109%%! TODO: Verify Config also in post_init and pre_end!
110
111post_init_per_testcase(Suite,TC,Config,Return,State) ->
112    empty_cth:post_init_per_testcase(Suite,TC,Config,Return,State).
113
114pre_end_per_testcase(Suite,TC,Config,State) ->
115    empty_cth:pre_end_per_testcase(Suite,TC,Config,State).
116
117post_end_per_testcase(Suite,TC,Config,Return,State) ->
118    true = ?val(post_init_per_suite, Config),
119    true = ?val(pre_init_per_testcase, Config),
120    case ?val(name, ?val(tc_group_properties, Config)) of
121	undefined ->
122	    ok;
123	_ ->
124	    true = ?val(post_init_per_group, Config),
125	    test_group = ct:get_config(group_cfg)
126    end,
127    ct_no_config_SUITE = ct:get_config(suite_cfg),
128    CfgKey = list_to_atom(atom_to_list(TC) ++ "_cfg"),
129    TC = ct:get_config(CfgKey),
130    empty_cth:post_end_per_testcase(Suite,TC,Config,Return,State).
131
132on_tc_fail(Suite,TC, Reason, State) ->
133    empty_cth:on_tc_fail(Suite,TC,Reason,State).
134
135on_tc_skip(Suite,TC, Reason, State) ->
136    empty_cth:on_tc_skip(Suite,TC,Reason,State).
137
138terminate(State) ->
139    empty_cth:terminate(State).
140