Skip to content

Commit

Permalink
Add support code to test the cast system tray item.
Browse files Browse the repository at this point in the history
This supporting code is used in https://codereview.chromium.org/1231593002/.

BUG=497343

Review URL: https://codereview.chromium.org/1218653006

Cr-Commit-Position: refs/heads/master@{#340790}
  • Loading branch information
jdufault authored and Commit bot committed Jul 28, 2015
1 parent b441cdd commit b90450d
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 6 deletions.
2 changes: 2 additions & 0 deletions ash/ash.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,8 @@
'test/test_user_wallpaper_delegate.h',
'test/test_volume_control_delegate.cc',
'test/test_volume_control_delegate.h',
'test/tray_cast_test_api.cc',
'test/tray_cast_test_api.h',
'test/ui_controls_factory_ash.cc',
'test/ui_controls_factory_ash.h',
'test/user_metrics_recorder_test_api.cc',
Expand Down
44 changes: 41 additions & 3 deletions ash/system/cast/tray_cast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class CastCastView : public views::View, public views::ButtonListener {
explicit CastCastView(CastConfigDelegate* cast_config_delegate);
~CastCastView() override;

void StopCasting();

// Updates the label for the stop view to include information about the
// current device that is being casted.
void UpdateLabel();
Expand Down Expand Up @@ -206,6 +208,12 @@ void CastCastView::Layout() {
}
}

void CastCastView::StopCasting() {
cast_config_delegate_->StopCasting();
Shell::GetInstance()->metrics()->RecordUserMetricsAction(
ash::UMA_STATUS_AREA_CAST_STOP_CAST);
}

void CastCastView::UpdateLabel() {
if (cast_config_delegate_ == nullptr ||
cast_config_delegate_->HasCastExtension() == false)
Expand Down Expand Up @@ -245,9 +253,7 @@ void CastCastView::UpdateLabelCallback(
void CastCastView::ButtonPressed(views::Button* sender,
const ui::Event& event) {
DCHECK(sender == stop_button_);
cast_config_delegate_->StopCasting();
Shell::GetInstance()->metrics()->RecordUserMetricsAction(
ash::UMA_STATUS_AREA_CAST_STOP_CAST);
StopCasting();
}

// This view by itself does very little. It acts as a front-end for managing
Expand Down Expand Up @@ -388,6 +394,10 @@ class CastDetailedView : public TrayDetailsView, public ViewClickListener {
user::LoginStatus login);
~CastDetailedView() override;

// Makes the detail view think the view associated with the given receiver_id
// was clicked. This will start a cast.
void SimulateViewClickedForTest(const std::string& receiver_id);

private:
void CreateItems();

Expand Down Expand Up @@ -430,6 +440,16 @@ CastDetailedView::CastDetailedView(SystemTrayItem* owner,
CastDetailedView::~CastDetailedView() {
}

void CastDetailedView::SimulateViewClickedForTest(
const std::string& receiver_id) {
for (auto& it : receiver_activity_map_) {
if (it.second == receiver_id) {
OnViewClicked(it.first);
break;
}
}
}

void CastDetailedView::CreateItems() {
CreateScrollableList();
AppendSettingsEntries();
Expand Down Expand Up @@ -548,6 +568,19 @@ TrayCast::~TrayCast() {
Shell::GetInstance()->RemoveShellObserver(this);
}

void TrayCast::StartCastForTest(const std::string& receiver_id) {
if (detailed_ != nullptr)
detailed_->SimulateViewClickedForTest(receiver_id);
}

void TrayCast::StopCastForTest() {
default_->cast_view()->StopCasting();
}

const views::View* TrayCast::GetDefaultView() const {
return default_;
}

views::View* TrayCast::CreateTrayView(user::LoginStatus status) {
CHECK(tray_ == nullptr);
tray_ = new tray::CastTrayView(this);
Expand All @@ -557,8 +590,13 @@ views::View* TrayCast::CreateTrayView(user::LoginStatus status) {

views::View* TrayCast::CreateDefaultView(user::LoginStatus status) {
CHECK(default_ == nullptr);

default_ = new tray::CastDuplexView(this, cast_config_delegate_,
status != user::LOGGED_IN_LOCKED);
default_->set_id(TRAY_VIEW);
default_->select_view()->set_id(SELECT_VIEW);
default_->cast_view()->set_id(CAST_VIEW);

UpdatePrimaryView();
return default_;
}
Expand Down
9 changes: 8 additions & 1 deletion ash/system/cast/tray_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ class CastDetailedView;
class CastDuplexView;
} // namespace tray

class TrayCast : public SystemTrayItem, public ShellObserver {
class ASH_EXPORT TrayCast : public SystemTrayItem, public ShellObserver {
public:
explicit TrayCast(SystemTray* system_tray);
~TrayCast() override;

private:
// Helper/utility methods for testing.
friend class TrayCastTestAPI;
void StartCastForTest(const std::string& receiver_id);
void StopCastForTest();
const views::View* GetDefaultView() const;
enum ChildViewId { TRAY_VIEW = 1, SELECT_VIEW, CAST_VIEW };

// Overridden from SystemTrayItem.
views::View* CreateTrayView(user::LoginStatus status) override;
views::View* CreateDefaultView(user::LoginStatus status) override;
Expand Down
6 changes: 4 additions & 2 deletions ash/system/tray/system_tray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ void SystemTray::CreateItems(SystemTrayDelegate* delegate) {
AddTrayItem(new TrayVPN(this));
AddTrayItem(new TraySms(this));
AddTrayItem(new TrayBluetooth(this));
AddTrayItem(new TrayCast(this));
tray_cast_ = new TrayCast(this);
AddTrayItem(tray_cast_);
AddTrayItem(new TrayDisplay(this));
screen_capture_tray_item_ = new ScreenCaptureTrayItem(this);
AddTrayItem(screen_capture_tray_item_);
Expand All @@ -205,7 +206,6 @@ void SystemTray::CreateItems(SystemTrayDelegate* delegate) {
#elif defined(OS_LINUX)
AddTrayItem(tray_accessibility_);
AddTrayItem(new TrayBluetooth(this));
AddTrayItem(new TrayCast(this));
AddTrayItem(new TrayUpdate(this));
AddTrayItem(tray_date_);
#endif
Expand Down Expand Up @@ -694,6 +694,8 @@ views::View* SystemTray::GetTrayItemViewForTest(SystemTrayItem* item) {
return it == tray_item_map_.end() ? NULL : it->second;
}

TrayCast* SystemTray::GetTrayCastForTesting() const { return tray_cast_; }

TrayDate* SystemTray::GetTrayDateForTesting() const { return tray_date_; }

bool SystemTray::PerformAction(const ui::Event& event) {
Expand Down
5 changes: 5 additions & 0 deletions ash/system/tray/system_tray.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define ASH_SYSTEM_TRAY_SYSTEM_TRAY_H_

#include "ash/ash_export.h"
#include "ash/system/cast/tray_cast.h"
#include "ash/system/tray/system_tray_bubble.h"
#include "ash/system/tray/tray_background_view.h"
#include "ash/system/user/login_status.h"
Expand Down Expand Up @@ -150,6 +151,9 @@ class ASH_EXPORT SystemTray : public TrayBackgroundView,
// Get the tray item view (or NULL) for a given |tray_item| in a unit test.
views::View* GetTrayItemViewForTest(SystemTrayItem* tray_item);

// Gets tray_cast_ for browser tests.
TrayCast* GetTrayCastForTesting() const;

// Gets tray_date_ for browser tests.
TrayDate* GetTrayDateForTesting() const;

Expand Down Expand Up @@ -232,6 +236,7 @@ class ASH_EXPORT SystemTray : public TrayBackgroundView,
bool full_system_tray_menu_;

TrayAccessibility* tray_accessibility_; // not owned
TrayCast* tray_cast_;
TrayDate* tray_date_;

// A reference to the Screen share and capture item.
Expand Down
49 changes: 49 additions & 0 deletions ash/test/tray_cast_test_api.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2015 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/test/tray_cast_test_api.h"

#include "ash/system/tray/system_tray.h"
#include "ui/views/view.h"

namespace ash {

TrayCastTestAPI::TrayCastTestAPI(TrayCast* tray_cast) : tray_cast_(tray_cast) {}

TrayCastTestAPI::~TrayCastTestAPI() {}

bool TrayCastTestAPI::IsTrayInitialized() const {
return tray_cast_->default_ != nullptr;
}

bool TrayCastTestAPI::IsTrayVisible() const {
return IsViewDrawn(TrayCast::TRAY_VIEW);
}

bool TrayCastTestAPI::IsTrayCastViewVisible() const {
return IsViewDrawn(TrayCast::CAST_VIEW);
}

bool TrayCastTestAPI::IsTraySelectViewVisible() const {
return IsViewDrawn(TrayCast::SELECT_VIEW);
}

void TrayCastTestAPI::StartCast(const std::string& receiver_id) {
return tray_cast_->StartCastForTest(receiver_id);
}

void TrayCastTestAPI::StopCast() {
return tray_cast_->StopCastForTest();
}

void TrayCastTestAPI::OnCastingSessionStartedOrStopped(bool is_casting) {
tray_cast_->OnCastingSessionStartedOrStopped(is_casting);
}

bool TrayCastTestAPI::IsViewDrawn(TrayCast::ChildViewId id) const {
const views::View* view = tray_cast_->GetDefaultView()->GetViewByID(id);
return view != nullptr && view->IsDrawn();
}

} // namespace ash
50 changes: 50 additions & 0 deletions ash/test/tray_cast_test_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2015 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_TEST_TRAY_CAST_TEST_API_H_
#define ASH_TEST_TRAY_CAST_TEST_API_H_

#include <string>

#include "ash/ash_export.h"
#include "ash/system/cast/tray_cast.h"

namespace ash {

class TrayCastTestAPI {
public:
explicit TrayCastTestAPI(TrayCast* tray_cast);
~TrayCastTestAPI();

bool IsTrayInitialized() const;
bool IsTrayVisible() const;

// IsTrayCastViewVisible returns true if the active casting view is
// visible, ie, the TrayCast believes we are casting.
// IsTraySelectViewVisible returns true when the view for selecting a
// receiver is active, ie, the TrayCast believes we are not casting.
bool IsTrayCastViewVisible() const;
bool IsTraySelectViewVisible() const;

// Start a new cast to the given receiver.
void StartCast(const std::string& receiver_id);
void StopCast();

// Exposed callback to update the casting state. The test code needs to call
// this function manually, as there is no actual casting going on. In a real
// environment, this method is invoked by the casting system in Chrome.
void OnCastingSessionStartedOrStopped(bool is_casting);

private:
bool IsViewDrawn(TrayCast::ChildViewId id) const;

// Not owned.
TrayCast* tray_cast_;

DISALLOW_COPY_AND_ASSIGN(TrayCastTestAPI);
};

} // namespace ash

#endif // ASH_TEST_TRAY_CAST_TEST_API_H_

0 comments on commit b90450d

Please sign in to comment.