1 // Copyright 2013 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/shelf/shelf.h" 6 #include "ash/shelf/shelf_button.h" 7 #include "ash/shelf/shelf_item_delegate_manager.h" 8 #include "ash/shelf/shelf_model.h" 9 #include "ash/shelf/shelf_view.h" 10 #include "ash/shelf/shelf_widget.h" 11 #include "ash/shell.h" 12 #include "ash/test/ash_test_base.h" 13 #include "ash/test/shelf_test_api.h" 14 #include "ash/test/shelf_view_test_api.h" 15 #include "ash/test/test_shelf_item_delegate.h" 16 #include "ash/wm/window_util.h" 17 #include "ui/aura/window_event_dispatcher.h" 18 #include "ui/gfx/display.h" 19 #include "ui/gfx/screen.h" 20 #include "ui/views/view.h" 21 #include "ui/views/widget/widget.h" 22 23 #if defined(OS_WIN) 24 #include "base/win/windows_version.h" 25 #endif 26 27 using ash::ShelfView; 28 using ash::ShelfButton; 29 30 namespace ash { 31 32 class ShelfTest : public ash::test::AshTestBase { 33 public: 34 ShelfTest() 35 : shelf_(NULL), 36 shelf_view_(NULL), 37 shelf_model_(NULL), 38 item_delegate_manager_(NULL) {} 39 40 virtual ~ShelfTest() {} 41 42 virtual void SetUp() { 43 test::AshTestBase::SetUp(); 44 45 shelf_ = Shelf::ForPrimaryDisplay(); 46 ASSERT_TRUE(shelf_); 47 48 test::ShelfTestAPI test(shelf_); 49 shelf_view_ = test.shelf_view(); 50 shelf_model_ = shelf_view_->model(); 51 item_delegate_manager_ = 52 Shell::GetInstance()->shelf_item_delegate_manager(); 53 54 test_.reset(new ash::test::ShelfViewTestAPI(shelf_view_)); 55 } 56 57 virtual void TearDown() OVERRIDE { 58 test::AshTestBase::TearDown(); 59 } 60 61 Shelf* shelf() { 62 return shelf_; 63 } 64 65 ShelfView* shelf_view() { 66 return shelf_view_; 67 } 68 69 ShelfModel* shelf_model() { 70 return shelf_model_; 71 } 72 73 ShelfItemDelegateManager* item_manager() { 74 return item_delegate_manager_; 75 } 76 77 ash::test::ShelfViewTestAPI* test_api() { 78 return test_.get(); 79 } 80 81 private: 82 Shelf* shelf_; 83 ShelfView* shelf_view_; 84 ShelfModel* shelf_model_; 85 ShelfItemDelegateManager* item_delegate_manager_; 86 scoped_ptr<test::ShelfViewTestAPI> test_; 87 88 DISALLOW_COPY_AND_ASSIGN(ShelfTest); 89 }; 90 91 // Confirms that ShelfItem reflects the appropriated state. 92 TEST_F(ShelfTest, StatusReflection) { 93 // Initially we have the app list. 94 int button_count = test_api()->GetButtonCount(); 95 96 // Add running platform app. 97 ShelfItem item; 98 item.type = TYPE_PLATFORM_APP; 99 item.status = STATUS_RUNNING; 100 int index = shelf_model()->Add(item); 101 ASSERT_EQ(++button_count, test_api()->GetButtonCount()); 102 ShelfButton* button = test_api()->GetButton(index); 103 EXPECT_EQ(ShelfButton::STATE_RUNNING, button->state()); 104 105 // Remove it. 106 shelf_model()->RemoveItemAt(index); 107 ASSERT_EQ(--button_count, test_api()->GetButtonCount()); 108 } 109 110 // Confirm that using the menu will clear the hover attribute. To avoid another 111 // browser test we check this here. 112 TEST_F(ShelfTest, checkHoverAfterMenu) { 113 // Initially we have the app list. 114 int button_count = test_api()->GetButtonCount(); 115 116 // Add running platform app. 117 ShelfItem item; 118 item.type = TYPE_PLATFORM_APP; 119 item.status = STATUS_RUNNING; 120 int index = shelf_model()->Add(item); 121 122 scoped_ptr<ShelfItemDelegate> delegate( 123 new test::TestShelfItemDelegate(NULL)); 124 item_manager()->SetShelfItemDelegate(shelf_model()->items()[index].id, 125 delegate.Pass()); 126 127 ASSERT_EQ(++button_count, test_api()->GetButtonCount()); 128 ShelfButton* button = test_api()->GetButton(index); 129 button->AddState(ShelfButton::STATE_HOVERED); 130 button->ShowContextMenu(gfx::Point(), ui::MENU_SOURCE_MOUSE); 131 EXPECT_FALSE(button->state() & ShelfButton::STATE_HOVERED); 132 133 // Remove it. 134 shelf_model()->RemoveItemAt(index); 135 } 136 137 TEST_F(ShelfTest, ShowOverflowBubble) { 138 ShelfID first_item_id = shelf_model()->next_id(); 139 140 // Add platform app button until overflow. 141 int items_added = 0; 142 while (!test_api()->IsOverflowButtonVisible()) { 143 ShelfItem item; 144 item.type = TYPE_PLATFORM_APP; 145 item.status = STATUS_RUNNING; 146 shelf_model()->Add(item); 147 148 ++items_added; 149 ASSERT_LT(items_added, 10000); 150 } 151 152 // Shows overflow bubble. 153 test_api()->ShowOverflowBubble(); 154 EXPECT_TRUE(shelf()->IsShowingOverflowBubble()); 155 156 // Removes the first item in main shelf view. 157 shelf_model()->RemoveItemAt(shelf_model()->ItemIndexByID(first_item_id)); 158 159 // Waits for all transitions to finish and there should be no crash. 160 test_api()->RunMessageLoopUntilAnimationsDone(); 161 EXPECT_FALSE(shelf()->IsShowingOverflowBubble()); 162 } 163 164 } // namespace ash 165