blob: 93d4e9be256ce3a21ba576edf625791501a46408 [file] [log] [blame]
// Copyright (c) 2012 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/shelf/shelf_view_test_api.h"
#include "ash/public/cpp/shelf_model.h"
#include "ash/shelf/shelf_app_button.h"
#include "ash/shelf/shelf_menu_model_adapter.h"
#include "ash/shelf/shelf_navigation_widget.h"
#include "ash/shelf/shelf_view.h"
#include "ash/shelf/shelf_widget.h"
#include "base/run_loop.h"
#include "ui/views/animation/bounds_animator.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/controls/separator.h"
#include "ui/views/view_model.h"
namespace {
// A class used to wait for animations.
class TestAPIAnimationObserver : public views::BoundsAnimatorObserver {
public:
TestAPIAnimationObserver() = default;
~TestAPIAnimationObserver() override = default;
// views::BoundsAnimatorObserver overrides:
void OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) override {}
void OnBoundsAnimatorDone(views::BoundsAnimator* animator) override {
base::RunLoop::QuitCurrentWhenIdleDeprecated();
}
private:
DISALLOW_COPY_AND_ASSIGN(TestAPIAnimationObserver);
};
} // namespace
namespace ash {
ShelfViewTestAPI::ShelfViewTestAPI(ShelfView* shelf_view)
: shelf_view_(shelf_view) {}
ShelfViewTestAPI::~ShelfViewTestAPI() = default;
int ShelfViewTestAPI::GetButtonCount() {
return shelf_view_->view_model_->view_size();
}
ShelfAppButton* ShelfViewTestAPI::GetButton(int index) {
return static_cast<ShelfAppButton*>(GetViewAt(index));
}
ShelfID ShelfViewTestAPI::AddItem(ShelfItemType type) {
ShelfItem item;
item.type = type;
item.id = ShelfID(base::NumberToString(id_++));
shelf_view_->model_->Add(item);
return item.id;
}
views::View* ShelfViewTestAPI::GetViewAt(int index) {
return shelf_view_->view_model_->view_at(index);
}
const gfx::Rect& ShelfViewTestAPI::GetBoundsByIndex(int index) {
return shelf_view_->view_model_->view_at(index)->bounds();
}
const gfx::Rect& ShelfViewTestAPI::GetIdealBoundsByIndex(int index) {
return shelf_view_->view_model_->ideal_bounds(index);
}
base::TimeDelta ShelfViewTestAPI::GetAnimationDuration() const {
DCHECK(shelf_view_->bounds_animator_);
return shelf_view_->bounds_animator_->GetAnimationDuration();
}
void ShelfViewTestAPI::SetAnimationDuration(base::TimeDelta duration) {
shelf_view_->bounds_animator_->SetAnimationDuration(duration);
}
void ShelfViewTestAPI::RunMessageLoopUntilAnimationsDone(
views::BoundsAnimator* bounds_animator) {
if (!bounds_animator->IsAnimating())
return;
std::unique_ptr<TestAPIAnimationObserver> observer(
new TestAPIAnimationObserver());
bounds_animator->AddObserver(observer.get());
// This nested loop will quit when TestAPIAnimationObserver's
// OnBoundsAnimatorDone is called.
base::RunLoop().Run();
bounds_animator->RemoveObserver(observer.get());
}
void ShelfViewTestAPI::RunMessageLoopUntilAnimationsDone() {
if (!shelf_view_->bounds_animator_->IsAnimating())
return;
RunMessageLoopUntilAnimationsDone(shelf_view_->bounds_animator_.get());
}
gfx::Rect ShelfViewTestAPI::GetMenuAnchorRect(const views::View& source,
const gfx::Point& location,
bool context_menu) const {
return shelf_view_->GetMenuAnchorRect(source, location, context_menu);
}
bool ShelfViewTestAPI::CloseMenu() {
if (!shelf_view_->IsShowingMenu())
return false;
shelf_view_->shelf_menu_model_adapter_->Cancel();
return true;
}
const gfx::Rect& ShelfViewTestAPI::visible_shelf_item_bounds_union() const {
return shelf_view_->visible_shelf_item_bounds_union_;
}
ShelfTooltipManager* ShelfViewTestAPI::tooltip_manager() {
return shelf_view_->shelf()->tooltip();
}
int ShelfViewTestAPI::GetMinimumDragDistance() const {
return ShelfView::kMinimumDragDistance;
}
bool ShelfViewTestAPI::SameDragType(ShelfItemType typea,
ShelfItemType typeb) const {
return shelf_view_->SameDragType(typea, typeb);
}
gfx::Rect ShelfViewTestAPI::GetBoundsForDragInsertInScreen() {
return shelf_view_->GetBoundsForDragInsertInScreen();
}
bool ShelfViewTestAPI::IsRippedOffFromShelf() {
return shelf_view_->dragged_off_shelf_;
}
ShelfButtonPressedMetricTracker*
ShelfViewTestAPI::shelf_button_pressed_metric_tracker() {
return &(shelf_view_->shelf_button_pressed_metric_tracker_);
}
void ShelfViewTestAPI::SetShelfContextMenuCallback(
base::RepeatingClosure closure) {
DCHECK(shelf_view_->context_menu_shown_callback_.is_null());
shelf_view_->context_menu_shown_callback_ = std::move(closure);
}
int ShelfViewTestAPI::GetSeparatorIndex() const {
return shelf_view_->separator_index_;
}
bool ShelfViewTestAPI::IsSeparatorVisible() const {
return shelf_view_->separator_->GetVisible();
}
} // namespace ash