summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTao Bao <tbao@google.com>2018-05-04 06:53:11 +0200
committerTao Bao <tbao@google.com>2018-05-09 00:53:56 +0200
commit6cd816859e462e8741ced8dc24c00b7cd8304e2a (patch)
treeb4e2e9919eb1d0ed11c6dd330d42ffacd71b3db1 /tests
parentMerge "screen_ui: Drop the parameter in CheckBackgroundTextImages." (diff)
downloadandroid_bootable_recovery-6cd816859e462e8741ced8dc24c00b7cd8304e2a.tar
android_bootable_recovery-6cd816859e462e8741ced8dc24c00b7cd8304e2a.tar.gz
android_bootable_recovery-6cd816859e462e8741ced8dc24c00b7cd8304e2a.tar.bz2
android_bootable_recovery-6cd816859e462e8741ced8dc24c00b7cd8304e2a.tar.lz
android_bootable_recovery-6cd816859e462e8741ced8dc24c00b7cd8304e2a.tar.xz
android_bootable_recovery-6cd816859e462e8741ced8dc24c00b7cd8304e2a.tar.zst
android_bootable_recovery-6cd816859e462e8741ced8dc24c00b7cd8304e2a.zip
Diffstat (limited to 'tests')
-rw-r--r--tests/Android.mk1
-rw-r--r--tests/testdata/font.pngbin0 -> 24437 bytes
-rw-r--r--tests/testdata/loop00000.pngbin0 -> 5900 bytes
-rw-r--r--tests/unit/screen_ui_test.cpp167
4 files changed, 168 insertions, 0 deletions
diff --git a/tests/Android.mk b/tests/Android.mk
index 538ae63e2..7234b521a 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -27,6 +27,7 @@ LOCAL_STATIC_LIBRARIES := \
libminui \
libotautil \
libupdater \
+ libpng \
libziparchive \
libutils \
libz \
diff --git a/tests/testdata/font.png b/tests/testdata/font.png
new file mode 100644
index 000000000..d95408a93
--- /dev/null
+++ b/tests/testdata/font.png
Binary files differ
diff --git a/tests/testdata/loop00000.png b/tests/testdata/loop00000.png
new file mode 100644
index 000000000..0e11c0100
--- /dev/null
+++ b/tests/testdata/loop00000.png
Binary files differ
diff --git a/tests/unit/screen_ui_test.cpp b/tests/unit/screen_ui_test.cpp
index e47d7054b..ff8a35d6c 100644
--- a/tests/unit/screen_ui_test.cpp
+++ b/tests/unit/screen_ui_test.cpp
@@ -16,11 +16,19 @@
#include <stddef.h>
+#include <functional>
+#include <map>
+#include <memory>
#include <string>
#include <vector>
+#include <android-base/logging.h>
#include <gtest/gtest.h>
+#include "common/test_constants.h"
+#include "device.h"
+#include "otautil/paths.h"
+#include "private/resources.h"
#include "screen_ui.h"
static const std::vector<std::string> HEADERS{ "header" };
@@ -185,3 +193,162 @@ TEST(ScreenUITest, WearMenuSelectItemsOverflow) {
ASSERT_EQ(0u, menu.MenuStart());
ASSERT_EQ(3u, menu.MenuEnd());
}
+
+static constexpr int kMagicAction = 101;
+
+enum class KeyCode : int {
+ TIMEOUT = -1,
+ NO_OP = 0,
+ UP = 1,
+ DOWN = 2,
+ ENTER = 3,
+ MAGIC = 1001,
+ LAST,
+};
+
+static const std::map<KeyCode, int> kKeyMapping{
+ // clang-format off
+ { KeyCode::NO_OP, Device::kNoAction },
+ { KeyCode::UP, Device::kHighlightUp },
+ { KeyCode::DOWN, Device::kHighlightDown },
+ { KeyCode::ENTER, Device::kInvokeItem },
+ { KeyCode::MAGIC, kMagicAction },
+ // clang-format on
+};
+
+class TestableScreenRecoveryUI : public ScreenRecoveryUI {
+ public:
+ int WaitKey() override;
+
+ void SetKeyBuffer(const std::vector<KeyCode>& buffer);
+
+ int KeyHandler(int key, bool visible) const;
+
+ bool GetRtlLocale() const {
+ return rtl_locale_;
+ }
+
+ private:
+ std::vector<KeyCode> key_buffer_;
+ size_t key_buffer_index_;
+};
+
+void TestableScreenRecoveryUI::SetKeyBuffer(const std::vector<KeyCode>& buffer) {
+ key_buffer_ = buffer;
+ key_buffer_index_ = 0;
+}
+
+int TestableScreenRecoveryUI::KeyHandler(int key, bool) const {
+ KeyCode key_code = static_cast<KeyCode>(key);
+ if (kKeyMapping.find(key_code) != kKeyMapping.end()) {
+ return kKeyMapping.at(key_code);
+ }
+ return Device::kNoAction;
+}
+
+int TestableScreenRecoveryUI::WaitKey() {
+ CHECK_LT(key_buffer_index_, key_buffer_.size());
+ return static_cast<int>(key_buffer_[key_buffer_index_++]);
+}
+
+class ScreenRecoveryUITest : public ::testing::Test {
+ protected:
+ const std::string kTestLocale = "en-US";
+ const std::string kTestRtlLocale = "ar";
+ const std::string kTestRtlLocaleWithSuffix = "ar_EG";
+
+ void SetUp() override {
+ ui_ = std::make_unique<TestableScreenRecoveryUI>();
+
+ std::string testdata_dir = from_testdata_base("");
+ Paths::Get().set_resource_dir(testdata_dir);
+ res_set_resource_dir(testdata_dir);
+
+ ASSERT_TRUE(ui_->Init(kTestLocale));
+ }
+
+ std::unique_ptr<TestableScreenRecoveryUI> ui_;
+};
+
+TEST_F(ScreenRecoveryUITest, Init) {
+ ASSERT_EQ(kTestLocale, ui_->GetLocale());
+ ASSERT_FALSE(ui_->GetRtlLocale());
+ ASSERT_FALSE(ui_->IsTextVisible());
+ ASSERT_FALSE(ui_->WasTextEverVisible());
+}
+
+TEST_F(ScreenRecoveryUITest, ShowText) {
+ ASSERT_FALSE(ui_->IsTextVisible());
+ ui_->ShowText(true);
+ ASSERT_TRUE(ui_->IsTextVisible());
+ ASSERT_TRUE(ui_->WasTextEverVisible());
+
+ ui_->ShowText(false);
+ ASSERT_FALSE(ui_->IsTextVisible());
+ ASSERT_TRUE(ui_->WasTextEverVisible());
+}
+
+TEST_F(ScreenRecoveryUITest, RtlLocale) {
+ ASSERT_TRUE(ui_->Init(kTestRtlLocale));
+ ASSERT_TRUE(ui_->GetRtlLocale());
+
+ ASSERT_TRUE(ui_->Init(kTestRtlLocaleWithSuffix));
+ ASSERT_TRUE(ui_->GetRtlLocale());
+}
+
+TEST_F(ScreenRecoveryUITest, ShowMenu) {
+ ui_->SetKeyBuffer({
+ KeyCode::UP,
+ KeyCode::DOWN,
+ KeyCode::UP,
+ KeyCode::DOWN,
+ KeyCode::ENTER,
+ });
+ ASSERT_EQ(3u, ui_->ShowMenu(HEADERS, ITEMS, 3, true,
+ std::bind(&TestableScreenRecoveryUI::KeyHandler, ui_.get(),
+ std::placeholders::_1, std::placeholders::_2)));
+
+ ui_->SetKeyBuffer({
+ KeyCode::UP,
+ KeyCode::UP,
+ KeyCode::NO_OP,
+ KeyCode::NO_OP,
+ KeyCode::UP,
+ KeyCode::ENTER,
+ });
+ ASSERT_EQ(2u, ui_->ShowMenu(HEADERS, ITEMS, 0, true,
+ std::bind(&TestableScreenRecoveryUI::KeyHandler, ui_.get(),
+ std::placeholders::_1, std::placeholders::_2)));
+}
+
+TEST_F(ScreenRecoveryUITest, ShowMenu_NotMenuOnly) {
+ ui_->SetKeyBuffer({
+ KeyCode::MAGIC,
+ });
+ ASSERT_EQ(static_cast<size_t>(kMagicAction),
+ ui_->ShowMenu(HEADERS, ITEMS, 3, false,
+ std::bind(&TestableScreenRecoveryUI::KeyHandler, ui_.get(),
+ std::placeholders::_1, std::placeholders::_2)));
+}
+
+TEST_F(ScreenRecoveryUITest, ShowMenu_TimedOut) {
+ ui_->SetKeyBuffer({
+ KeyCode::TIMEOUT,
+ });
+ ASSERT_EQ(static_cast<size_t>(-1), ui_->ShowMenu(HEADERS, ITEMS, 3, true, nullptr));
+}
+
+TEST_F(ScreenRecoveryUITest, ShowMenu_TimedOut_TextWasEverVisible) {
+ ui_->ShowText(true);
+ ui_->ShowText(false);
+ ASSERT_TRUE(ui_->WasTextEverVisible());
+
+ ui_->SetKeyBuffer({
+ KeyCode::TIMEOUT,
+ KeyCode::DOWN,
+ KeyCode::ENTER,
+ });
+ ASSERT_EQ(4u, ui_->ShowMenu(HEADERS, ITEMS, 3, true,
+ std::bind(&TestableScreenRecoveryUI::KeyHandler, ui_.get(),
+ std::placeholders::_1, std::placeholders::_2)));
+}