File: desktop_media_picker_views_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (178 lines) | stat: -rw-r--r-- 7,103 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
// Copyright 2017 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/browser/ui/views/desktop_capture/desktop_media_picker_views.h"

#include <memory>
#include <string>

#include "base/functional/callback.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "chrome/browser/media/webrtc/desktop_media_list.h"
#include "chrome/browser/media/webrtc/fake_desktop_media_list.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/desktop_media_id.h"
#include "content/public/test/browser_test.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/window/dialog_delegate.h"

class DesktopMediaPickerViewsBrowserTest : public DialogBrowserTest {
 public:
  DesktopMediaPickerViewsBrowserTest() = default;

  DesktopMediaPickerViewsBrowserTest(
      const DesktopMediaPickerViewsBrowserTest&) = delete;
  DesktopMediaPickerViewsBrowserTest& operator=(
      const DesktopMediaPickerViewsBrowserTest&) = delete;

  // DialogBrowserTest:
  void ShowUi(const std::string& name) override {
    picker_ = std::make_unique<DesktopMediaPickerImpl>();
    auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
    gfx::NativeWindow native_window = browser()->window()->GetNativeWindow();

    std::vector<std::unique_ptr<DesktopMediaList>> sources;
    if (override_source_lists_.empty()) {
      sources = CreateDefaultSourceLists();
    } else {
      for (auto& source : override_source_lists_) {
        sources.push_back(std::move(source));
      }
    }

    std::vector<FakeDesktopMediaList*> source_lists;
    for (const auto& source : sources) {
      source_lists.push_back(static_cast<FakeDesktopMediaList*>(source.get()));
    }

    DesktopMediaPicker::Params picker_params{
        DesktopMediaPicker::Params::RequestSource::kUnknown};
    picker_params.web_contents = web_contents;
    picker_params.context = native_window;
    picker_params.app_name = u"app_name";
    picker_params.target_name = u"target_name";
    picker_params.request_audio = true;
    picker_->Show(picker_params, std::move(sources),
                  DesktopMediaPicker::DoneCallback());

    if (after_show_callback_) {
      std::move(after_show_callback_).Run(source_lists);
    }
  }

 protected:
  std::vector<std::unique_ptr<DesktopMediaList>> CreateDefaultSourceLists() {
    std::vector<std::unique_ptr<DesktopMediaList>> sources;
    for (auto type :
         {DesktopMediaList::Type::kScreen, DesktopMediaList::Type::kWindow,
          DesktopMediaList::Type::kWebContents}) {
      sources.push_back(std::make_unique<FakeDesktopMediaList>(type));
    }
    return sources;
  }

  std::unique_ptr<DesktopMediaPickerImpl> picker_;

  // If this list isn't filled in, a default list of source lists will be
  // created.
  std::vector<std::unique_ptr<FakeDesktopMediaList>> override_source_lists_;

  // This callback is called in ShowUi after the picker dialog has been shown.
  // This both more closely mirrors how this code behaves in production (where
  // the DesktopMediaList is filled asynchronously, so it starts off empty and
  // then becomes filled after the UI is showing) and allows InvokeUi-style
  // tests to update the UI state after showing it.
  base::OnceCallback<void(const std::vector<FakeDesktopMediaList*>&)>
      after_show_callback_;
};

// Invokes a dialog that allows the user to select what view of their desktop
// they would like to share.
IN_PROC_BROWSER_TEST_F(DesktopMediaPickerViewsBrowserTest, InvokeUi_default) {
  after_show_callback_ =
      base::BindOnce([](const std::vector<FakeDesktopMediaList*>& sources) {
        sources[0]->AddSource(0);

        // Fill in a bit of test data for nicer UI screenshots :)
        sources[1]->AddSource(0);
        sources[1]->SetSourceName(0, u"Warty Warthog");
        sources[1]->AddSource(1);
        sources[1]->SetSourceName(1, u"Hoary Hedgehog");
        sources[1]->AddSource(2);
        sources[1]->SetSourceName(2, u"Breezy Badger");

        sources[2]->AddSource(0);
        sources[2]->SetSourceName(0, u"Dapper Drake");
        sources[2]->AddSource(1);
        sources[2]->SetSourceName(1, u"Edgy Eft");
        sources[2]->AddSource(2);
        sources[2]->SetSourceName(2, u"Feisty Fawn");
      });
  ShowAndVerifyUi();
}

// Show the picker UI with only one source type: TYPE_WEB_CONTENTS, aka the
// tab picker.
IN_PROC_BROWSER_TEST_F(DesktopMediaPickerViewsBrowserTest, InvokeUi_tabs) {
  after_show_callback_ =
      base::BindOnce([](const std::vector<FakeDesktopMediaList*>& sources) {
        sources[0]->AddSource(0);
        sources[0]->SetSourceName(0, u"Dapper Drake");
        sources[0]->AddSource(1);
        sources[0]->SetSourceName(1, u"Edgy Eft");
        sources[0]->AddSource(2);
        sources[0]->SetSourceName(2, u"Feisty Fawn");
      });
  override_source_lists_.push_back(std::make_unique<FakeDesktopMediaList>(
      DesktopMediaList::Type::kWebContents));
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(DesktopMediaPickerViewsBrowserTest,
                       InitiallyFocusesDisabledOK) {
  ShowUi(std::string());
  views::DialogDelegate* dialog =
      picker_->GetDialogViewForTesting()->AsDialogDelegate();
  EXPECT_EQ(dialog->GetOkButton(),
            dialog->DialogDelegate::GetInitiallyFocusedView());
  EXPECT_FALSE(picker_->GetDialogViewForTesting()->IsDialogButtonEnabled(
      ui::mojom::DialogButton::kOk));
}

// Validate that the dialog title changes to match the source type when there's
// only one source type present.
IN_PROC_BROWSER_TEST_F(DesktopMediaPickerViewsBrowserTest,
                       SingleSourceTypeChangesTitle) {
  override_source_lists_.push_back(std::make_unique<FakeDesktopMediaList>(
      DesktopMediaList::Type::kWebContents));
  ShowUi(std::string());

  EXPECT_EQ(picker_->GetDialogViewForTesting()->GetWindowTitle(),
            l10n_util::GetStringUTF16(
                IDS_DESKTOP_MEDIA_PICKER_TITLE_WEB_CONTENTS_ONLY));
}

// Validate that the scroll view min height is correct
IN_PROC_BROWSER_TEST_F(DesktopMediaPickerViewsBrowserTest,
                       CorrectScrollViewMinHeight) {
  override_source_lists_.push_back(
      std::make_unique<FakeDesktopMediaList>(DesktopMediaList::Type::kScreen));
  ShowUi(std::string());

  auto* scroll_view = picker_->GetDialogViewForTesting()->GetViewByID(
      VIEW_ID_MEDIA_PICKER_SCREEN_SCROLL_VIEW);
  int expected_height = GetGenericScreenStyle().item_size.height() +
                        GetGenericScreenStyle().label_rect.height();
  EXPECT_EQ(scroll_view->bounds().height(), expected_height);
}