Home | History | Annotate | Download | only in test
      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_model.h"
     10 #include "ash/shelf/shelf_view.h"
     11 #include "base/message_loop/message_loop.h"
     12 #include "ui/views/animation/bounds_animator.h"
     13 #include "ui/views/view_model.h"
     14 
     15 namespace {
     16 
     17 // A class used to wait for animations.
     18 class TestAPIAnimationObserver : public views::BoundsAnimatorObserver {
     19  public:
     20   TestAPIAnimationObserver() {}
     21   virtual ~TestAPIAnimationObserver() {}
     22 
     23   // views::BoundsAnimatorObserver overrides:
     24   virtual void OnBoundsAnimatorProgressed(
     25       views::BoundsAnimator* animator) OVERRIDE {}
     26   virtual void OnBoundsAnimatorDone(views::BoundsAnimator* animator) OVERRIDE {
     27     base::MessageLoop::current()->Quit();
     28   }
     29 
     30  private:
     31   DISALLOW_COPY_AND_ASSIGN(TestAPIAnimationObserver);
     32 };
     33 
     34 }  // namespace
     35 
     36 namespace ash {
     37 namespace test {
     38 
     39 ShelfViewTestAPI::ShelfViewTestAPI(internal::ShelfView* shelf_view)
     40     : shelf_view_(shelf_view) {
     41 }
     42 
     43 ShelfViewTestAPI::~ShelfViewTestAPI() {
     44 }
     45 
     46 int ShelfViewTestAPI::GetButtonCount() {
     47   return shelf_view_->view_model_->view_size();
     48 }
     49 
     50 internal::ShelfButton* 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<internal::ShelfButton*>(
     56       shelf_view_->view_model_->view_at(index));
     57 }
     58 
     59 int ShelfViewTestAPI::GetFirstVisibleIndex() {
     60   return shelf_view_->first_visible_index_;
     61 }
     62 
     63 int ShelfViewTestAPI::GetLastVisibleIndex() {
     64   return shelf_view_->last_visible_index_;
     65 }
     66 
     67 bool ShelfViewTestAPI::IsOverflowButtonVisible() {
     68   return shelf_view_->overflow_button_->visible();
     69 }
     70 
     71 void ShelfViewTestAPI::ShowOverflowBubble() {
     72   if (!shelf_view_->IsShowingOverflowBubble())
     73     shelf_view_->ToggleOverflowBubble();
     74 }
     75 
     76 const gfx::Rect& ShelfViewTestAPI::GetBoundsByIndex(int index) {
     77   return shelf_view_->view_model_->view_at(index)->bounds();
     78 }
     79 
     80 const gfx::Rect& ShelfViewTestAPI::GetIdealBoundsByIndex(int index) {
     81   return shelf_view_->view_model_->ideal_bounds(index);
     82 }
     83 
     84 void ShelfViewTestAPI::SetAnimationDuration(int duration_ms) {
     85   shelf_view_->bounds_animator_->SetAnimationDuration(duration_ms);
     86 }
     87 
     88 void ShelfViewTestAPI::RunMessageLoopUntilAnimationsDone() {
     89   if (!shelf_view_->bounds_animator_->IsAnimating())
     90     return;
     91 
     92   scoped_ptr<TestAPIAnimationObserver> observer(new TestAPIAnimationObserver());
     93   shelf_view_->bounds_animator_->AddObserver(observer.get());
     94 
     95   // This nested loop will quit when TestAPIAnimationObserver's
     96   // OnBoundsAnimatorDone is called.
     97   base::MessageLoop::current()->Run();
     98 
     99   shelf_view_->bounds_animator_->RemoveObserver(observer.get());
    100 }
    101 
    102 internal::OverflowBubble* ShelfViewTestAPI::overflow_bubble() {
    103   return shelf_view_->overflow_bubble_.get();
    104 }
    105 
    106 gfx::Size ShelfViewTestAPI::GetPreferredSize() {
    107   return shelf_view_->GetPreferredSize();
    108 }
    109 
    110 int ShelfViewTestAPI::GetButtonSize() {
    111   return shelf_view_->GetButtonSize();
    112 }
    113 
    114 int ShelfViewTestAPI::GetButtonSpacing() {
    115   return shelf_view_->GetButtonSpacing();
    116 }
    117 
    118 bool ShelfViewTestAPI::SameDragType(LauncherItemType typea,
    119                                     LauncherItemType typeb) const {
    120   return shelf_view_->SameDragType(typea, typeb);
    121 }
    122 
    123 void ShelfViewTestAPI::SetShelfDelegate(ShelfDelegate* delegate) {
    124   shelf_view_->delegate_ = delegate;
    125 }
    126 
    127 gfx::Rect ShelfViewTestAPI::GetBoundsForDragInsertInScreen() {
    128   return shelf_view_->GetBoundsForDragInsertInScreen();
    129 }
    130 
    131 bool ShelfViewTestAPI::IsRippedOffFromShelf() {
    132   return shelf_view_->dragged_off_shelf_;
    133 }
    134 
    135 bool ShelfViewTestAPI::DraggedItemFromOverflowToShelf() {
    136     return shelf_view_->dragged_off_from_overflow_to_shelf_;
    137 }
    138 
    139 }  // namespace test
    140 }  // namespace ash
    141