Skip to content

Commit

Permalink
[CrOS Network] Add NetworkListNetworkHeaderView class
Browse files Browse the repository at this point in the history
This CL continues the effort to refactor network quick settings,
it adds new classes NetworkListNetworkHeaderView but does not yet add
its subclasses these will be added in subsequent CLs.

Note: Test for this class are added to its subclasses
NetworkListMobileHeaderView and NetworkListWifiHeaderView which will be
added in subsequent CLs

Bug: b/207089013
Test: Ran CQ and added new unittest
Change-Id: I5aa55132964aec192a1af4ad0dfd63f6199a3abb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3564752
Reviewed-by: Chad Duffin <chadduffin@chromium.org>
Commit-Queue: Theo Johnson-kanu <tjohnsonkanu@google.com>
Cr-Commit-Position: refs/heads/main@{#989549}
  • Loading branch information
Theo Johnson-Kanu authored and Chromium LUCI CQ committed Apr 6, 2022
1 parent 58c7fc6 commit 448c896
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ash/BUILD.gn
Expand Up @@ -1268,6 +1268,8 @@ component("ash") {
"system/network/fake_network_detailed_network_view.h",
"system/network/fake_network_detailed_view_delegate.cc",
"system/network/fake_network_detailed_view_delegate.h",
"system/network/fake_network_list_network_header_view_delegate.cc",
"system/network/fake_network_list_network_header_view_delegate.h",
"system/network/network_detailed_network_view.cc",
"system/network/network_detailed_network_view.h",
"system/network/network_detailed_network_view_impl.cc",
Expand Down Expand Up @@ -1295,6 +1297,8 @@ component("ash") {
"system/network/network_info_bubble.h",
"system/network/network_list_header_view.cc",
"system/network/network_list_header_view.h",
"system/network/network_list_network_header_view.cc",
"system/network/network_list_network_header_view.h",
"system/network/network_list_view.cc",
"system/network/network_list_view.h",
"system/network/network_list_view_controller.cc",
Expand Down Expand Up @@ -2675,6 +2679,7 @@ test("ash_unittests") {
"system/network/network_feature_pod_controller_unittest.cc",
"system/network/network_icon_unittest.cc",
"system/network/network_info_bubble_unittest.cc",
"system/network/network_list_network_header_view_unittest.cc",
"system/network/network_list_view_controller_unittest.cc",
"system/network/sms_observer_unittest.cc",
"system/network/vpn_list_unittest.cc",
Expand Down
@@ -0,0 +1,24 @@
// Copyright 2022 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/network/fake_network_list_network_header_view_delegate.h"

namespace ash {

FakeNetworkListNetworkHeaderViewDelegate::
FakeNetworkListNetworkHeaderViewDelegate() {}
FakeNetworkListNetworkHeaderViewDelegate::
~FakeNetworkListNetworkHeaderViewDelegate() = default;

void FakeNetworkListNetworkHeaderViewDelegate::OnMobileToggleClicked(
bool new_state) {
mobile_toggle_clicked_count_++;
}

void FakeNetworkListNetworkHeaderViewDelegate::OnWifiToggleClicked(
bool new_state) {
wifi_toggle_clicked_count_++;
}

} // namespace ash
@@ -0,0 +1,43 @@
// Copyright 2022 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.

#ifndef ASH_SYSTEM_NETWORK_FAKE_NETWORK_LIST_NETWORK_HEADER_VIEW_DELEGATE_H_
#define ASH_SYSTEM_NETWORK_FAKE_NETWORK_LIST_NETWORK_HEADER_VIEW_DELEGATE_H_

#include "ash/ash_export.h"
#include "ash/system/network/network_list_network_header_view.h"

namespace ash {

// Fake of NetworkListNetworkHeaderView::Delegate implementation.
class ASH_EXPORT FakeNetworkListNetworkHeaderViewDelegate
: public NetworkListNetworkHeaderView::Delegate {
public:
FakeNetworkListNetworkHeaderViewDelegate();
FakeNetworkListNetworkHeaderViewDelegate(
const FakeNetworkListNetworkHeaderViewDelegate&) = delete;
FakeNetworkListNetworkHeaderViewDelegate& operator=(
const FakeNetworkListNetworkHeaderViewDelegate&) = delete;
~FakeNetworkListNetworkHeaderViewDelegate() override;

// NetworkListNetworkHeaderView::Delegate
void OnMobileToggleClicked(bool new_state) override;
void OnWifiToggleClicked(bool new_state) override;

size_t mobile_toggle_clicked_count() const {
return mobile_toggle_clicked_count_;
}

size_t wifi_toggle_clicked_count() const {
return wifi_toggle_clicked_count_;
}

private:
size_t mobile_toggle_clicked_count_ = 0;
size_t wifi_toggle_clicked_count_ = 0;
};

} // namespace ash

#endif // ASH_SYSTEM_NETWORK_FAKE_NETWORK_LIST_NETWORK_HEADER_VIEW_DELEGATE_H_
2 changes: 2 additions & 0 deletions ash/system/network/network_list_header_view.h
Expand Up @@ -25,6 +25,8 @@ class ASH_EXPORT NetworkListHeaderView : public views::View {
TriView* container() const { return container_; }

private:
friend class NetworkListNetworkHeaderViewTest;

// Used for testing. This is 1 because view IDs should not be 0.
static constexpr int kTitleLabelViewId = 1;

Expand Down
58 changes: 58 additions & 0 deletions ash/system/network/network_list_network_header_view.cc
@@ -0,0 +1,58 @@
// Copyright 2022 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/network/network_list_network_header_view.h"

#include "ash/ash_export.h"
#include "ash/constants/ash_features.h"
#include "ash/shell.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/network/network_list_header_view.h"
#include "ash/system/network/tray_network_state_model.h"
#include "ash/system/tray/tray_popup_utils.h"
#include "ash/system/tray/tray_toggle_button.h"
#include "ash/system/tray/tri_view.h"
#include "ui/views/view.h"

namespace ash {

NetworkListNetworkHeaderView::NetworkListNetworkHeaderView(Delegate* delegate,
int label_id)
: NetworkListHeaderView(label_id),
model_(Shell::Get()->system_tray_model()->network_state_model()),
delegate_(delegate) {
toggle_ = new TrayToggleButton(
base::BindRepeating(&NetworkListNetworkHeaderView::ToggleButtonPressed,
base::Unretained(this)),
label_id);
toggle_->SetID(kToggleButtonId);
container()->AddView(TriView::Container::END, toggle_);
AddExtraButtons();
}

void NetworkListNetworkHeaderView::SetToggleState(bool enabled, bool is_on) {
toggle_->SetEnabled(enabled);
toggle_->SetAcceptsEvents(enabled);
toggle_->AnimateIsOn(is_on);
}

void NetworkListNetworkHeaderView::AddExtraButtons() {}

void NetworkListNetworkHeaderView::OnToggleToggled(bool is_on) {}

void NetworkListNetworkHeaderView::SetToggleVisibility(bool visible) {
toggle_->SetVisible(visible);
}

void NetworkListNetworkHeaderView::ToggleButtonPressed() {
// In the event of frequent clicks, helps to prevent a toggle button state
// from becoming inconsistent with the async operation of enabling /
// disabling of mobile radio. The toggle will get unlocked in the next
// call to SetToggleState(). Note that we don't disable/enable
// because that would clear focus.
toggle_->SetAcceptsEvents(false);
OnToggleToggled(toggle_->GetIsOn());
}

} // namespace ash
73 changes: 73 additions & 0 deletions ash/system/network/network_list_network_header_view.h
@@ -0,0 +1,73 @@
// Copyright 2022 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.

#ifndef ASH_SYSTEM_NETWORK_NETWORK_LIST_NETWORK_HEADER_VIEW_H_
#define ASH_SYSTEM_NETWORK_NETWORK_LIST_NETWORK_HEADER_VIEW_H_

#include "ash/ash_export.h"
#include "ash/system/network/network_list_header_view.h"
#include "ash/system/network/network_row_title_view.h"
#include "ash/system/tray/tri_view.h"
#include "ui/views/controls/button/toggle_button.h"
#include "ui/views/view.h"

namespace ash {

class TrayNetworkStateModel;

// This class is used for the headers of networks (Mobile and Wifi), and
// implements the functionality shared between the headers of Mobile and Wifi
// network headers.
class ASH_EXPORT NetworkListNetworkHeaderView : public NetworkListHeaderView {
public:
class Delegate {
public:
Delegate() = default;
virtual ~Delegate() = default;

virtual void OnMobileToggleClicked(bool new_state) = 0;
virtual void OnWifiToggleClicked(bool new_state) = 0;
};

NetworkListNetworkHeaderView(Delegate* delegate, int label_id);
NetworkListNetworkHeaderView(const NetworkListNetworkHeaderView&) = delete;
NetworkListNetworkHeaderView& operator=(const NetworkListNetworkHeaderView&) =
delete;
~NetworkListNetworkHeaderView() override = default;

virtual void SetToggleState(bool enabled, bool is_on);

protected:
virtual void AddExtraButtons();

// Called when |toggle_| is clicked and toggled. Subclasses should override to
// enabled/disable their respective technology.
virtual void OnToggleToggled(bool is_on);

void SetToggleVisibility(bool visible);

Delegate* delegate() const { return delegate_; };

TrayNetworkStateModel* model() { return model_; }

private:
friend class NetworkListNetworkHeaderViewTest;

// Used for testing. This is 2 because view IDs should not be 0. Id is set to
// 2 here because NetworkListHeaderView is using ID 1 for title label.
static constexpr int kToggleButtonId = 2;

void ToggleButtonPressed();

TrayNetworkStateModel* model_;

// ToggleButton to toggle section on or off.
views::ToggleButton* toggle_ = nullptr;

Delegate* delegate_ = nullptr;
};

} // namespace ash

#endif // ASH_SYSTEM_NETWORK_NETWORK_LIST_NETWORK_HEADER_VIEW_H_
91 changes: 91 additions & 0 deletions ash/system/network/network_list_network_header_view_unittest.cc
@@ -0,0 +1,91 @@
// Copyright 2022 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/network/network_list_network_header_view.h"

#include <memory>

#include "ash/constants/ash_features.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/network/fake_network_list_network_header_view_delegate.h"
#include "ash/system/network/network_list_header_view.h"
#include "ash/system/tray/tri_view.h"
#include "ash/test/ash_test_base.h"
#include "base/test/scoped_feature_list.h"
#include "ui/views/controls/button/toggle_button.h"
#include "ui/views/view.h"

namespace ash {

class NetworkListNetworkHeaderViewTest : public AshTestBase {
public:
void SetUp() override {
AshTestBase::SetUp();

feature_list_.InitAndEnableFeature(features::kQuickSettingsNetworkRevamp);

network_list_network_header_view_ =
std::make_unique<NetworkListNetworkHeaderView>(
&fake_network_list_network_header_delegate_,
IDS_ASH_STATUS_TRAY_NETWORK_MOBILE);
}

void TearDown() override {
network_list_network_header_view_.reset();

AshTestBase::TearDown();
}

FakeNetworkListNetworkHeaderViewDelegate*
fake_network_list_network_header_delegate() {
return &fake_network_list_network_header_delegate_;
}

NetworkListNetworkHeaderView* network_list_network_header_view() {
return network_list_network_header_view_.get();
}

views::ToggleButton* GetToggleButton() {
return FindViewById<views::ToggleButton*>(
NetworkListNetworkHeaderView::kToggleButtonId);
}

void SetToggleVisibility(bool visible) {
network_list_network_header_view()->SetToggleVisibility(visible);
}

template <class T>
T FindViewById(int id) {
return static_cast<T>(
network_list_network_header_view_->container()->GetViewByID(id));
}

private:
base::test::ScopedFeatureList feature_list_;
FakeNetworkListNetworkHeaderViewDelegate
fake_network_list_network_header_delegate_;
std::unique_ptr<NetworkListNetworkHeaderView>
network_list_network_header_view_;
};

TEST_F(NetworkListNetworkHeaderViewTest, ToggleStates) {
views::ToggleButton* toggle_button = GetToggleButton();
EXPECT_NE(nullptr, toggle_button);
EXPECT_EQ(views::Button::ButtonState::STATE_NORMAL,
toggle_button->GetState());
EXPECT_TRUE(toggle_button->GetVisible());

EXPECT_TRUE(toggle_button->GetAcceptsEvents());
EXPECT_FALSE(toggle_button->GetIsOn());

network_list_network_header_view()->SetToggleState(/*enabled=*/false,
/*is_on=*/true);
EXPECT_FALSE(toggle_button->GetAcceptsEvents());
EXPECT_TRUE(toggle_button->GetIsOn());

SetToggleVisibility(/*visible=*/false);
EXPECT_FALSE(toggle_button->GetVisible());
}

} // namespace ash

0 comments on commit 448c896

Please sign in to comment.