Commit 14bc8d2e authored by Tetsui Ohkubo's avatar Tetsui Ohkubo Committed by Commit Bot

Add unit tests to TopShortcutsView

This CL ports unit tests from TrayTilesTest.
Also this CL fixes DCHECK failures and crash in logged out state.

UnifiedSystemTray design doc: go/cros-qs-restyling

TEST=TopShortcutsViewTest(ash_unittests)
BUG=none

Change-Id: Id32540a3213866aa86b89025f1aa7bc97bae989b
Reviewed-on: https://chromium-review.googlesource.com/958304Reviewed-by: default avatarYoshiki Iguchi <yoshiki@chromium.org>
Commit-Queue: Tetsui Ohkubo <tetsui@chromium.org>
Cr-Commit-Position: refs/heads/master@{#543031}
parent d8206c55
...@@ -1585,6 +1585,7 @@ test("ash_unittests") { ...@@ -1585,6 +1585,7 @@ test("ash_unittests") {
"system/tray_accessibility_unittest.cc", "system/tray_accessibility_unittest.cc",
"system/tray_caps_lock_unittest.cc", "system/tray_caps_lock_unittest.cc",
"system/tray_tracing_unittest.cc", "system/tray_tracing_unittest.cc",
"system/unified/top_shortcuts_view_unittest.cc",
"system/unified/unified_system_info_view_unittest.cc", "system/unified/unified_system_info_view_unittest.cc",
"system/update/tray_update_unittest.cc", "system/update/tray_update_unittest.cc",
"system/user/tray_user_unittest.cc", "system/user/tray_user_unittest.cc",
......
...@@ -27,6 +27,7 @@ views::View* CreateUserAvatarView() { ...@@ -27,6 +27,7 @@ views::View* CreateUserAvatarView() {
DCHECK(Shell::Get()); DCHECK(Shell::Get());
const mojom::UserSession* const user_session = const mojom::UserSession* const user_session =
Shell::Get()->session_controller()->GetUserSession(0); Shell::Get()->session_controller()->GetUserSession(0);
DCHECK(user_session);
auto* image_view = new tray::RoundedImageView(kTrayItemSize / 2); auto* image_view = new tray::RoundedImageView(kTrayItemSize / 2);
if (user_session->user_info->type == user_manager::USER_TYPE_GUEST) { if (user_session->user_info->type == user_manager::USER_TYPE_GUEST) {
...@@ -47,12 +48,16 @@ TopShortcutsView::TopShortcutsView(UnifiedSystemTrayController* controller) ...@@ -47,12 +48,16 @@ TopShortcutsView::TopShortcutsView(UnifiedSystemTrayController* controller)
: controller_(controller) { : controller_(controller) {
DCHECK(controller_); DCHECK(controller_);
auto layout = std::make_unique<views::BoxLayout>( auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::kHorizontal, kUnifiedTopShortcutPadding, views::BoxLayout::kHorizontal, kUnifiedTopShortcutPadding,
kUnifiedTopShortcutSpacing); kUnifiedTopShortcutSpacing));
layout->set_cross_axis_alignment(views::BoxLayout::CROSS_AXIS_ALIGNMENT_END); layout->set_cross_axis_alignment(views::BoxLayout::CROSS_AXIS_ALIGNMENT_END);
AddChildView(CreateUserAvatarView()); if (Shell::Get()->session_controller()->login_status() !=
LoginStatus::NOT_LOGGED_IN) {
user_avatar_view_ = CreateUserAvatarView();
AddChildView(user_avatar_view_);
}
// Show the buttons in this row as disabled if the user is at the login // Show the buttons in this row as disabled if the user is at the login
// screen, lock screen, or in a secondary account flow. The exception is // screen, lock screen, or in a secondary account flow. The exception is
...@@ -82,13 +87,11 @@ TopShortcutsView::TopShortcutsView(UnifiedSystemTrayController* controller) ...@@ -82,13 +87,11 @@ TopShortcutsView::TopShortcutsView(UnifiedSystemTrayController* controller)
// |collapse_button_| should be right-aligned, so we need spacing between // |collapse_button_| should be right-aligned, so we need spacing between
// other buttons and |collapse_button_|. // other buttons and |collapse_button_|.
views::View* spacing = new views::View; views::View* spacing = new views::View;
layout->SetFlexForView(spacing, 1);
AddChildView(spacing); AddChildView(spacing);
layout->SetFlexForView(spacing, 1);
collapse_button_ = new CollapseButton(this); collapse_button_ = new CollapseButton(this);
AddChildView(collapse_button_); AddChildView(collapse_button_);
SetLayoutManager(std::move(layout));
} }
TopShortcutsView::~TopShortcutsView() = default; TopShortcutsView::~TopShortcutsView() = default;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef ASH_SYSTEM_UNIFIED_TOP_SHORTCUTS_VIEW_H_ #ifndef ASH_SYSTEM_UNIFIED_TOP_SHORTCUTS_VIEW_H_
#define ASH_SYSTEM_UNIFIED_TOP_SHORTCUTS_VIEW_H_ #define ASH_SYSTEM_UNIFIED_TOP_SHORTCUTS_VIEW_H_
#include "ash/ash_export.h"
#include "ui/views/controls/button/button.h" #include "ui/views/controls/button/button.h"
#include "ui/views/view.h" #include "ui/views/view.h"
...@@ -13,10 +14,12 @@ namespace ash { ...@@ -13,10 +14,12 @@ namespace ash {
class CollapseButton; class CollapseButton;
class SignOutButton; class SignOutButton;
class TopShortcutButton; class TopShortcutButton;
class TopShortcutsViewTest;
class UnifiedSystemTrayController; class UnifiedSystemTrayController;
// Top shortcuts view shown on the top of UnifiedSystemTrayView. // Top shortcuts view shown on the top of UnifiedSystemTrayView.
class TopShortcutsView : public views::View, public views::ButtonListener { class ASH_EXPORT TopShortcutsView : public views::View,
public views::ButtonListener {
public: public:
explicit TopShortcutsView(UnifiedSystemTrayController* controller); explicit TopShortcutsView(UnifiedSystemTrayController* controller);
~TopShortcutsView() override; ~TopShortcutsView() override;
...@@ -25,9 +28,12 @@ class TopShortcutsView : public views::View, public views::ButtonListener { ...@@ -25,9 +28,12 @@ class TopShortcutsView : public views::View, public views::ButtonListener {
void ButtonPressed(views::Button* sender, const ui::Event& event) override; void ButtonPressed(views::Button* sender, const ui::Event& event) override;
private: private:
friend class TopShortcutsViewTest;
UnifiedSystemTrayController* controller_; UnifiedSystemTrayController* controller_;
// Owned by views hierarchy. // Owned by views hierarchy.
views::View* user_avatar_view_ = nullptr;
SignOutButton* sign_out_button_ = nullptr; SignOutButton* sign_out_button_ = nullptr;
TopShortcutButton* lock_button_ = nullptr; TopShortcutButton* lock_button_ = nullptr;
TopShortcutButton* settings_button_ = nullptr; TopShortcutButton* settings_button_ = nullptr;
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/unified/top_shortcuts_view.h"
#include "ash/system/unified/unified_system_tray_controller.h"
#include "ash/session/test_session_controller_client.h"
#include "ash/system/unified/collapse_button.h"
#include "ash/system/unified/sign_out_button.h"
#include "ash/system/unified/top_shortcut_button.h"
#include "ash/test/ash_test_base.h"
#include "base/command_line.h"
using views::Button;
namespace ash {
// Tests manually control their session state.
class TopShortcutsViewTest : public NoSessionAshTestBase {
public:
TopShortcutsViewTest() = default;
~TopShortcutsViewTest() override = default;
void SetUp() override {
NoSessionAshTestBase::SetUp();
controller_ =
std::make_unique<UnifiedSystemTrayController>(GetPrimarySystemTray());
}
void TearDown() override {
controller_.reset();
top_shortcuts_view_.reset();
NoSessionAshTestBase::TearDown();
}
protected:
void SetUpView() {
top_shortcuts_view_ = std::make_unique<TopShortcutsView>(controller_.get());
}
views::View* GetUserAvatar() {
return top_shortcuts_view_->user_avatar_view_;
}
views::Button* GetSignOutButton() {
return top_shortcuts_view_->sign_out_button_;
}
views::Button* GetLockButton() { return top_shortcuts_view_->lock_button_; }
views::Button* GetSettingsButton() {
return top_shortcuts_view_->settings_button_;
}
views::Button* GetPowerButton() { return top_shortcuts_view_->power_button_; }
views::Button* GetCollapseButton() {
return top_shortcuts_view_->collapse_button_;
}
private:
std::unique_ptr<UnifiedSystemTrayController> controller_;
std::unique_ptr<TopShortcutsView> top_shortcuts_view_;
DISALLOW_COPY_AND_ASSIGN(TopShortcutsViewTest);
};
// Settings buttons are disabled before login.
TEST_F(TopShortcutsViewTest, ButtonStatesNotLoggedIn) {
SetUpView();
EXPECT_EQ(nullptr, GetUserAvatar());
EXPECT_FALSE(GetSignOutButton()->visible());
EXPECT_EQ(Button::STATE_DISABLED, GetLockButton()->state());
EXPECT_EQ(Button::STATE_DISABLED, GetSettingsButton()->state());
EXPECT_EQ(Button::STATE_NORMAL, GetPowerButton()->state());
EXPECT_EQ(Button::STATE_NORMAL, GetCollapseButton()->state());
}
// All buttons are enabled after login.
TEST_F(TopShortcutsViewTest, ButtonStatesLoggedIn) {
CreateUserSessions(1);
SetUpView();
EXPECT_NE(nullptr, GetUserAvatar());
EXPECT_TRUE(GetSignOutButton()->visible());
EXPECT_EQ(Button::STATE_NORMAL, GetLockButton()->state());
EXPECT_EQ(Button::STATE_NORMAL, GetSettingsButton()->state());
EXPECT_EQ(Button::STATE_NORMAL, GetPowerButton()->state());
EXPECT_EQ(Button::STATE_NORMAL, GetCollapseButton()->state());
}
// Settings buttons are disabled at the lock screen.
TEST_F(TopShortcutsViewTest, ButtonStatesLockScreen) {
BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
SetUpView();
EXPECT_NE(nullptr, GetUserAvatar());
EXPECT_TRUE(GetSignOutButton()->visible());
EXPECT_EQ(Button::STATE_DISABLED, GetLockButton()->state());
EXPECT_EQ(Button::STATE_DISABLED, GetSettingsButton()->state());
EXPECT_EQ(Button::STATE_NORMAL, GetPowerButton()->state());
EXPECT_EQ(Button::STATE_NORMAL, GetCollapseButton()->state());
}
// Settings buttons are disabled when adding a second multiprofile user.
TEST_F(TopShortcutsViewTest, ButtonStatesAddingUser) {
CreateUserSessions(1);
SetUserAddingScreenRunning(true);
SetUpView();
EXPECT_NE(nullptr, GetUserAvatar());
EXPECT_TRUE(GetSignOutButton()->visible());
EXPECT_EQ(Button::STATE_DISABLED, GetLockButton()->state());
EXPECT_EQ(Button::STATE_DISABLED, GetSettingsButton()->state());
EXPECT_EQ(Button::STATE_NORMAL, GetPowerButton()->state());
EXPECT_EQ(Button::STATE_NORMAL, GetCollapseButton()->state());
}
// Settings buttons are disabled when adding a supervised user.
TEST_F(TopShortcutsViewTest, ButtonStatesSupervisedUserFlow) {
// Simulate the add supervised user flow, which is a regular user session but
// with web UI settings disabled.
const bool enable_settings = false;
GetSessionControllerClient()->AddUserSession(
"foo@example.com", user_manager::USER_TYPE_REGULAR, enable_settings);
SetUpView();
EXPECT_EQ(nullptr, GetUserAvatar());
EXPECT_FALSE(GetSignOutButton()->visible());
EXPECT_EQ(Button::STATE_DISABLED, GetLockButton()->state());
EXPECT_EQ(Button::STATE_DISABLED, GetSettingsButton()->state());
EXPECT_EQ(Button::STATE_NORMAL, GetPowerButton()->state());
EXPECT_EQ(Button::STATE_NORMAL, GetCollapseButton()->state());
}
} // namespace ash
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "ash/ash_export.h"
#include "base/macros.h" #include "base/macros.h"
namespace ash { namespace ash {
...@@ -18,7 +19,7 @@ class UnifiedBrightnessSliderController; ...@@ -18,7 +19,7 @@ class UnifiedBrightnessSliderController;
class UnifiedSystemTrayView; class UnifiedSystemTrayView;
// Controller class of UnifiedSystemTrayView. Handles events of the view. // Controller class of UnifiedSystemTrayView. Handles events of the view.
class UnifiedSystemTrayController { class ASH_EXPORT UnifiedSystemTrayController {
public: public:
// |system_tray| is used to show detailed views which are still not // |system_tray| is used to show detailed views which are still not
// implemented on UnifiedSystemTray. // implemented on UnifiedSystemTray.
......
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