File: content_setting_media_image_model_unittest.mm

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 (214 lines) | stat: -rw-r--r-- 9,804 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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <AVFoundation/AVFoundation.h>

#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/content_settings/page_specific_content_settings_delegate.h"
#include "chrome/browser/permissions/system/system_media_capture_permissions_mac.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/content_settings/content_setting_image_model.h"
#include "chrome/browser/ui/content_settings/media_authorization_wrapper_test.h"
#include "chrome/common/chrome_features.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/browser/page_specific_content_settings.h"
#include "components/content_settings/core/common/features.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/no_state_prefetch/browser/no_state_prefetch_manager.h"
#include "components/permissions/permission_recovery_success_rate_tracker.h"
#include "components/vector_icons/vector_icons.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/vector_icon_types.h"

namespace gfx {
struct VectorIcon;
}

using content_settings::PageSpecificContentSettings;

namespace {

bool HasIcon(const ContentSettingImageModel& model) {
  return !model.GetIcon(gfx::kPlaceholderColor).IsEmpty();
}

void ExpectImageModelState(const ContentSettingImageModel& model,
                           bool is_visible,
                           bool has_icon,
                           const std::u16string& tooltip,
                           int explanatory_string_id,
                           const gfx::VectorIcon* icon_badge) {
  EXPECT_EQ(model.is_visible(), is_visible);
  EXPECT_EQ(HasIcon(model), has_icon);
  EXPECT_EQ(model.get_tooltip(), tooltip);
  EXPECT_EQ(model.explanatory_string_id(), explanatory_string_id);
  EXPECT_EQ(model.get_icon_badge(), icon_badge);
}

class ContentSettingMediaImageModelTest
    : public ChromeRenderViewHostTestHarness {
 protected:
  void SetUp() override {
    ChromeRenderViewHostTestHarness::SetUp();

    PageSpecificContentSettings::CreateForWebContents(
        web_contents(),
        std::make_unique<PageSpecificContentSettingsDelegate>(web_contents()));
    infobars::ContentInfoBarManager::CreateForWebContents(web_contents());

    permissions::PermissionRecoverySuccessRateTracker::CreateForWebContents(
        web_contents());
  }
};

TEST_F(ContentSettingMediaImageModelTest, MediaUpdate) {
  // Keep `kLeftHandSideActivityIndicators` disabled to test camera/mic content
  // setting bubble.
  base::test::ScopedFeatureList scoped_list;
  scoped_list.InitAndDisableFeature(
      content_settings::features::kLeftHandSideActivityIndicators);
  PageSpecificContentSettings::CreateForWebContents(
      web_contents(),
      std::make_unique<PageSpecificContentSettingsDelegate>(web_contents()));
  auto* content_settings = PageSpecificContentSettings::GetForFrame(
      web_contents()->GetPrimaryMainFrame());
  const GURL kTestOrigin("https://www.example.com");
  auto content_setting_image_model =
      ContentSettingImageModel::CreateForContentType(
          ContentSettingImageModel::ImageType::MEDIASTREAM);
  MediaAuthorizationWrapperTest auth_wrapper;
  system_permission_settings::SetMediaAuthorizationWrapperForTesting(
      &auth_wrapper);

  // Camera allowed per site: Test for system level permissions.
  {
    content_settings->OnMediaStreamPermissionSet(
        kTestOrigin, {PageSpecificContentSettings::kCameraAccessed});
    auth_wrapper.SetMockMediaPermissionStatus(AVAuthorizationStatusAuthorized);
    content_setting_image_model->Update(web_contents());
    ExpectImageModelState(*content_setting_image_model, /*is_visible=*/true,
                          /*has_icon=*/true,
                          l10n_util::GetStringUTF16(IDS_CAMERA_ACCESSED), 0,
                          &gfx::VectorIcon::EmptyIcon());
    auth_wrapper.SetMockMediaPermissionStatus(AVAuthorizationStatusDenied);
    content_setting_image_model->Update(web_contents());
    ExpectImageModelState(
        *content_setting_image_model, /*is_visible=*/true,
        /*has_icon=*/true,
        l10n_util::GetStringUTF16(IDS_CAMERA_TURNED_OFF_IN_MACOS),
        IDS_CAMERA_TURNED_OFF, &gfx::VectorIcon::EmptyIcon());
    auth_wrapper.SetMockMediaPermissionStatus(
        AVAuthorizationStatusNotDetermined);
    content_setting_image_model->Update(web_contents());
    EXPECT_FALSE(content_setting_image_model->is_visible());
  }

  // Microphone allowed per site: Test for system level permissions.
  {
    content_settings->OnMediaStreamPermissionSet(
        kTestOrigin, {PageSpecificContentSettings::kMicrophoneAccessed});
    auth_wrapper.SetMockMediaPermissionStatus(AVAuthorizationStatusAuthorized);
    content_setting_image_model->Update(web_contents());
    ExpectImageModelState(*content_setting_image_model, /*is_visible=*/true,
                          /*has_icon=*/true,
                          l10n_util::GetStringUTF16(IDS_MICROPHONE_ACCESSED), 0,
                          &gfx::VectorIcon::EmptyIcon());
    auth_wrapper.SetMockMediaPermissionStatus(AVAuthorizationStatusDenied);
    content_setting_image_model->Update(web_contents());
    ExpectImageModelState(
        *content_setting_image_model, /*is_visible=*/true,
        /*has_icon=*/true,
        l10n_util::GetStringUTF16(IDS_MIC_TURNED_OFF_IN_MACOS),
        IDS_MIC_TURNED_OFF, &gfx::VectorIcon::EmptyIcon());
    auth_wrapper.SetMockMediaPermissionStatus(
        AVAuthorizationStatusNotDetermined);
    content_setting_image_model->Update(web_contents());
    EXPECT_FALSE(content_setting_image_model->is_visible());
  }

  // Microphone & camera allowed per site: Test for system level permissions.
  {
    content_settings->OnMediaStreamPermissionSet(
        kTestOrigin, {PageSpecificContentSettings::kMicrophoneAccessed,
                      PageSpecificContentSettings::kCameraAccessed});
    auth_wrapper.SetMockMediaPermissionStatus(AVAuthorizationStatusAuthorized);
    auth_wrapper.SetMockMediaPermissionStatus(AVAuthorizationStatusAuthorized);
    content_setting_image_model->Update(web_contents());
    ExpectImageModelState(
        *content_setting_image_model, /*is_visible=*/true, /*has_icon=*/true,
        l10n_util::GetStringUTF16(IDS_MICROPHONE_CAMERA_ALLOWED), 0,
        &gfx::VectorIcon::EmptyIcon());
    auth_wrapper.SetMockMediaPermissionStatus(AVAuthorizationStatusDenied);
    auth_wrapper.SetMockMediaPermissionStatus(AVAuthorizationStatusDenied);
    content_setting_image_model->Update(web_contents());
    ExpectImageModelState(
        *content_setting_image_model, /*is_visible=*/true, /*has_icon=*/true,
        l10n_util::GetStringUTF16(IDS_CAMERA_MIC_TURNED_OFF_IN_MACOS),
        IDS_CAMERA_TURNED_OFF, &gfx::VectorIcon::EmptyIcon());
    auth_wrapper.SetMockMediaPermissionStatus(
        AVAuthorizationStatusNotDetermined);
    auth_wrapper.SetMockMediaPermissionStatus(
        AVAuthorizationStatusNotDetermined);
    content_setting_image_model->Update(web_contents());
    EXPECT_EQ(content_setting_image_model->is_visible(), false);
  }

  // Test that system permissions being allowed do not affect the image view,
  // when the per site permission is denied.
  for (const auto system_state :
       {AVAuthorizationStatusAuthorized, AVAuthorizationStatusDenied}) {
    SCOPED_TRACE(system_state);
    auth_wrapper.SetMockMediaPermissionStatus(system_state);
    auth_wrapper.SetMockMediaPermissionStatus(system_state);

    // Camera blocked per site.
    {
      content_settings->OnMediaStreamPermissionSet(
          kTestOrigin, {PageSpecificContentSettings::kCameraAccessed,
                        PageSpecificContentSettings::kCameraBlocked});
      content_setting_image_model->Update(web_contents());
      ExpectImageModelState(*content_setting_image_model,
                            /*is_visible=*/true,
                            /*has_icon=*/true,
                            l10n_util::GetStringUTF16(IDS_CAMERA_BLOCKED), 0,
                            &gfx::VectorIcon::EmptyIcon());
    }

    // Microphone blocked per site.
    {
      content_settings->OnMediaStreamPermissionSet(
          kTestOrigin, {PageSpecificContentSettings::kMicrophoneAccessed,
                        PageSpecificContentSettings::kMicrophoneBlocked});
      content_setting_image_model->Update(web_contents());
      ExpectImageModelState(*content_setting_image_model,
                            /*is_visible=*/true,
                            /*has_icon=*/true,
                            l10n_util::GetStringUTF16(IDS_MICROPHONE_BLOCKED),
                            0, &gfx::VectorIcon::EmptyIcon());
    }

    // Microphone & camera blocked per site
    {
      content_settings->OnMediaStreamPermissionSet(
          kTestOrigin, {PageSpecificContentSettings::kCameraAccessed,
                        PageSpecificContentSettings::kCameraBlocked,
                        PageSpecificContentSettings::kMicrophoneAccessed,
                        PageSpecificContentSettings::kMicrophoneBlocked});
      content_setting_image_model->Update(web_contents());
      ExpectImageModelState(
          *content_setting_image_model, /*is_visible=*/true,
          /*has_icon=*/true,
          l10n_util::GetStringUTF16(IDS_MICROPHONE_CAMERA_BLOCKED), 0,
          &gfx::VectorIcon::EmptyIcon());
    }
  }
}

}  // namespace