File: auto_pip_setting_helper_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (384 lines) | stat: -rw-r--r-- 14,309 bytes parent folder | download | duplicates (3)
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright 2023 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/picture_in_picture/auto_pip_setting_helper.h"

#include <memory>
#include <utility>

#include "base/memory/raw_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "chrome/browser/picture_in_picture/auto_pip_setting_overlay_view.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/permissions/permission_decision_auto_blocker.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "media/base/picture_in_picture_events_info.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_utils.h"

using testing::_;
using testing::AtLeast;
using testing::Return;

using UiResult = AutoPipSettingView::UiResult;
using AutoPipReason = media::PictureInPictureEventsInfo::AutoPipReason;

namespace {

const char kVideoConferencingHistogram[] =
    "Media.AutoPictureInPicture.EnterPictureInPicture.AutomaticReason."
    "VideoConferencing.PromptResultV2";
const char kMediaPlaybackHistogram[] =
    "Media.AutoPictureInPicture.EnterPictureInPicture.AutomaticReason."
    "MediaPlayback.PromptResultV2";

struct TestParams {
  AutoPipReason auto_pip_reason;
};

}  // anonymous namespace

class MockAutoBlocker : public permissions::PermissionDecisionAutoBlockerBase {
 public:
  MOCK_METHOD(bool,
              IsEmbargoed,
              (const GURL& request_origin, ContentSettingsType permission),
              (override));
  MOCK_METHOD(bool,
              RecordDismissAndEmbargo,
              (const GURL& url,
               ContentSettingsType permission,
               bool dismissed_prompt_was_quiet),
              (override));
  MOCK_METHOD(bool,
              RecordIgnoreAndEmbargo,
              (const GURL& url,
               ContentSettingsType permission,
               bool ignored_prompt_was_quiet),
              (override));
};

class AutoPipSettingHelperTest
    : public views::ViewsTestBase,
      public testing::WithParamInterface<TestParams> {
 public:
  AutoPipSettingHelperTest() = default;

  void SetUp() override {
    ViewsTestBase::SetUp();
    widget_ =
        CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
    widget_->Show();

    // Create parent Widget for AutoPiP setting view.
    parent_widget_ =
        CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
    parent_widget_->Show();

    // Create the anchor Widget for AutoPiP setting view.
    anchor_view_widget_ =
        CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
    anchor_view_widget_->Show();

    HostContentSettingsMap::RegisterProfilePrefs(prefs_.registry());
    settings_map_ = base::MakeRefCounted<HostContentSettingsMap>(
        &prefs_, false /* is_off_the_record */, false /* store_last_modified */,
        false /* restore_session */, true /* should_record_metrics */);

    setting_helper_ = std::make_unique<AutoPipSettingHelper>(
        origin_, settings_map_.get(), &auto_blocker());
  }

  void TearDown() override {
    anchor_view_widget_.reset();
    parent_widget_.reset();
    setting_overlay_ = nullptr;
    widget_.reset();
    setting_helper_.reset();
    ViewsTestBase::TearDown();
    settings_map_->ShutdownOnUIThread();
  }

  AutoPipSettingHelper* setting_helper() { return setting_helper_.get(); }
  AutoPipSettingOverlayView* setting_overlay() const {
    return setting_overlay_;
  }

  AutoPipSettingView* setting_view() const {
    return setting_overlay()->get_view_for_testing();
  }

  void clear_setting_helper() { setting_helper_.reset(); }

  views::Widget* widget() const { return widget_.get(); }

  base::MockOnceCallback<void()>& close_cb() { return close_cb_; }

  // Ask the helper for the overlay view, and return whatever it gives us.  This
  // may be null if it decides that one shouldn't be shown.
  AutoPipSettingOverlayView* AttachOverlayView(
      AutoPipReason auto_pip_reason = AutoPipReason::kUnknown) {
    auto* anchor_view =
        anchor_view_widget_->SetContentsView(std::make_unique<views::View>());
    auto setting_overlay = setting_helper_->CreateOverlayViewIfNeeded(
        close_cb_.Get(), auto_pip_reason, std::nullopt, anchor_view,
        views::BubbleBorder::TOP_CENTER);
    if (setting_overlay) {
      setting_overlay_ = static_cast<AutoPipSettingOverlayView*>(
          widget_->SetContentsView(std::move(setting_overlay)));
    } else {
      setting_overlay_ = nullptr;
    }

    return setting_overlay_;
  }

  void set_content_setting(ContentSetting new_setting) {
    settings_map_->SetContentSettingDefaultScope(
        origin_, GURL(), ContentSettingsType::AUTO_PICTURE_IN_PICTURE,
        new_setting);
  }

  ContentSetting get_content_setting() {
    return settings_map_->GetContentSetting(
        origin_, GURL(), ContentSettingsType::AUTO_PICTURE_IN_PICTURE);
  }

  MockAutoBlocker& auto_blocker() { return auto_blocker_; }

  // Set up expectations that there will be no interaction with the permissions
  // auto blocker.
  void ExpectEmbargoWillNotBeChecked() {
    EXPECT_CALL(auto_blocker(), IsEmbargoed(_, _)).Times(0);
  }

  // Expect that there will be an embargo check, and that there isn't currently
  // an embargo.
  void SetupNoEmbargo() {
    // Expect `auto_blocker()` will be called at least once for the correct
    // origin and content setting, but never for anything else.
    EXPECT_CALL(auto_blocker(), IsEmbargoed(_, _)).Times(0);
    EXPECT_CALL(
        auto_blocker(),
        IsEmbargoed(origin_, ContentSettingsType::AUTO_PICTURE_IN_PICTURE))
        .Times(AtLeast(1))
        .WillRepeatedly(Return(false));
  }

  const GURL& origin() const { return origin_; }

 private:
  base::MockOnceCallback<void()> close_cb_;

  std::unique_ptr<views::Widget> widget_;
  raw_ptr<AutoPipSettingOverlayView> setting_overlay_ = nullptr;
  std::unique_ptr<views::Widget> parent_widget_;
  std::unique_ptr<views::Widget> anchor_view_widget_;

  const GURL origin_{"https://example.com"};

  // Used by the HostContentSettingsMap instance.
  sync_preferences::TestingPrefServiceSyncable prefs_;

  // Used by the AutoPipSettingHelper instance.
  scoped_refptr<HostContentSettingsMap> settings_map_;
  MockAutoBlocker auto_blocker_;

  std::unique_ptr<AutoPipSettingHelper> setting_helper_;
};

TEST_F(AutoPipSettingHelperTest, NoUiIfContentSettingIsAllow) {
  set_content_setting(CONTENT_SETTING_ALLOW);
  ExpectEmbargoWillNotBeChecked();

  EXPECT_CALL(close_cb(), Run()).Times(0);
  AttachOverlayView();
  EXPECT_FALSE(setting_overlay());
  EXPECT_EQ(get_content_setting(), CONTENT_SETTING_ALLOW);
}

TEST_F(AutoPipSettingHelperTest, UiShownIfContentSettingIsAsk) {
  set_content_setting(CONTENT_SETTING_ASK);
  SetupNoEmbargo();

  EXPECT_CALL(close_cb(), Run()).Times(0);
  AttachOverlayView();
  EXPECT_TRUE(setting_overlay());
  EXPECT_EQ(get_content_setting(), CONTENT_SETTING_ASK);
}

TEST_F(AutoPipSettingHelperTest,
       NoUiButCallbackIsCalledIfContentSettingIsBlock) {
  set_content_setting(CONTENT_SETTING_BLOCK);
  ExpectEmbargoWillNotBeChecked();

  EXPECT_CALL(close_cb(), Run()).Times(1);
  AttachOverlayView();
  EXPECT_FALSE(setting_overlay());
  EXPECT_EQ(get_content_setting(), CONTENT_SETTING_BLOCK);
}

TEST_F(AutoPipSettingHelperTest, AllowOnceDoesNotCallCloseCb) {
  set_content_setting(CONTENT_SETTING_DEFAULT);
  SetupNoEmbargo();
  ASSERT_TRUE(AttachOverlayView());
  setting_overlay()->ShowBubble(widget()->GetNativeView());

  // Run result callback with "allow once" UiResult.  Nothing should happen.
  EXPECT_CALL(close_cb(), Run()).Times(0);
  setting_view()->simulate_button_press_for_testing(UiResult::kAllowOnce);
  EXPECT_EQ(get_content_setting(), CONTENT_SETTING_ASK);

  // Also verify that asking again does not create the view again, since 'allow
  // once' persists for the lifetime of the setting helper.
  ASSERT_FALSE(AttachOverlayView());
}

TEST_F(AutoPipSettingHelperTest, AllowOnEveryVisitDoesNotCallCloseCb) {
  set_content_setting(CONTENT_SETTING_DEFAULT);
  SetupNoEmbargo();
  ASSERT_TRUE(AttachOverlayView());
  setting_overlay()->ShowBubble(widget()->GetNativeView());

  // Run result callback with "allow on every visit" UiResult.  Nothing should
  // happen.
  EXPECT_CALL(close_cb(), Run()).Times(0);
  setting_view()->simulate_button_press_for_testing(
      UiResult::kAllowOnEveryVisit);
  EXPECT_EQ(get_content_setting(), CONTENT_SETTING_ALLOW);
}

TEST_F(AutoPipSettingHelperTest, BlockDoesCallCloseCb) {
  set_content_setting(CONTENT_SETTING_DEFAULT);
  SetupNoEmbargo();
  ASSERT_TRUE(AttachOverlayView());
  setting_overlay()->ShowBubble(widget()->GetNativeView());

  // Run result callback with "block" UiResult.  The close cb should be called.
  EXPECT_CALL(close_cb(), Run()).Times(1);
  setting_view()->simulate_button_press_for_testing(UiResult::kBlock);
  EXPECT_EQ(get_content_setting(), CONTENT_SETTING_BLOCK);
}

TEST_F(AutoPipSettingHelperTest,
       DestructionDoesNotNotifyEmbargoEvenIfUiIsCreated) {
  // If the UI is created and not used, and the window is destroyed without
  // notifying the helper that the user explicitly closed it, then the embargo
  // should not be updated.  For example, switching back to the opener tab or
  // the site closing the pip window should not count against the embargo.
  set_content_setting(CONTENT_SETTING_DEFAULT);
  SetupNoEmbargo();
  EXPECT_CALL(auto_blocker(), RecordDismissAndEmbargo(_, _, _)).Times(0);

  // The close cb shouldn't be called because the setting helper shouldn't want
  // to close the window.
  EXPECT_CALL(close_cb(), Run()).Times(0);
  AttachOverlayView();
  EXPECT_TRUE(setting_overlay());
  // Destroy the setting helper without calling `OnUserClosedWindow()`.
  clear_setting_helper();
  EXPECT_EQ(get_content_setting(), CONTENT_SETTING_ASK);
}

TEST_F(AutoPipSettingHelperTest, DismissNotifiesEmbargoIfUiIsCreated) {
  set_content_setting(CONTENT_SETTING_DEFAULT);
  SetupNoEmbargo();
  EXPECT_CALL(
      auto_blocker(),
      RecordDismissAndEmbargo(
          origin(), ContentSettingsType::AUTO_PICTURE_IN_PICTURE, false))
      .Times(1)
      .WillOnce(Return(true));

  // Notify the setting helper that the user closed the window manually.  The
  // close cb should not be called, because the user closed it already.
  EXPECT_CALL(close_cb(), Run()).Times(0);
  AttachOverlayView();
  EXPECT_TRUE(setting_overlay());
  setting_helper()->OnUserClosedWindow(AutoPipReason::kUnknown, std::nullopt);
  EXPECT_EQ(get_content_setting(), CONTENT_SETTING_ASK);
}

TEST_F(AutoPipSettingHelperTest,
       DismissDoesNotNotifyEmbargoIfUiIsNotRequested) {
  set_content_setting(CONTENT_SETTING_DEFAULT);
  ExpectEmbargoWillNotBeChecked();
  // We don't ask for the UI, so there should not be an embargo check.  There
  // should also not be an embargo update.  That's the point of this test --
  // creating the setting helper but not using it should not cause any update to
  // the embargo.
  EXPECT_CALL(auto_blocker(), RecordDismissAndEmbargo(_, _, _)).Times(0);

  EXPECT_CALL(close_cb(), Run()).Times(0);
  // Do not attach the overlay view, which should prevent a callback since the
  // user wasn't presented with any UI.
  setting_helper()->OnUserClosedWindow(AutoPipReason::kUnknown, std::nullopt);
  EXPECT_EQ(get_content_setting(), CONTENT_SETTING_ASK);
}

TEST_F(AutoPipSettingHelperTest,
       UiIsNotShownIfContentSettingIsAskButUnderEmbargo) {
  set_content_setting(CONTENT_SETTING_ASK);
  EXPECT_CALL(
      auto_blocker(),
      IsEmbargoed(origin(), ContentSettingsType::AUTO_PICTURE_IN_PICTURE))
      .Times(AtLeast(1))
      .WillRepeatedly(Return(true));
  // Since there's no UI shown, there also should not be an embargo update.
  EXPECT_CALL(auto_blocker(), RecordDismissAndEmbargo(_, _, _)).Times(0);

  // Since there's an embargo, we expect that there should not be an overlay
  // view shown.  However, the close cb should be called.
  EXPECT_CALL(close_cb(), Run()).Times(1);
  AttachOverlayView();
  EXPECT_FALSE(setting_overlay());
  // Should not change the content setting as a result of the embargo.
  EXPECT_EQ(get_content_setting(), CONTENT_SETTING_ASK);
}

const struct TestParams kTestHistogramNameParams[] = {
    {AutoPipReason::kUnknown},
    {AutoPipReason::kVideoConferencing},
    {AutoPipReason::kMediaPlayback}};

INSTANTIATE_TEST_SUITE_P(AllHistogramNames,
                         AutoPipSettingHelperTest,
                         testing::ValuesIn(kTestHistogramNameParams));

TEST_P(AutoPipSettingHelperTest, HistogramExpectedCounts) {
  set_content_setting(CONTENT_SETTING_DEFAULT);
  SetupNoEmbargo();
  ASSERT_TRUE(AttachOverlayView(GetParam().auto_pip_reason));
  setting_overlay()->ShowBubble(widget()->GetNativeView());

  // Run result callback with "block" UiResult.  The close cb should be called,
  // and the corresponding histogram count recorded.
  base::HistogramTester histograms;
  EXPECT_CALL(close_cb(), Run()).Times(1);
  setting_view()->simulate_button_press_for_testing(UiResult::kBlock);
  EXPECT_EQ(get_content_setting(), CONTENT_SETTING_BLOCK);

  auto video_conferencing_samples =
      histograms.GetHistogramSamplesSinceCreation(kVideoConferencingHistogram);
  auto media_playback_samples =
      histograms.GetHistogramSamplesSinceCreation(kMediaPlaybackHistogram);

  const auto auto_pip_reason = GetParam().auto_pip_reason;
  if (auto_pip_reason == AutoPipReason::kUnknown) {
    EXPECT_EQ(0, video_conferencing_samples->TotalCount());
    EXPECT_EQ(0, media_playback_samples->TotalCount());
  } else if (auto_pip_reason == AutoPipReason::kVideoConferencing) {
    EXPECT_EQ(1, video_conferencing_samples->TotalCount());
    EXPECT_EQ(0, media_playback_samples->TotalCount());
  } else if (auto_pip_reason == AutoPipReason::kMediaPlayback) {
    EXPECT_EQ(0, video_conferencing_samples->TotalCount());
    EXPECT_EQ(1, media_playback_samples->TotalCount());
  } else {
    FAIL() << "Unhandled auto picture in picture reason: "
           << static_cast<int>(auto_pip_reason);
  }
}