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/system/tray/tray_details_view.h"
6
7 #include "ash/root_window_controller.h"
8 #include "ash/shelf/shelf_widget.h"
9 #include "ash/shell.h"
10 #include "ash/system/status_area_widget.h"
11 #include "ash/system/tray/system_tray.h"
12 #include "ash/system/tray/system_tray_item.h"
13 #include "ash/system/tray/tray_details_view.h"
14 #include "ash/system/tray/view_click_listener.h"
15 #include "ash/test/ash_test_base.h"
16 #include "base/run_loop.h"
17 #include "grit/ash_strings.h"
18 #include "ui/aura/window.h"
19 #include "ui/views/view.h"
20 #include "ui/views/widget/widget.h"
21
22 namespace ash {
23 namespace test {
24
25 namespace {
26
GetSystemTray()27 SystemTray* GetSystemTray() {
28 return Shell::GetPrimaryRootWindowController()->shelf()->
29 status_area_widget()->system_tray();
30 }
31
32 class TestDetailsView : public internal::TrayDetailsView,
33 public internal::ViewClickListener {
34 public:
TestDetailsView(SystemTrayItem * owner)35 explicit TestDetailsView(SystemTrayItem* owner)
36 : internal::TrayDetailsView(owner) {}
37
~TestDetailsView()38 virtual ~TestDetailsView() {}
39
CreateFooterAndFocus()40 void CreateFooterAndFocus() {
41 // Uses bluetooth label for testing purpose. It can be changed to any
42 // string_id.
43 CreateSpecialRow(IDS_ASH_STATUS_TRAY_BLUETOOTH, this);
44 footer()->content()->RequestFocus();
45 }
46
47 // Overridden from internal::ViewClickListener:
OnViewClicked(views::View * sender)48 virtual void OnViewClicked(views::View* sender) OVERRIDE {}
49
50 private:
51 DISALLOW_COPY_AND_ASSIGN(TestDetailsView);
52 };
53
54 // Trivial item implementation that tracks its views for testing.
55 class TestItem : public SystemTrayItem {
56 public:
TestItem()57 TestItem() : SystemTrayItem(GetSystemTray()), tray_view_(NULL) {}
58
59 // Overridden from SystemTrayItem:
CreateTrayView(user::LoginStatus status)60 virtual views::View* CreateTrayView(user::LoginStatus status) OVERRIDE {
61 tray_view_ = new views::View;
62 return tray_view_;
63 }
CreateDefaultView(user::LoginStatus status)64 virtual views::View* CreateDefaultView(user::LoginStatus status) OVERRIDE {
65 default_view_ = new views::View;
66 default_view_->SetFocusable(true);
67 return default_view_;
68 }
CreateDetailedView(user::LoginStatus status)69 virtual views::View* CreateDetailedView(user::LoginStatus status) OVERRIDE {
70 detailed_view_ = new TestDetailsView(this);
71 return detailed_view_;
72 }
DestroyTrayView()73 virtual void DestroyTrayView() OVERRIDE {
74 tray_view_ = NULL;
75 }
DestroyDefaultView()76 virtual void DestroyDefaultView() OVERRIDE {
77 default_view_ = NULL;
78 }
DestroyDetailedView()79 virtual void DestroyDetailedView() OVERRIDE {
80 detailed_view_ = NULL;
81 }
82
tray_view() const83 views::View* tray_view() const { return tray_view_; }
default_view() const84 views::View* default_view() const { return default_view_; }
detailed_view() const85 TestDetailsView* detailed_view() const { return detailed_view_; }
86
87 private:
88 views::View* tray_view_;
89 views::View* default_view_;
90 TestDetailsView* detailed_view_;
91
92 DISALLOW_COPY_AND_ASSIGN(TestItem);
93 };
94
95 } // namespace
96
97 typedef AshTestBase TrayDetailsViewTest;
98
TEST_F(TrayDetailsViewTest,TransitionToDefaultViewTest)99 TEST_F(TrayDetailsViewTest, TransitionToDefaultViewTest) {
100 SystemTray* tray = GetSystemTray();
101 ASSERT_TRUE(tray->GetWidget());
102
103 TestItem* test_item_1 = new TestItem;
104 TestItem* test_item_2 = new TestItem;
105 tray->AddTrayItem(test_item_1);
106 tray->AddTrayItem(test_item_2);
107
108 // Ensure the tray views are created.
109 ASSERT_TRUE(test_item_1->tray_view() != NULL);
110 ASSERT_TRUE(test_item_2->tray_view() != NULL);
111
112 // Show the default view.
113 tray->ShowDefaultView(BUBBLE_CREATE_NEW);
114 RunAllPendingInMessageLoop();
115
116 // Show the detailed view of item 2.
117 tray->ShowDetailedView(test_item_2, 0, true, BUBBLE_USE_EXISTING);
118 EXPECT_TRUE(test_item_2->detailed_view());
119 RunAllPendingInMessageLoop();
120 EXPECT_FALSE(test_item_2->default_view());
121
122 // Transition back to default view, the default view of item 2 should have
123 // focus.
124 test_item_2->detailed_view()->CreateFooterAndFocus();
125 test_item_2->detailed_view()->TransitionToDefaultView();
126 RunAllPendingInMessageLoop();
127
128 EXPECT_TRUE(test_item_2->default_view());
129 EXPECT_FALSE(test_item_2->detailed_view());
130 EXPECT_TRUE(test_item_2->default_view()->HasFocus());
131
132 // Show the detailed view of item 2 again.
133 tray->ShowDetailedView(test_item_2, 0, true, BUBBLE_USE_EXISTING);
134 EXPECT_TRUE(test_item_2->detailed_view());
135 RunAllPendingInMessageLoop();
136 EXPECT_FALSE(test_item_2->default_view());
137
138 // Transition back to default view, the default view of item 2 should NOT have
139 // focus.
140 test_item_2->detailed_view()->TransitionToDefaultView();
141 RunAllPendingInMessageLoop();
142
143 EXPECT_TRUE(test_item_2->default_view());
144 EXPECT_FALSE(test_item_2->detailed_view());
145 EXPECT_FALSE(test_item_2->default_view()->HasFocus());
146 }
147
148 } // namespace test
149 } // namespace ash
150