summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--board/host/board.c1
-rw-r--r--board/host/board.h2
-rw-r--r--chip/host/gpio.c5
-rw-r--r--common/lid_switch.c2
-rw-r--r--test/build.mk3
-rw-r--r--test/kb_mkbp.c5
-rw-r--r--test/kb_scan.c7
-rw-r--r--test/lid_sw.c127
-rw-r--r--test/lid_sw.tasklist17
9 files changed, 163 insertions, 6 deletions
diff --git a/board/host/board.c b/board/host/board.c
index a3b8c28aca..c646340534 100644
--- a/board/host/board.c
+++ b/board/host/board.c
@@ -9,4 +9,5 @@
const struct gpio_info gpio_list[GPIO_COUNT] = {
{"EC_INT", 0, 0, 0, 0},
+ {"LID_OPEN", 0, 0, 0, 0},
};
diff --git a/board/host/board.h b/board/host/board.h
index de7049bd9d..1997a23a7e 100644
--- a/board/host/board.h
+++ b/board/host/board.h
@@ -11,9 +11,11 @@
#define CONFIG_HOST_EMU
#define CONFIG_HOSTCMD
#define CONFIG_KEYBOARD_PROTOCOL_MKBP
+#define CONFIG_LID_SWITCH
enum gpio_signal {
GPIO_EC_INT,
+ GPIO_LID_OPEN,
GPIO_COUNT
};
diff --git a/chip/host/gpio.c b/chip/host/gpio.c
index 03cfaa4aed..64953d7682 100644
--- a/chip/host/gpio.c
+++ b/chip/host/gpio.c
@@ -22,3 +22,8 @@ test_mockable void gpio_set_level(enum gpio_signal signal, int value)
{
/* Nothing */
}
+
+test_mockable int gpio_enable_interrupt(enum gpio_signal signal)
+{
+ return EC_SUCCESS;
+}
diff --git a/common/lid_switch.c b/common/lid_switch.c
index 8e1e1bf082..80e3dbfbaa 100644
--- a/common/lid_switch.c
+++ b/common/lid_switch.c
@@ -64,7 +64,7 @@ static void lid_switch_close(void)
host_set_single_event(EC_HOST_EVENT_LID_CLOSED);
}
-int lid_is_open(void)
+test_mockable int lid_is_open(void)
{
return debounced_lid_open;
}
diff --git a/test/build.mk b/test/build.mk
index 2c61e80e7d..aa964a71fd 100644
--- a/test/build.mk
+++ b/test/build.mk
@@ -25,11 +25,12 @@ test-list-$(BOARD_link)=
test-list-$(BOARD_slippy)=
# Emulator tests
-test-list-host=mutex pingpong utils kb_scan
+test-list-host=mutex pingpong utils kb_scan kb_mkbp lid_sw
flash-y=flash.o
kb_mkbp-y=kb_mkbp.o
kb_scan-y=kb_scan.o
+lid_sw-y=lid_sw.o
mutex-y=mutex.o
pingpong-y=pingpong.o
powerdemo-y=powerdemo.o
diff --git a/test/kb_mkbp.c b/test/kb_mkbp.c
index 57a4d9cc67..a8e57bfe57 100644
--- a/test/kb_mkbp.c
+++ b/test/kb_mkbp.c
@@ -36,6 +36,11 @@ void gpio_set_level(enum gpio_signal signal, int level)
ec_int_level = !!level;
}
+int lid_is_open(void)
+{
+ return 1;
+}
+
/*****************************************************************************/
/* Test utilities */
diff --git a/test/kb_scan.c b/test/kb_scan.c
index c242c43e09..be08e838ff 100644
--- a/test/kb_scan.c
+++ b/test/kb_scan.c
@@ -11,6 +11,7 @@
#include "gpio.h"
#include "keyboard_raw.h"
#include "keyboard_scan.h"
+#include "lid_switch.h"
#include "task.h"
#include "timer.h"
#include "util.h"
@@ -53,11 +54,9 @@ static int error_count;
static int lid_open;
#ifdef CONFIG_LID_SWITCH
-int gpio_get_level(enum gpio_signal signal)
+int lid_is_open(void)
{
- if (signal == GPIO_LID_OPEN)
- return lid_open;
- return 0;
+ return lid_open;
}
#endif
diff --git a/test/lid_sw.c b/test/lid_sw.c
new file mode 100644
index 0000000000..4993147dcb
--- /dev/null
+++ b/test/lid_sw.c
@@ -0,0 +1,127 @@
+/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Test lid switch.
+ */
+
+#include "common.h"
+#include "console.h"
+#include "hooks.h"
+#include "host_command.h"
+#include "lid_switch.h"
+#include "timer.h"
+#include "util.h"
+
+static int error_count;
+
+static int mock_lid;
+static int lid_hook_count;
+
+#define RUN_TEST(n) \
+ do { \
+ ccprintf("Running %s...", #n); \
+ cflush(); \
+ if (n() == EC_SUCCESS) { \
+ ccputs("OK\n"); \
+ } else { \
+ ccputs("Fail\n"); \
+ error_count++; \
+ } \
+ } while (0)
+
+#define TEST_ASSERT(n) \
+ do { \
+ if (!(n)) \
+ return EC_ERROR_UNKNOWN; \
+ } while (0)
+
+int gpio_get_level(enum gpio_signal signal)
+{
+ if (signal == GPIO_LID_OPEN)
+ return mock_lid;
+ return 0;
+}
+
+static void lid_change_hook(void)
+{
+ lid_hook_count++;
+}
+DECLARE_HOOK(HOOK_LID_CHANGE, lid_change_hook, HOOK_PRIO_DEFAULT);
+
+static int test_hook(void)
+{
+ /* Close lid for testing */
+ mock_lid = 0;
+ lid_interrupt(GPIO_LID_OPEN);
+ msleep(100);
+ lid_hook_count = 0;
+ host_clear_events(0xffffffff);
+
+ mock_lid = 1;
+ lid_interrupt(GPIO_LID_OPEN);
+ msleep(50);
+ TEST_ASSERT(lid_hook_count == 1);
+ TEST_ASSERT(lid_is_open());
+ TEST_ASSERT(host_get_events() &
+ EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN));
+
+ mock_lid = 0;
+ lid_interrupt(GPIO_LID_OPEN);
+ msleep(50);
+ TEST_ASSERT(lid_hook_count == 2);
+ TEST_ASSERT(!lid_is_open());
+ TEST_ASSERT(host_get_events() &
+ EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
+
+ return EC_SUCCESS;
+}
+
+static int test_debounce(void)
+{
+ /* Close lid for testing */
+ mock_lid = 0;
+ lid_interrupt(GPIO_LID_OPEN);
+ msleep(100);
+ lid_hook_count = 0;
+ host_clear_events(0xffffffff);
+
+ mock_lid = 1;
+ lid_interrupt(GPIO_LID_OPEN);
+ msleep(20);
+ TEST_ASSERT(lid_hook_count == 0);
+ TEST_ASSERT(!lid_is_open());
+ TEST_ASSERT(!(host_get_events() &
+ EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN)));
+
+ mock_lid = 0;
+ lid_interrupt(GPIO_LID_OPEN);
+ msleep(50);
+ TEST_ASSERT(lid_hook_count == 0);
+ TEST_ASSERT(!lid_is_open());
+ TEST_ASSERT(!(host_get_events() &
+ EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN)));
+
+ return EC_SUCCESS;
+}
+
+void run_test(void)
+{
+ error_count = 0;
+
+ RUN_TEST(test_hook);
+ RUN_TEST(test_debounce);
+
+ if (error_count)
+ ccprintf("Fail!\n", error_count);
+ else
+ ccprintf("Pass!\n");
+}
+
+static int command_run_test(int argc, char **argv)
+{
+ run_test();
+ return EC_SUCCESS;
+}
+DECLARE_CONSOLE_COMMAND(runtest, command_run_test,
+ NULL, NULL, NULL);
diff --git a/test/lid_sw.tasklist b/test/lid_sw.tasklist
new file mode 100644
index 0000000000..26cfc53453
--- /dev/null
+++ b/test/lid_sw.tasklist
@@ -0,0 +1,17 @@
+/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/**
+ * List of enabled tasks in the priority order
+ *
+ * The first one has the lowest priority.
+ *
+ * For each task, use the macro TASK_TEST(n, r, d, s) where :
+ * 'n' in the name of the task
+ * 'r' in the main routine of the task
+ * 'd' in an opaque parameter passed to the routine at startup
+ * 's' is the stack size in bytes; must be a multiple of 8
+ */
+#define CONFIG_TEST_TASK_LIST /* No test task */