File: media_router_cast_ui_for_test.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (249 lines) | stat: -rw-r--r-- 8,140 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/media_router/media_router_cast_ui_for_test.h"

#include <algorithm>

#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/media/router/media_router_feature.h"
#include "chrome/browser/ui/media_router/media_router_ui.h"
#include "chrome/browser/ui/views/media_router/cast_dialog_coordinator.h"
#include "chrome/browser/ui/views/media_router/cast_dialog_sink_button.h"
#include "chrome/browser/ui/views/media_router/media_router_dialog_controller_views.h"
#include "ui/gfx/geometry/point.h"

namespace media_router {

MediaRouterCastUiForTest::MediaRouterCastUiForTest(
    content::WebContents* web_contents)
    : MediaRouterUiForTestBase(web_contents) {}

MediaRouterCastUiForTest::~MediaRouterCastUiForTest() {
  CHECK(!watch_callback_);
}

void MediaRouterCastUiForTest::SetUp() {}

void MediaRouterCastUiForTest::ShowDialog() {
  dialog_controller_->ShowMediaRouterDialog(
      MediaRouterDialogActivationLocation::TOOLBAR);
  base::RunLoop().RunUntilIdle();
}

bool MediaRouterCastUiForTest::IsDialogShown() const {
  return dialog_controller_->IsShowingMediaRouterDialog();
}

void MediaRouterCastUiForTest::HideDialog() {
  dialog_controller_->HideMediaRouterDialog();
  base::RunLoop().RunUntilIdle();
}

void MediaRouterCastUiForTest::ChooseSourceType(
    CastDialogView::SourceType source_type) {
  CastDialogView* dialog_view = GetDialogView();
  CHECK(dialog_view);

  ClickOnButton(dialog_view->sources_button_for_test());
  int source_index;
  switch (source_type) {
    case CastDialogView::kTab:
      source_index = 0;
      break;
    case CastDialogView::kDesktop:
      source_index = 1;
      break;
  }
  dialog_view->sources_menu_model_for_test()->ActivatedAt(source_index);
}

CastDialogView::SourceType MediaRouterCastUiForTest::GetChosenSourceType()
    const {
  const CastDialogView* dialog_view = GetDialogView();
  CHECK(dialog_view);
  return dialog_view->selected_source_;
}

void MediaRouterCastUiForTest::StartCasting(const std::string& sink_name) {
  CastDialogSinkView* sink_view = GetSinkView(sink_name);
  ClickOnButton(sink_view->cast_sink_button_for_test());
}

void MediaRouterCastUiForTest::StopCasting(const std::string& sink_name) {
  CastDialogSinkView* sink_view = GetSinkView(sink_name);
  if (sink_view->stop_button_for_test()) {
    ClickOnButton(sink_view->stop_button_for_test());
    return;
  }
  NOTREACHED() << "No stop button found for sink " << sink_name;
}

MediaRoute::Id MediaRouterCastUiForTest::GetRouteIdForSink(
    const std::string& sink_name) const {
  CastDialogSinkView* sink_view = GetSinkView(sink_name);
  if (!sink_view->sink().route) {
    return "";
  }
  return sink_view->sink().route->media_route_id();
}

std::string MediaRouterCastUiForTest::GetStatusTextForSink(
    const std::string& sink_name) const {
  CastDialogSinkView* sink_view = GetSinkView(sink_name);
  return base::UTF16ToUTF8(sink_view->sink().status_text);
}

std::string MediaRouterCastUiForTest::GetIssueTextForSink(
    const std::string& sink_name) const {
  CastDialogSinkButton* sink_button =
      static_cast<CastDialogSinkButton*>(GetSinkButton(sink_name));
  if (!sink_button->sink().issue) {
    NOTREACHED() << "Issue not found for sink " << sink_name;
  }
  return sink_button->sink().issue->info().title;
}

void MediaRouterCastUiForTest::WaitForSink(const std::string& sink_name) {
  ObserveDialog(WatchType::kSink, sink_name);
}

void MediaRouterCastUiForTest::WaitForSinkAvailable(
    const std::string& sink_name) {
  ObserveDialog(WatchType::kSinkAvailable, sink_name);
}

void MediaRouterCastUiForTest::WaitForAnyIssue() {
  ObserveDialog(WatchType::kAnyIssue);
}

void MediaRouterCastUiForTest::WaitForAnyRoute() {
  ObserveDialog(WatchType::kAnyRoute);
}

void MediaRouterCastUiForTest::WaitForDialogShown() {
  CHECK(!watch_sink_name_);
  CHECK(!watch_callback_);
  CHECK_EQ(watch_type_, WatchType::kNone);
  if (IsDialogShown())
    return;
  WaitForAnyDialogShown();
}

void MediaRouterCastUiForTest::WaitForDialogHidden() {
  if (!IsDialogShown())
    return;

  ObserveDialog(WatchType::kDialogHidden);
}

void MediaRouterCastUiForTest::OnDialogCreated() {
  MediaRouterUiForTestBase::OnDialogCreated();
  GetDialogView()->KeepShownForTesting();
}

void MediaRouterCastUiForTest::OnDialogModelUpdated(
    CastDialogView* dialog_view) {
  if (!watch_callback_ || watch_type_ == WatchType::kDialogShown ||
      watch_type_ == WatchType::kDialogHidden) {
    return;
  }

  const std::vector<raw_ptr<CastDialogSinkView, DanglingUntriaged>>&
      sink_views = dialog_view->sink_views_for_test();
  if (std::ranges::any_of(
          sink_views, [&, this](CastDialogSinkView* sink_view) {
            switch (watch_type_) {
              case WatchType::kSink:
                return sink_view->sink().friendly_name ==
                       base::UTF8ToUTF16(*watch_sink_name_);
              case WatchType::kSinkAvailable:
                return sink_view->sink().friendly_name ==
                           base::UTF8ToUTF16(*watch_sink_name_) &&
                       sink_view->sink().state == UIMediaSinkState::AVAILABLE &&
                       sink_view->cast_sink_button_for_test()->GetEnabled();
              case WatchType::kAnyIssue:
                return sink_view->sink().issue.has_value();
              case WatchType::kAnyRoute:
                return sink_view->sink().route.has_value();
              case WatchType::kNone:
              case WatchType::kDialogShown:
              case WatchType::kDialogHidden:
                NOTREACHED() << "Invalid WatchType";
            }
          })) {
    std::move(*watch_callback_).Run();
    watch_callback_.reset();
    watch_sink_name_.reset();
    watch_type_ = WatchType::kNone;
    dialog_view->RemoveObserver(this);
  }
}

void MediaRouterCastUiForTest::OnDialogWillClose(CastDialogView* dialog_view) {
  if (watch_type_ == WatchType::kDialogHidden) {
    std::move(*watch_callback_).Run();
    watch_callback_.reset();
    watch_type_ = WatchType::kNone;
  }
  CHECK(!watch_callback_);
  if (dialog_view)
    dialog_view->RemoveObserver(this);
}

CastDialogSinkButton* MediaRouterCastUiForTest::GetSinkButton(
    const std::string& sink_name) const {
  return GetSinkView(sink_name)->cast_sink_button_for_test();
}

void MediaRouterCastUiForTest::ObserveDialog(
    WatchType watch_type,
    std::optional<std::string> sink_name) {
  CHECK(!watch_sink_name_);
  CHECK(!watch_callback_);
  CHECK_EQ(watch_type_, WatchType::kNone);
  base::RunLoop run_loop;
  watch_sink_name_ = std::move(sink_name);
  watch_callback_ = run_loop.QuitClosure();
  watch_type_ = watch_type;

  CastDialogView* dialog_view = GetDialogView();
  CHECK(dialog_view);
  dialog_view->AddObserver(this);
  // Check if the current dialog state already meets the condition that we are
  // waiting for.
  OnDialogModelUpdated(dialog_view);

  run_loop.Run();
}

const CastDialogView* MediaRouterCastUiForTest::GetDialogView() const {
  return dialog_controller_->GetCastDialogCoordinatorForTesting()
      .GetCastDialogView();
}

CastDialogView* MediaRouterCastUiForTest::GetDialogView() {
  return dialog_controller_->GetCastDialogCoordinatorForTesting()
      .GetCastDialogView();
}

CastDialogSinkView* MediaRouterCastUiForTest::GetSinkView(
    const std::string& sink_name) const {
  const CastDialogView* dialog_view = GetDialogView();
  CHECK(dialog_view);
  const std::vector<raw_ptr<CastDialogSinkView, DanglingUntriaged>>&
      sink_views = dialog_view->sink_views_for_test();
  auto it = std::ranges::find(sink_views, base::UTF8ToUTF16(sink_name),
                              [](CastDialogSinkView* sink_view) {
                                return sink_view->sink().friendly_name;
                              });
  if (it == sink_views.end()) {
    NOTREACHED() << "Sink view not found for sink: " << sink_name;
  } else {
    return it->get();
  }
}

}  // namespace media_router