1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ash/test/shelf_view_test_api.h"
6
7#include "ash/shelf/overflow_button.h"
8#include "ash/shelf/shelf_button.h"
9#include "ash/shelf/shelf_constants.h"
10#include "ash/shelf/shelf_model.h"
11#include "ash/shelf/shelf_view.h"
12#include "base/message_loop/message_loop.h"
13#include "ui/views/animation/bounds_animator.h"
14#include "ui/views/view_model.h"
15
16namespace {
17
18// A class used to wait for animations.
19class TestAPIAnimationObserver : public views::BoundsAnimatorObserver {
20 public:
21  TestAPIAnimationObserver() {}
22  virtual ~TestAPIAnimationObserver() {}
23
24  // views::BoundsAnimatorObserver overrides:
25  virtual void OnBoundsAnimatorProgressed(
26      views::BoundsAnimator* animator) OVERRIDE {}
27  virtual void OnBoundsAnimatorDone(views::BoundsAnimator* animator) OVERRIDE {
28    base::MessageLoop::current()->Quit();
29  }
30
31 private:
32  DISALLOW_COPY_AND_ASSIGN(TestAPIAnimationObserver);
33};
34
35}  // namespace
36
37namespace ash {
38namespace test {
39
40ShelfViewTestAPI::ShelfViewTestAPI(ShelfView* shelf_view)
41    : shelf_view_(shelf_view) {}
42
43ShelfViewTestAPI::~ShelfViewTestAPI() {
44}
45
46int ShelfViewTestAPI::GetButtonCount() {
47  return shelf_view_->view_model_->view_size();
48}
49
50ShelfButton* ShelfViewTestAPI::GetButton(int index) {
51  // App list button is not a ShelfButton.
52  if (shelf_view_->model_->items()[index].type == ash::TYPE_APP_LIST)
53    return NULL;
54
55  return static_cast<ShelfButton*>(shelf_view_->view_model_->view_at(index));
56}
57
58int ShelfViewTestAPI::GetFirstVisibleIndex() {
59  return shelf_view_->first_visible_index_;
60}
61
62int ShelfViewTestAPI::GetLastVisibleIndex() {
63  return shelf_view_->last_visible_index_;
64}
65
66bool ShelfViewTestAPI::IsOverflowButtonVisible() {
67  return shelf_view_->overflow_button_->visible();
68}
69
70void ShelfViewTestAPI::ShowOverflowBubble() {
71  if (!shelf_view_->IsShowingOverflowBubble())
72    shelf_view_->ToggleOverflowBubble();
73}
74
75const gfx::Rect& ShelfViewTestAPI::GetBoundsByIndex(int index) {
76  return shelf_view_->view_model_->view_at(index)->bounds();
77}
78
79const gfx::Rect& ShelfViewTestAPI::GetIdealBoundsByIndex(int index) {
80  return shelf_view_->view_model_->ideal_bounds(index);
81}
82
83void ShelfViewTestAPI::SetAnimationDuration(int duration_ms) {
84  shelf_view_->bounds_animator_->SetAnimationDuration(duration_ms);
85}
86
87void ShelfViewTestAPI::RunMessageLoopUntilAnimationsDone() {
88  if (!shelf_view_->bounds_animator_->IsAnimating())
89    return;
90
91  scoped_ptr<TestAPIAnimationObserver> observer(new TestAPIAnimationObserver());
92  shelf_view_->bounds_animator_->AddObserver(observer.get());
93
94  // This nested loop will quit when TestAPIAnimationObserver's
95  // OnBoundsAnimatorDone is called.
96  base::MessageLoop::current()->Run();
97
98  shelf_view_->bounds_animator_->RemoveObserver(observer.get());
99}
100
101OverflowBubble* ShelfViewTestAPI::overflow_bubble() {
102  return shelf_view_->overflow_bubble_.get();
103}
104
105gfx::Size ShelfViewTestAPI::GetPreferredSize() {
106  return shelf_view_->GetPreferredSize();
107}
108
109int ShelfViewTestAPI::GetButtonSize() {
110  return kShelfButtonSize;
111}
112
113int ShelfViewTestAPI::GetButtonSpacing() {
114  return kShelfButtonSpacing;
115}
116
117void ShelfViewTestAPI::ButtonPressed(views::Button* sender,
118                                     const ui::Event& event) {
119  return shelf_view_->ButtonPressed(sender, event);
120}
121
122bool ShelfViewTestAPI::SameDragType(ShelfItemType typea,
123                                    ShelfItemType typeb) const {
124  return shelf_view_->SameDragType(typea, typeb);
125}
126
127void ShelfViewTestAPI::SetShelfDelegate(ShelfDelegate* delegate) {
128  shelf_view_->delegate_ = delegate;
129}
130
131gfx::Rect ShelfViewTestAPI::GetBoundsForDragInsertInScreen() {
132  return shelf_view_->GetBoundsForDragInsertInScreen();
133}
134
135bool ShelfViewTestAPI::IsRippedOffFromShelf() {
136  return shelf_view_->dragged_off_shelf_;
137}
138
139bool ShelfViewTestAPI::DraggedItemFromOverflowToShelf() {
140    return shelf_view_->dragged_off_from_overflow_to_shelf_;
141}
142
143}  // namespace test
144}  // namespace ash
145