summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVic Yang <victoryang@chromium.org>2013-07-11 17:00:04 +0800
committerChromeBot <chrome-bot@google.com>2013-07-11 22:32:53 -0700
commit396cb8e69e11bd474864d203b2f67cb95e7ae0e5 (patch)
tree0a3f78b2ef4bba8353c4200596553c669b7d68e2
parent786437229728ffeeda19d533941d813f74ba9441 (diff)
downloadchrome-ec-396cb8e69e11bd474864d203b2f67cb95e7ae0e5.tar.gz
Add console editing unit test
This tests console editing and history functionality. BUG=chrome-os-partner:19236 TEST=Pass the test BRANCH=None Change-Id: Ia3edf2dd627d081fbcae7f775992d7570e5e1dd0 Signed-off-by: Vic Yang <victoryang@chromium.org> Reviewed-on: https://gerrit.chromium.org/gerrit/61555 Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r--test/build.mk3
-rw-r--r--test/console_edit.c224
-rw-r--r--test/console_edit.tasklist17
3 files changed, 243 insertions, 1 deletions
diff --git a/test/build.mk b/test/build.mk
index 5145c54e73..b9c24ddaf0 100644
--- a/test/build.mk
+++ b/test/build.mk
@@ -29,8 +29,9 @@ test-list-$(BOARD_wolf)=
# Emulator tests
test-list-host=mutex pingpong utils kb_scan kb_mkbp lid_sw power_button hooks
-test-list-host+=thermal flash queue kb_8042 extpwr_gpio
+test-list-host+=thermal flash queue kb_8042 extpwr_gpio console_edit
+console_edit-y=console_edit.o
extpwr_gpio-y=extpwr_gpio.o
flash-y=flash.o
hooks-y=hooks.o
diff --git a/test/console_edit.c b/test/console_edit.c
new file mode 100644
index 0000000000..1649102477
--- /dev/null
+++ b/test/console_edit.c
@@ -0,0 +1,224 @@
+/* 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 console editing and history.
+ */
+
+#include "common.h"
+#include "console.h"
+#include "test_util.h"
+#include "timer.h"
+#include "util.h"
+
+static int cmd_1_call_cnt;
+static int cmd_2_call_cnt;
+
+static int command_test_1(int argc, char **argv)
+{
+ cmd_1_call_cnt++;
+ return EC_SUCCESS;
+}
+DECLARE_CONSOLE_COMMAND(test1, command_test_1, NULL, NULL, NULL);
+
+static int command_test_2(int argc, char **argv)
+{
+ cmd_2_call_cnt++;
+ return EC_SUCCESS;
+}
+DECLARE_CONSOLE_COMMAND(test2, command_test_2, NULL, NULL, NULL);
+
+/*****************************************************************************/
+/* Test utilities */
+
+enum arrow_key_t {
+ ARROW_UP = 0,
+ ARROW_DOWN,
+ ARROW_RIGHT,
+ ARROW_LEFT,
+};
+
+static void arrow_key(enum arrow_key_t k, int repeat)
+{
+ static char seq[4] = {0x1B, '[', 0, 0};
+ seq[2] = 'A' + k;
+ while (repeat--)
+ UART_INJECT(seq);
+}
+
+static void delete_key(void)
+{
+ UART_INJECT("\x1b[3~");
+}
+
+static void home_key(void)
+{
+ UART_INJECT("\x1b[1~");
+}
+
+static void end_key(void)
+{
+ UART_INJECT("\x1bOF");
+}
+
+static void ctrl_key(char c)
+{
+ static char seq[2] = {0, 0};
+ seq[0] = c - '@';
+ UART_INJECT(seq);
+}
+
+/*****************************************************************************/
+/* Tests */
+
+static int test_backspace(void)
+{
+ cmd_1_call_cnt = 0;
+ UART_INJECT("testx\b1\n");
+ msleep(30);
+ TEST_CHECK(cmd_1_call_cnt == 1);
+}
+
+static int test_insert_char(void)
+{
+ cmd_1_call_cnt = 0;
+ UART_INJECT("tet1");
+ arrow_key(ARROW_LEFT, 2);
+ UART_INJECT("s\n");
+ msleep(30);
+ TEST_CHECK(cmd_1_call_cnt == 1);
+}
+
+static int test_delete_char(void)
+{
+ cmd_1_call_cnt = 0;
+ UART_INJECT("testt1");
+ arrow_key(ARROW_LEFT, 1);
+ UART_INJECT("\b\n");
+ msleep(30);
+ TEST_CHECK(cmd_1_call_cnt == 1);
+}
+
+static int test_insert_delete_char(void)
+{
+ cmd_1_call_cnt = 0;
+ UART_INJECT("txet1");
+ arrow_key(ARROW_LEFT, 4);
+ delete_key();
+ arrow_key(ARROW_RIGHT, 1);
+ UART_INJECT("s\n");
+ msleep(30);
+ TEST_CHECK(cmd_1_call_cnt == 1);
+}
+
+static int test_home_end_key(void)
+{
+ cmd_1_call_cnt = 0;
+ UART_INJECT("est");
+ home_key();
+ UART_INJECT("t");
+ end_key();
+ UART_INJECT("1\n");
+ msleep(30);
+ TEST_CHECK(cmd_1_call_cnt == 1);
+}
+
+static int test_ctrl_k(void)
+{
+ cmd_1_call_cnt = 0;
+ UART_INJECT("test123");
+ arrow_key(ARROW_LEFT, 2);
+ ctrl_key('K');
+ UART_INJECT("\n");
+ msleep(30);
+ TEST_CHECK(cmd_1_call_cnt == 1);
+}
+
+static int test_history_up(void)
+{
+ cmd_1_call_cnt = 0;
+ UART_INJECT("test1\n");
+ msleep(30);
+ arrow_key(ARROW_UP, 1);
+ UART_INJECT("\n");
+ msleep(30);
+ TEST_CHECK(cmd_1_call_cnt == 2);
+}
+
+static int test_history_up_up(void)
+{
+ cmd_1_call_cnt = 0;
+ cmd_2_call_cnt = 0;
+ UART_INJECT("test1\n");
+ msleep(30);
+ UART_INJECT("test2\n");
+ msleep(30);
+ arrow_key(ARROW_UP, 2);
+ UART_INJECT("\n");
+ msleep(30);
+ TEST_CHECK(cmd_1_call_cnt == 2);
+ TEST_CHECK(cmd_2_call_cnt == 1);
+}
+
+static int test_history_up_up_down(void)
+{
+ cmd_1_call_cnt = 0;
+ cmd_2_call_cnt = 0;
+ UART_INJECT("test1\n");
+ msleep(30);
+ UART_INJECT("test2\n");
+ msleep(30);
+ arrow_key(ARROW_UP, 2);
+ arrow_key(ARROW_DOWN, 1);
+ UART_INJECT("\n");
+ msleep(30);
+ TEST_CHECK(cmd_1_call_cnt == 1);
+ TEST_CHECK(cmd_2_call_cnt == 2);
+}
+
+static int test_history_edit(void)
+{
+ cmd_1_call_cnt = 0;
+ cmd_2_call_cnt = 0;
+ UART_INJECT("test1\n");
+ msleep(30);
+ arrow_key(ARROW_UP, 1);
+ UART_INJECT("\b2\n");
+ msleep(30);
+ TEST_CHECK(cmd_1_call_cnt == 1);
+ TEST_CHECK(cmd_2_call_cnt == 1);
+}
+
+static int test_history_stash(void)
+{
+ cmd_1_call_cnt = 0;
+ cmd_2_call_cnt = 0;
+ UART_INJECT("test1\n");
+ msleep(30);
+ UART_INJECT("test");
+ arrow_key(ARROW_UP, 1);
+ arrow_key(ARROW_DOWN, 1);
+ UART_INJECT("2\n");
+ msleep(30);
+ TEST_CHECK(cmd_1_call_cnt == 1);
+ TEST_CHECK(cmd_2_call_cnt == 1);
+}
+
+void run_test(void)
+{
+ test_reset();
+
+ RUN_TEST(test_backspace);
+ RUN_TEST(test_insert_char);
+ RUN_TEST(test_delete_char);
+ RUN_TEST(test_insert_delete_char);
+ RUN_TEST(test_home_end_key);
+ RUN_TEST(test_ctrl_k);
+ RUN_TEST(test_history_up);
+ RUN_TEST(test_history_up_up);
+ RUN_TEST(test_history_up_up_down);
+ RUN_TEST(test_history_edit);
+ RUN_TEST(test_history_stash);
+
+ test_print_result();
+}
diff --git a/test/console_edit.tasklist b/test/console_edit.tasklist
new file mode 100644
index 0000000000..26cfc53453
--- /dev/null
+++ b/test/console_edit.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 */