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