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