Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / media / midi_permission_context_unittest.cc
blob3256130e5986e7c2b30c40529eb665400e966a83
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/media/midi_permission_context.h"
7 #include "base/bind.h"
8 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
9 #include "chrome/browser/infobars/infobar_service.h"
10 #include "chrome/browser/permissions/permission_queue_controller.h"
11 #include "chrome/browser/permissions/permission_request_id.h"
12 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "components/content_settings/core/browser/host_content_settings_map.h"
16 #include "components/content_settings/core/common/content_settings.h"
17 #include "components/content_settings/core/common/content_settings_types.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/test/mock_render_process_host.h"
20 #include "content/public/test/web_contents_tester.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace {
25 class TestPermissionContext : public MidiPermissionContext {
26 public:
27 explicit TestPermissionContext(Profile* profile)
28 : MidiPermissionContext(profile),
29 permission_set_(false),
30 permission_granted_(false),
31 tab_context_updated_(false) {}
33 ~TestPermissionContext() override {}
35 PermissionQueueController* GetInfoBarController() {
36 return GetQueueController();
39 bool permission_granted() {
40 return permission_granted_;
43 bool permission_set() {
44 return permission_set_;
47 bool tab_context_updated() {
48 return tab_context_updated_;
51 void TrackPermissionDecision(ContentSetting content_setting) {
52 permission_set_ = true;
53 permission_granted_ = content_setting == CONTENT_SETTING_ALLOW;
56 protected:
57 void UpdateTabContext(const PermissionRequestID& id,
58 const GURL& requesting_origin,
59 bool allowed) override {
60 tab_context_updated_ = true;
63 private:
64 bool permission_set_;
65 bool permission_granted_;
66 bool tab_context_updated_;
69 } // anonymous namespace
71 class MidiPermissionContextTests : public ChromeRenderViewHostTestHarness {
72 protected:
73 MidiPermissionContextTests() = default;
75 private:
76 // ChromeRenderViewHostTestHarness:
77 void SetUp() override {
78 ChromeRenderViewHostTestHarness::SetUp();
79 InfoBarService::CreateForWebContents(web_contents());
80 PermissionBubbleManager::CreateForWebContents(web_contents());
83 DISALLOW_COPY_AND_ASSIGN(MidiPermissionContextTests);
86 // Web MIDI permission should be denied for insecure origin.
87 TEST_F(MidiPermissionContextTests, TestInsecureRequestingUrl) {
88 TestPermissionContext permission_context(profile());
89 GURL url("http://www.example.com");
90 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url);
92 const PermissionRequestID id(
93 web_contents()->GetRenderProcessHost()->GetID(),
94 web_contents()->GetMainFrame()->GetRoutingID(),
95 -1);
96 permission_context.RequestPermission(
97 web_contents(),
98 id, url, true,
99 base::Bind(&TestPermissionContext::TrackPermissionDecision,
100 base::Unretained(&permission_context)));
102 EXPECT_TRUE(permission_context.permission_set());
103 EXPECT_FALSE(permission_context.permission_granted());
104 EXPECT_TRUE(permission_context.tab_context_updated());
106 ContentSetting setting =
107 HostContentSettingsMapFactory::GetForProfile(profile())
108 ->GetContentSetting(url.GetOrigin(),
109 url.GetOrigin(),
110 CONTENT_SETTINGS_TYPE_MIDI_SYSEX,
111 std::string());
112 EXPECT_EQ(CONTENT_SETTING_ASK, setting);
115 // Web MIDI permission status should be denied for insecure origin.
116 TEST_F(MidiPermissionContextTests, TestInsecureQueryingUrl) {
117 TestPermissionContext permission_context(profile());
118 GURL insecure_url("http://www.example.com");
119 GURL secure_url("https://www.example.com");
121 // Check that there is no saved content settings.
122 EXPECT_EQ(CONTENT_SETTING_ASK,
123 HostContentSettingsMapFactory::GetForProfile(profile())
124 ->GetContentSetting(insecure_url.GetOrigin(),
125 insecure_url.GetOrigin(),
126 CONTENT_SETTINGS_TYPE_MIDI_SYSEX,
127 std::string()));
128 EXPECT_EQ(CONTENT_SETTING_ASK,
129 HostContentSettingsMapFactory::GetForProfile(profile())
130 ->GetContentSetting(secure_url.GetOrigin(),
131 insecure_url.GetOrigin(),
132 CONTENT_SETTINGS_TYPE_MIDI_SYSEX,
133 std::string()));
134 EXPECT_EQ(CONTENT_SETTING_ASK,
135 HostContentSettingsMapFactory::GetForProfile(profile())
136 ->GetContentSetting(insecure_url.GetOrigin(),
137 secure_url.GetOrigin(),
138 CONTENT_SETTINGS_TYPE_MIDI_SYSEX,
139 std::string()));
141 EXPECT_EQ(CONTENT_SETTING_BLOCK, permission_context.GetPermissionStatus(
142 insecure_url, insecure_url));
143 EXPECT_EQ(CONTENT_SETTING_BLOCK, permission_context.GetPermissionStatus(
144 insecure_url, secure_url));