Commit b873f586 authored by Aga Wronska's avatar Aga Wronska Committed by Commit Bot

child user: Submit parent access code with enter key

Bug: 911326
Test: ParentAccessViewTest
Change-Id: I1be9fe83b2902a8895cc5af87b51d675e8711166
Reviewed-on: https://chromium-review.googlesource.com/c/1471494
Commit-Queue: Aga Wronska <agawronska@chromium.org>
Reviewed-by: default avatarMichael Giuffrida <michaelpg@chromium.org>
Reviewed-by: default avatarJacob Dufault <jdufault@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636265}
parent c0164ba8
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/events/event.h"
#include "ui/gfx/canvas.h" #include "ui/gfx/canvas.h"
#include "ui/gfx/color_analysis.h" #include "ui/gfx/color_analysis.h"
#include "ui/gfx/color_palette.h" #include "ui/gfx/color_palette.h"
...@@ -92,13 +93,16 @@ class ParentAccessView::AccessCodeInput : public views::View, ...@@ -92,13 +93,16 @@ class ParentAccessView::AccessCodeInput : public views::View,
public views::TextfieldController { public views::TextfieldController {
public: public:
using OnInputChange = base::RepeatingCallback<void(bool complete)>; using OnInputChange = base::RepeatingCallback<void(bool complete)>;
using OnEnter = base::RepeatingClosure;
// Builds the view for an access code that consists out of |length| digits. // Builds the view for an access code that consists out of |length| digits.
// |on_input_change| will be called upon access code digit insertion, deletion // |on_input_change| will be called upon access code digit insertion, deletion
// or change. True will be passed if the current code is complete (all digits // or change. True will be passed if the current code is complete (all digits
// have input values) and false otherwise. // have input values) and false otherwise. |on_enter| will be called when code
AccessCodeInput(int length, OnInputChange on_input_change) // is complete and user presses enter to submit it for validation.
: on_input_change_(std::move(on_input_change)) { AccessCodeInput(int length, OnInputChange on_input_change, OnEnter on_enter)
: on_input_change_(std::move(on_input_change)),
on_enter_(std::move(on_enter)) {
DCHECK_LT(0, length); DCHECK_LT(0, length);
DCHECK(on_input_change_); DCHECK(on_input_change_);
...@@ -200,6 +204,9 @@ class ParentAccessView::AccessCodeInput : public views::View, ...@@ -200,6 +204,9 @@ class ParentAccessView::AccessCodeInput : public views::View,
FocusNextField(); FocusNextField();
} else if (key_code == ui::VKEY_BACK) { } else if (key_code == ui::VKEY_BACK) {
Backspace(); Backspace();
} else if (key_code == ui::VKEY_RETURN) {
if (GetCode().has_value())
on_enter_.Run();
} }
return true; return true;
...@@ -269,6 +276,9 @@ class ParentAccessView::AccessCodeInput : public views::View, ...@@ -269,6 +276,9 @@ class ParentAccessView::AccessCodeInput : public views::View,
// and false otherwise. // and false otherwise.
OnInputChange on_input_change_; OnInputChange on_input_change_;
// To be called when user pressed enter to submit.
OnEnter on_enter_;
// An active/focused input field index. Incoming digit will be inserted here. // An active/focused input field index. Incoming digit will be inserted here.
int active_input_index_ = 0; int active_input_index_ = 0;
...@@ -397,6 +407,8 @@ ParentAccessView::ParentAccessView(const AccountId& account_id, ...@@ -397,6 +407,8 @@ ParentAccessView::ParentAccessView(const AccountId& account_id,
access_code_view_ = access_code_view_ =
new AccessCodeInput(kParentAccessCodePinLength, new AccessCodeInput(kParentAccessCodePinLength,
base::BindRepeating(&ParentAccessView::OnInputChange, base::BindRepeating(&ParentAccessView::OnInputChange,
base::Unretained(this)),
base::BindRepeating(&ParentAccessView::SubmitCode,
base::Unretained(this))); base::Unretained(this)));
AddChildView(access_code_view_); AddChildView(access_code_view_);
......
...@@ -70,9 +70,6 @@ class ParentAccessViewTest : public LoginTestBase { ...@@ -70,9 +70,6 @@ class ParentAccessViewTest : public LoginTestBase {
const AccountId account_id_; const AccountId account_id_;
std::unique_ptr<MockLoginScreenClient> login_client_; std::unique_ptr<MockLoginScreenClient> login_client_;
// Submitted access code.
base::Optional<std::string> code_;
// Number of times the view was dismissed with back button. // Number of times the view was dismissed with back button.
int back_action_ = 0; int back_action_ = 0;
...@@ -140,6 +137,64 @@ TEST_F(ParentAccessViewTest, Numpad) { ...@@ -140,6 +137,64 @@ TEST_F(ParentAccessViewTest, Numpad) {
EXPECT_EQ(1, successful_validation_); EXPECT_EQ(1, successful_validation_);
} }
// Tests that access code can be submitted with press of 'enter' key.
TEST_F(ParentAccessViewTest, SubmitWithEnter) {
ParentAccessView::TestApi test_api(view_);
EXPECT_FALSE(test_api.submit_button()->enabled());
ui::test::EventGenerator* generator = GetEventGenerator();
for (int i = 0; i < 6; ++i) {
generator->PressKey(ui::KeyboardCode(ui::KeyboardCode::VKEY_0 + i),
ui::EF_NONE);
}
EXPECT_TRUE(test_api.submit_button()->enabled());
login_client_->set_validate_parent_access_code_result(true);
EXPECT_CALL(*login_client_,
ValidateParentAccessCode_(account_id_, "012345", testing::_))
.Times(1);
generator->PressKey(ui::KeyboardCode::VKEY_RETURN, ui::EF_NONE);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, successful_validation_);
}
// Tests that 'enter' key does not submit incomplete code.
TEST_F(ParentAccessViewTest, PressEnterOnIncompleteCode) {
ParentAccessView::TestApi test_api(view_);
EXPECT_FALSE(test_api.submit_button()->enabled());
// Enter incomplete code.
ui::test::EventGenerator* generator = GetEventGenerator();
for (int i = 0; i < 5; ++i) {
generator->PressKey(ui::KeyboardCode(ui::KeyboardCode::VKEY_0 + i),
ui::EF_NONE);
}
EXPECT_FALSE(test_api.submit_button()->enabled());
login_client_->set_validate_parent_access_code_result(true);
EXPECT_CALL(*login_client_, ValidateParentAccessCode_).Times(0);
// Pressing enter should not submit incomplete code.
generator->PressKey(ui::KeyboardCode::VKEY_RETURN, ui::EF_NONE);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, successful_validation_);
// Fill in last digit of the code.
generator->PressKey(ui::KeyboardCode(ui::KeyboardCode::VKEY_9), ui::EF_NONE);
EXPECT_TRUE(test_api.submit_button()->enabled());
login_client_->set_validate_parent_access_code_result(true);
EXPECT_CALL(*login_client_,
ValidateParentAccessCode_(account_id_, "012349", testing::_))
.Times(1);
// Now the code should be submitted with enter key.
generator->PressKey(ui::KeyboardCode::VKEY_RETURN, ui::EF_NONE);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, successful_validation_);
}
// Tests that backspace button works. // Tests that backspace button works.
TEST_F(ParentAccessViewTest, Backspace) { TEST_F(ParentAccessViewTest, Backspace) {
ParentAccessView::TestApi test_api(view_); ParentAccessView::TestApi test_api(view_);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment