Ahmed Mehfooz | e32a4b5 | 2020-04-14 20:34:10 | [diff] [blame] | 1 | // Copyright 2018 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/public/cpp/system_tray_test_api.h" |
| 6 | |
Jan Wilken Dörrie | ad587c3 | 2021-03-11 14:09:27 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Ahmed Mehfooz | e32a4b5 | 2020-04-14 20:34:10 | [diff] [blame] | 9 | #include "ash/root_window_controller.h" |
| 10 | #include "ash/shell.h" |
| 11 | #include "ash/system/accessibility/select_to_speak_tray.h" |
| 12 | #include "ash/system/status_area_widget.h" |
| 13 | #include "ash/system/time/time_tray_item_view.h" |
| 14 | #include "ash/system/time/time_view.h" |
| 15 | #include "ash/system/unified/unified_system_tray.h" |
| 16 | #include "ash/system/unified/unified_system_tray_bubble.h" |
| 17 | #include "ash/system/unified/unified_system_tray_controller.h" |
| 18 | #include "base/strings/string16.h" |
| 19 | #include "ui/events/test/event_generator.h" |
| 20 | #include "ui/views/controls/label.h" |
| 21 | #include "ui/views/view.h" |
| 22 | #include "ui/views/widget/widget_utils.h" |
| 23 | |
| 24 | namespace { |
| 25 | ash::UnifiedSystemTray* GetTray() { |
| 26 | return ash::Shell::Get() |
| 27 | ->GetPrimaryRootWindowController() |
| 28 | ->GetStatusAreaWidget() |
| 29 | ->unified_system_tray(); |
| 30 | } |
| 31 | |
| 32 | views::View* GetBubbleView(int view_id) { |
| 33 | return GetTray()->bubble()->GetBubbleView()->GetViewByID(view_id); |
| 34 | } |
| 35 | |
| 36 | } // namespace |
| 37 | |
| 38 | namespace ash { |
| 39 | |
| 40 | SystemTrayTestApi::SystemTrayTestApi() = default; |
| 41 | |
| 42 | SystemTrayTestApi::~SystemTrayTestApi() = default; |
| 43 | |
| 44 | bool SystemTrayTestApi::IsTrayBubbleOpen() { |
| 45 | return GetTray()->IsBubbleShown(); |
| 46 | } |
| 47 | |
| 48 | bool SystemTrayTestApi::IsTrayBubbleExpanded() { |
| 49 | return GetTray()->bubble_->controller_->IsExpanded(); |
| 50 | } |
| 51 | |
| 52 | void SystemTrayTestApi::ShowBubble() { |
Ahmed Mehfooz | 15cebdd | 2021-03-03 01:07:07 | [diff] [blame] | 53 | GetTray()->ShowBubble(); |
Ahmed Mehfooz | e32a4b5 | 2020-04-14 20:34:10 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void SystemTrayTestApi::CloseBubble() { |
| 57 | GetTray()->CloseBubble(); |
| 58 | } |
| 59 | |
| 60 | void SystemTrayTestApi::CollapseBubble() { |
| 61 | GetTray()->EnsureQuickSettingsCollapsed(true /*animate*/); |
| 62 | } |
| 63 | |
| 64 | void SystemTrayTestApi::ExpandBubble() { |
| 65 | GetTray()->EnsureBubbleExpanded(); |
| 66 | } |
| 67 | |
| 68 | void SystemTrayTestApi::ShowAccessibilityDetailedView() { |
Ahmed Mehfooz | 15cebdd | 2021-03-03 01:07:07 | [diff] [blame] | 69 | GetTray()->ShowBubble(); |
Ahmed Mehfooz | e32a4b5 | 2020-04-14 20:34:10 | [diff] [blame] | 70 | GetTray()->bubble_->controller_->ShowAccessibilityDetailedView(); |
| 71 | } |
| 72 | |
| 73 | void SystemTrayTestApi::ShowNetworkDetailedView() { |
Ahmed Mehfooz | 15cebdd | 2021-03-03 01:07:07 | [diff] [blame] | 74 | GetTray()->ShowBubble(); |
Ahmed Mehfooz | e32a4b5 | 2020-04-14 20:34:10 | [diff] [blame] | 75 | GetTray()->bubble_->controller_->ShowNetworkDetailedView(true /* force */); |
| 76 | } |
| 77 | |
| 78 | bool SystemTrayTestApi::IsBubbleViewVisible(int view_id, bool open_tray) { |
| 79 | if (open_tray) |
Ahmed Mehfooz | 15cebdd | 2021-03-03 01:07:07 | [diff] [blame] | 80 | GetTray()->ShowBubble(); |
Ahmed Mehfooz | e32a4b5 | 2020-04-14 20:34:10 | [diff] [blame] | 81 | views::View* view = GetBubbleView(view_id); |
| 82 | return view && view->GetVisible(); |
| 83 | } |
| 84 | |
| 85 | void SystemTrayTestApi::ClickBubbleView(int view_id) { |
| 86 | views::View* view = GetBubbleView(view_id); |
| 87 | if (view && view->GetVisible()) { |
| 88 | gfx::Point cursor_location = view->GetLocalBounds().CenterPoint(); |
| 89 | views::View::ConvertPointToScreen(view, &cursor_location); |
| 90 | |
| 91 | ui::test::EventGenerator generator(GetRootWindow(view->GetWidget())); |
| 92 | generator.MoveMouseTo(cursor_location); |
| 93 | generator.ClickLeftButton(); |
| 94 | } |
| 95 | } |
| 96 | |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 97 | std::u16string SystemTrayTestApi::GetBubbleViewTooltip(int view_id) { |
Ahmed Mehfooz | e32a4b5 | 2020-04-14 20:34:10 | [diff] [blame] | 98 | views::View* view = GetBubbleView(view_id); |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 99 | return view ? view->GetTooltipText(gfx::Point()) : std::u16string(); |
Ahmed Mehfooz | e32a4b5 | 2020-04-14 20:34:10 | [diff] [blame] | 100 | } |
| 101 | |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 102 | std::u16string SystemTrayTestApi::GetBubbleViewText(int view_id) { |
Andre Le | e0f19547 | 2020-07-22 21:24:25 | [diff] [blame] | 103 | views::View* view = GetBubbleView(view_id); |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 104 | return view ? static_cast<views::Label*>(view)->GetText() : std::u16string(); |
Andre Le | e0f19547 | 2020-07-22 21:24:25 | [diff] [blame] | 105 | } |
| 106 | |
Ahmed Mehfooz | e32a4b5 | 2020-04-14 20:34:10 | [diff] [blame] | 107 | bool SystemTrayTestApi::Is24HourClock() { |
| 108 | base::HourClockType type = |
| 109 | GetTray()->time_view_->time_view()->GetHourTypeForTesting(); |
| 110 | return type == base::k24HourClock; |
| 111 | } |
| 112 | |
| 113 | void SystemTrayTestApi::TapSelectToSpeakTray() { |
| 114 | // The Select-to-Speak tray doesn't actually use the event, so construct |
| 115 | // a bare bones event to perform the action. |
Dave Tapuska | 1621efb | 2020-04-29 15:39:54 | [diff] [blame] | 116 | ui::TouchEvent event(ui::ET_TOUCH_PRESSED, gfx::Point(), |
| 117 | base::TimeTicks::Now(), |
| 118 | ui::PointerDetails(ui::EventPointerType::kTouch), 0); |
Ahmed Mehfooz | e32a4b5 | 2020-04-14 20:34:10 | [diff] [blame] | 119 | StatusAreaWidget* status_area_widget = |
| 120 | RootWindowController::ForWindow(GetTray()->GetWidget()->GetNativeWindow()) |
| 121 | ->GetStatusAreaWidget(); |
| 122 | status_area_widget->select_to_speak_tray()->PerformAction(event); |
| 123 | } |
| 124 | |
| 125 | message_center::MessagePopupView* |
| 126 | SystemTrayTestApi::GetPopupViewForNotificationID( |
| 127 | const std::string& notification_id) { |
| 128 | return GetTray()->GetPopupViewForNotificationID(notification_id); |
| 129 | } |
| 130 | |
| 131 | // static |
| 132 | std::unique_ptr<SystemTrayTestApi> SystemTrayTestApi::Create() { |
| 133 | return std::make_unique<SystemTrayTestApi>(); |
| 134 | } |
| 135 | |
| 136 | } // namespace ash |