File: content_setting_bubble_model_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 (373 lines) | stat: -rw-r--r-- 16,958 bytes parent folder | download | duplicates (6)
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
// Copyright 2014 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/content_settings/content_setting_bubble_model.h"

#include <memory>
#include <vector>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/content_settings/chrome_content_settings_utils.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_content_setting_bubble_model_delegate.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/content_settings/fake_owner.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/browser/page_specific_content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/features.h"
#include "components/content_settings/core/test/content_settings_mock_provider.h"
#include "components/content_settings/core/test/content_settings_test_utils.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "components/permissions/permission_util.h"
#include "components/strings/grit/components_strings.h"
#include "components/url_formatter/elide_url.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/request_handler_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/event.h"

using content_settings::PageSpecificContentSettings;

class ContentSettingBubbleModelMediaStreamTest : public InProcessBrowserTest {
 public:
  void ManageMediaStreamSettings(
      PageSpecificContentSettings::MicrophoneCameraState state) {
    content::WebContents* original_tab = OpenTab();
    std::unique_ptr<ContentSettingBubbleModel> bubble = ShowBubble(state);

    // Click the manage button, which opens in a new tab or window. Wait until
    // it loads.
    bubble->OnManageButtonClicked();
    ASSERT_NE(GetActiveTab(), original_tab);
    content::TestNavigationObserver observer(GetActiveTab());
    observer.Wait();
  }

  std::unique_ptr<ContentSettingBubbleModel> ShowBubble(
      PageSpecificContentSettings::MicrophoneCameraState state) {
    content::WebContents* web_contents = GetActiveTab();

    // Create a bubble with the given camera and microphone access state.
    PageSpecificContentSettings::GetForFrame(
        web_contents->GetPrimaryMainFrame())
        ->OnMediaStreamPermissionSet(web_contents->GetLastCommittedURL(),
                                     state);
    return std::make_unique<ContentSettingMediaStreamBubbleModel>(
        browser()->content_setting_bubble_model_delegate(), web_contents);
  }

  content::WebContents* GetActiveTab() {
    // First, we need to find the active browser window. It should be at
    // the same desktop as the browser in which we invoked the bubble.
    Browser* active_browser = chrome::FindLastActive();
    return active_browser->tab_strip_model()->GetActiveWebContents();
  }

  content::WebContents* OpenTab() {
    // Open a tab for which we will invoke the media bubble.
    GURL url(
        https_server_->GetURL("/content_setting_bubble/mixed_script.html"));
    EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
    return GetActiveTab();
  }

 protected:
  void SetUpInProcessBrowserTestFixture() override {
    scoped_feature_list_.InitWithFeatures(
        {}, {content_settings::features::kLeftHandSideActivityIndicators});

    https_server_ = std::make_unique<net::EmbeddedTestServer>(
        net::EmbeddedTestServer::TYPE_HTTPS);
    https_server_->ServeFilesFromSourceDirectory(GetChromeTestDataDir());
    ASSERT_TRUE(https_server_->Start());
  }

  void BlockMediaStreamPermissionWithSupervision(
      const std::vector<ContentSettingsType>& types) {
    auto provider = std::make_unique<content_settings::MockProvider>();
    for (ContentSettingsType type : types) {
      provider->SetWebsiteSetting(
          ContentSettingsPattern::Wildcard(),
          ContentSettingsPattern::Wildcard(), type,
          base::Value(ContentSetting::CONTENT_SETTING_BLOCK),
          /*constraints=*/{},
          content_settings::PartitionKey::GetDefaultForTesting());
    }
    HostContentSettingsMap* map =
        HostContentSettingsMapFactory::GetForProfile(browser()->profile());
    content_settings::TestUtils::OverrideProvider(
        map, std::move(provider),
        content_settings::ProviderType::kSupervisedProvider);
  }

  std::unique_ptr<net::EmbeddedTestServer> https_server_;
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Tests that clicking on the manage button in the media bubble opens the
// correct section of the settings UI. This test sometimes leaks memory,
// detected by linux_chromium_asan_rel_ng. See http://crbug/668693 for more
// info.
IN_PROC_BROWSER_TEST_F(ContentSettingBubbleModelMediaStreamTest,
                       DISABLED_ManageLink) {
  // For each of the three options, we click the manage button and check if the
  // active tab loads the correct internal url.

  // The microphone bubble links to microphone exceptions.
  ManageMediaStreamSettings({PageSpecificContentSettings::kMicrophoneAccessed});
  EXPECT_EQ(GURL("chrome://settings/contentExceptions#media-stream-mic"),
            GetActiveTab()->GetLastCommittedURL());

  // The bubble for both media devices links to the the first section of the
  // default media content settings, which is the microphone section.
  ManageMediaStreamSettings({PageSpecificContentSettings::kMicrophoneAccessed,
                             PageSpecificContentSettings::kCameraAccessed});
  EXPECT_EQ(GURL("chrome://settings/content#media-stream-mic"),
            GetActiveTab()->GetLastCommittedURL());

  // The camera bubble links to camera exceptions.
  ManageMediaStreamSettings({PageSpecificContentSettings::kCameraAccessed});
  EXPECT_EQ(GURL("chrome://settings/contentExceptions#media-stream-camera"),
            GetActiveTab()->GetLastCommittedURL());
}

// Tests that media bubble content matches the expect layout when the
// mic is not blocked on NTP
IN_PROC_BROWSER_TEST_F(ContentSettingBubbleModelMediaStreamTest,
                       BubbleContentForNonBlockedMic) {
  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(web_contents);

  content::RenderFrameHost* main_rfh =
      ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
          browser(), GURL(chrome::kChromeUINewTabURL), 1);
  content::WebContents::FromRenderFrameHost(main_rfh)->Focus();

  ASSERT_TRUE(main_rfh);
  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
            web_contents->GetLastCommittedURL());
  EXPECT_EQ(GURL(chrome::kChromeUINewTabPageURL),
            main_rfh->GetLastCommittedOrigin().GetURL());

  // Create a bubble with the given camera and microphone access state.
  PageSpecificContentSettings::GetForFrame(web_contents->GetPrimaryMainFrame())
      ->OnMediaStreamPermissionSet(
          permissions::PermissionUtil::GetCanonicalOrigin(
              ContentSettingsType::MEDIASTREAM_MIC,
              web_contents->GetPrimaryMainFrame()
                  ->GetLastCommittedOrigin()
                  .GetURL(),
              web_contents->GetLastCommittedURL()),
          {PageSpecificContentSettings::kMicrophoneAccessed});
  std::unique_ptr<ContentSettingBubbleModel> mic_bubble =
      std::make_unique<ContentSettingMediaStreamBubbleModel>(
          browser()->content_setting_bubble_model_delegate(), web_contents);

  EXPECT_TRUE(mic_bubble->bubble_content().is_user_modifiable);
}

// Tests that media bubble content matches the expect layout when the
// microphone is blocked by a custodian.
IN_PROC_BROWSER_TEST_F(ContentSettingBubbleModelMediaStreamTest,
                       BubbleContentForSupervisedBlockedMic) {
  BlockMediaStreamPermissionWithSupervision(
      {ContentSettingsType::MEDIASTREAM_MIC});

  OpenTab();

  std::unique_ptr<ContentSettingBubbleModel> mic_and_camera_bubble =
      ShowBubble({PageSpecificContentSettings::kMicrophoneAccessed,
                  PageSpecificContentSettings::kCameraAccessed});
  EXPECT_TRUE(mic_and_camera_bubble->bubble_content().is_user_modifiable);

  std::unique_ptr<ContentSettingBubbleModel> mic_bubble =
      ShowBubble({PageSpecificContentSettings::kMicrophoneAccessed});
  EXPECT_FALSE(mic_bubble->bubble_content().is_user_modifiable);

  std::unique_ptr<ContentSettingBubbleModel> camera_bubble =
      ShowBubble({PageSpecificContentSettings::kCameraAccessed});
  EXPECT_TRUE(camera_bubble->bubble_content().is_user_modifiable);
}

// Tests that media bubble content matches the expect layout when the
// camera is blocked by a custodian.
IN_PROC_BROWSER_TEST_F(ContentSettingBubbleModelMediaStreamTest,
                       BubbleContentForSupervisedBlockedCamera) {
  BlockMediaStreamPermissionWithSupervision(
      {ContentSettingsType::MEDIASTREAM_CAMERA});

  OpenTab();

  std::unique_ptr<ContentSettingBubbleModel> mic_and_camera_bubble =
      ShowBubble({PageSpecificContentSettings::kMicrophoneAccessed,
                  PageSpecificContentSettings::kCameraAccessed});
  EXPECT_TRUE(mic_and_camera_bubble->bubble_content().is_user_modifiable);

  std::unique_ptr<ContentSettingBubbleModel> mic_bubble =
      ShowBubble({PageSpecificContentSettings::kMicrophoneAccessed});
  EXPECT_TRUE(mic_bubble->bubble_content().is_user_modifiable);

  std::unique_ptr<ContentSettingBubbleModel> camera_bubble =
      ShowBubble({PageSpecificContentSettings::kCameraAccessed});
  EXPECT_FALSE(camera_bubble->bubble_content().is_user_modifiable);
}

// Tests that media bubble content matches the expect layout when the
// mic and camera are blocked by a custodian.
IN_PROC_BROWSER_TEST_F(ContentSettingBubbleModelMediaStreamTest,
                       BubbleContentForSupervisedBlockedMicAndCamera) {
  BlockMediaStreamPermissionWithSupervision(
      {ContentSettingsType::MEDIASTREAM_CAMERA,
       ContentSettingsType::MEDIASTREAM_MIC});

  OpenTab();

  std::unique_ptr<ContentSettingBubbleModel> mic_and_camera_bubble =
      ShowBubble({PageSpecificContentSettings::kMicrophoneAccessed,
                  PageSpecificContentSettings::kCameraAccessed});
  EXPECT_FALSE(mic_and_camera_bubble->bubble_content().is_user_modifiable);

  std::unique_ptr<ContentSettingBubbleModel> mic_bubble =
      ShowBubble({PageSpecificContentSettings::kMicrophoneAccessed});
  EXPECT_FALSE(mic_bubble->bubble_content().is_user_modifiable);

  std::unique_ptr<ContentSettingBubbleModel> camera_bubble =
      ShowBubble({PageSpecificContentSettings::kCameraAccessed});
  EXPECT_FALSE(camera_bubble->bubble_content().is_user_modifiable);
}

// Tests that media bubble content includes camera PTZ when the permission has
// been granted to the website.
IN_PROC_BROWSER_TEST_F(ContentSettingBubbleModelMediaStreamTest,
                       BubbleContentIncludesCameraPanTiltZoom) {
  content::WebContents* web_contents = OpenTab();
  GURL url = web_contents->GetLastCommittedURL();

  // Do not grant camera PTZ permission to current tab.
  HostContentSettingsMapFactory::GetForProfile(browser()->profile())
      ->SetContentSettingDefaultScope(url, GURL(),
                                      ContentSettingsType::CAMERA_PAN_TILT_ZOOM,
                                      CONTENT_SETTING_ASK);

  // The mic & camera bubble content does not include camera PTZ.
  std::unique_ptr<ContentSettingBubbleModel> mic_and_camera_bubble =
      ShowBubble({PageSpecificContentSettings::kMicrophoneAccessed,
                  PageSpecificContentSettings::kCameraAccessed});
  EXPECT_EQ(mic_and_camera_bubble->bubble_content().radio_group.radio_items[0],
            l10n_util::GetStringFUTF16(
                IDS_ALLOWED_MEDIASTREAM_MIC_AND_CAMERA_NO_ACTION,
                url_formatter::FormatUrlForSecurityDisplay(url)));

  // The camera bubble content does not include camera PTZ.
  std::unique_ptr<ContentSettingBubbleModel> camera_bubble =
      ShowBubble({PageSpecificContentSettings::kCameraAccessed});
  EXPECT_EQ(camera_bubble->bubble_content().radio_group.radio_items[0],
            l10n_util::GetStringFUTF16(
                IDS_ALLOWED_MEDIASTREAM_CAMERA_NO_ACTION,
                url_formatter::FormatUrlForSecurityDisplay(url)));

  // Grant camera PTZ permission to current tab.
  HostContentSettingsMapFactory::GetForProfile(browser()->profile())
      ->SetContentSettingDefaultScope(url, GURL(),
                                      ContentSettingsType::CAMERA_PAN_TILT_ZOOM,
                                      CONTENT_SETTING_ALLOW);

  // The mic & camera bubble content includes camera PTZ.
  std::unique_ptr<ContentSettingBubbleModel> mic_and_camera_ptz_bubble =
      ShowBubble({PageSpecificContentSettings::kMicrophoneAccessed,
                  PageSpecificContentSettings::kCameraAccessed});
  EXPECT_EQ(
      mic_and_camera_ptz_bubble->bubble_content().radio_group.radio_items[0],
      l10n_util::GetStringFUTF16(
          IDS_ALLOWED_MEDIASTREAM_MIC_AND_CAMERA_PAN_TILT_ZOOM_NO_ACTION,
          url_formatter::FormatUrlForSecurityDisplay(url)));

  // The camera bubble content includes camera PTZ.
  std::unique_ptr<ContentSettingBubbleModel> camera_ptz_bubble =
      ShowBubble({PageSpecificContentSettings::kCameraAccessed});
  EXPECT_EQ(camera_ptz_bubble->bubble_content().radio_group.radio_items[0],
            l10n_util::GetStringFUTF16(
                IDS_ALLOWED_CAMERA_PAN_TILT_ZOOM_NO_ACTION,
                url_formatter::FormatUrlForSecurityDisplay(url)));
}

class ContentSettingBubbleModelPopupTest : public InProcessBrowserTest {
 protected:
  static constexpr int kDisallowButtonIndex = 1;

  void SetUpInProcessBrowserTestFixture() override {
    https_server_ = std::make_unique<net::EmbeddedTestServer>(
        net::EmbeddedTestServer::TYPE_HTTPS);
    https_server_->ServeFilesFromSourceDirectory(GetChromeTestDataDir());
    ASSERT_TRUE(https_server_->Start());
  }
  std::unique_ptr<net::EmbeddedTestServer> https_server_;
};

// Tests that each popup action is counted in the right bucket.
IN_PROC_BROWSER_TEST_F(ContentSettingBubbleModelPopupTest, PopupsActionsCount) {
  GURL url(https_server_->GetURL("/popup_blocker/popup-many.html"));
  base::HistogramTester histograms;
  histograms.ExpectTotalCount("ContentSettings.Popups", 0);

  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

  histograms.ExpectBucketCount(
      "ContentSettings.Popups",
      content_settings::POPUPS_ACTION_DISPLAYED_BLOCKED_ICON_IN_OMNIBOX, 1);

  // Creates the ContentSettingPopupBubbleModel in order to emulate clicks.
  std::unique_ptr<ContentSettingBubbleModel> model(
      ContentSettingBubbleModel::CreateContentSettingBubbleModel(
          browser()->content_setting_bubble_model_delegate(),
          browser()->tab_strip_model()->GetActiveWebContents(),
          ContentSettingsType::POPUPS));
  std::unique_ptr<FakeOwner> owner =
      FakeOwner::Create(*model, kDisallowButtonIndex);

  histograms.ExpectBucketCount("ContentSettings.Popups",
                               content_settings::POPUPS_ACTION_DISPLAYED_BUBBLE,
                               1);

  ui::MouseEvent click_event(ui::EventType::kMousePressed, gfx::Point(),
                             gfx::Point(), ui::EventTimeForNow(),
                             ui::EF_LEFT_MOUSE_BUTTON,
                             ui::EF_LEFT_MOUSE_BUTTON);

  model->OnListItemClicked(0, click_event);
  histograms.ExpectBucketCount(
      "ContentSettings.Popups",
      content_settings::POPUPS_ACTION_CLICKED_LIST_ITEM_CLICKED, 1);

  model->OnManageButtonClicked();
  histograms.ExpectBucketCount(
      "ContentSettings.Popups",
      content_settings::POPUPS_ACTION_CLICKED_MANAGE_POPUPS_BLOCKING, 1);

  owner->SetSelectedRadioOptionAndCommit(model->kAllowButtonIndex);
  histograms.ExpectBucketCount(
      "ContentSettings.Popups",
      content_settings::POPUPS_ACTION_SELECTED_ALWAYS_ALLOW_POPUPS_FROM, 1);

  histograms.ExpectTotalCount("ContentSettings.Popups", 5);
}