49ebc75ea3516df7888b56600d1ab5af1b591b29
[platform/core/system/deviced.git] / src / auto-test / power.c
1 /*
2  * test
3  *
4  * Copyright (c) 2013 Samsung Electronics Co., Ltd.
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 #include "test.h"
19
20 #define METHOD_POWEROFF             "PowerOff"
21 #define METHOD_POWEROFF_WITH_OPTION "PowerOffWithOption"
22
23 static bool set_reboot_method(const char *method, GVariant *param)
24 {
25         GVariant *msg;
26         int val, err;
27         bool ret = FALSE;
28
29         err = dbus_handle_method_sync_with_reply_var(DEVICED_BUS_NAME,
30                                                 DEVICED_PATH_POWEROFF,
31                                                 DEVICED_INTERFACE_POWEROFF,
32                                                 method,
33                                                 param,
34                                                 &msg);
35         if (err < 0) {
36                 _E("fail (%s): no reply", method);
37                 return ret;
38         }
39
40         if (!g_variant_get_safe(msg, "(i)", &val))
41                 _E("fail (%s): no message", method);
42         else {
43                 if ((val == -ENOTSUP) || (val == -ENOSYS)) {
44                         _I("Not supported feature! (%s): %d", method, val);
45                         ret = TRUE;
46                 } else if (val < 0) {
47                         _E("fail (%s): returned fail (%d)", method, val);
48                 } else {
49                         _I("success (%s): %d", method, val);
50                         ret = TRUE;
51                 }
52         }
53
54         g_variant_unref(msg);
55         return ret;
56 }
57
58 static bool set_reboot(char *type)
59 {
60         return set_reboot_method(METHOD_POWEROFF, g_variant_new("(s)", type));
61 }
62
63 static bool set_reboot_with_option(char *type, char *option)
64 {
65         return set_reboot_method(METHOD_POWEROFF_WITH_OPTION, g_variant_new("(ss)", type, option));
66 }
67
68 static void power_init(void *data)
69 {
70         int success = 0;
71         int fail = 0;
72
73         _I("start test");
74
75         _I("Total: %d, Success: %d, Fail: %d", success+fail, success, fail);
76 }
77
78 static void power_exit(void *data)
79 {
80         _I("end test");
81 }
82
83 static int power_unit(int argc, char **argv)
84 {
85         if (argc < 4) {
86                 int success = 0;
87                 int fail = 0;
88
89                 _I("start test");
90
91                 _I("Total: %d, Success: %d, Fail: %d", success+fail, success, fail);
92         } else if (0 == strcmp(argv[3], METHOD_POWEROFF))
93                 set_reboot(argv[4]);
94         else if (0 == strcasecmp(argv[3], METHOD_POWEROFF_WITH_OPTION))
95                 set_reboot_with_option(argv[4], argv[5]);
96         else
97                 _E("Unknown test case!!!");
98
99         return 0;
100 }
101
102 static const struct test_ops power_test_ops = {
103         .priority = TEST_PRIORITY_HIGH,
104         .name     = "power",
105         .init     = power_init,
106         .exit    = power_exit,
107         .unit    = power_unit,
108 };
109
110 TEST_OPS_REGISTER(&power_test_ops)