-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CrOS Network] Add NetworkListNetworkHeaderView class
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
Showing
7 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
ash/system/network/fake_network_list_network_header_view_delegate.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
43 changes: 43 additions & 0 deletions
43
ash/system/network/fake_network_list_network_header_view_delegate.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
91
ash/system/network/network_list_network_header_view_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |