Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(812)

Side by Side Diff: chrome/browser/media/midi_permission_context_unittest.cc

Issue 2897833002: Add a PermissionContext subclass for the midi permission (Closed)
Patch Set: Midi sysex Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/media/midi_permission_context.h"
6
7 #include "base/bind.h"
8 #include "base/macros.h"
9 #include "build/build_config.h"
10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
11 #include "chrome/browser/permissions/permission_request_id.h"
12 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "components/content_settings/core/browser/host_content_settings_map.h"
15 #include "components/content_settings/core/common/content_settings.h"
16 #include "components/content_settings/core/common/content_settings_types.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/mock_render_process_host.h"
19 #include "content/public/test/web_contents_tester.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 #if defined(OS_ANDROID)
23 #include "chrome/browser/infobars/infobar_service.h"
24 #else
25 #include "chrome/browser/permissions/permission_request_manager.h"
26 #endif
27
28 namespace {
29
30 class TestPermissionContext : public MidiPermissionContext {
31 public:
32 explicit TestPermissionContext(Profile* profile)
33 : MidiPermissionContext(profile),
34 permission_set_(false),
35 permission_granted_(false),
36 tab_context_updated_(false) {}
37
38 ~TestPermissionContext() override {}
39
40 bool permission_granted() {
41 return permission_granted_;
42 }
43
44 bool permission_set() {
45 return permission_set_;
46 }
47
48 bool tab_context_updated() {
49 return tab_context_updated_;
50 }
51
52 void TrackPermissionDecision(ContentSetting content_setting) {
53 permission_set_ = true;
54 permission_granted_ = content_setting == CONTENT_SETTING_ALLOW;
55 }
56
57 protected:
58 void UpdateTabContext(const PermissionRequestID& id,
59 const GURL& requesting_origin,
60 bool allowed) override {
61 tab_context_updated_ = true;
62 }
63
64 private:
65 bool permission_set_;
66 bool permission_granted_;
67 bool tab_context_updated_;
68 };
69
70 } // anonymous namespace
71
72 class MidiPermissionContextTests : public ChromeRenderViewHostTestHarness {
73 protected:
74 MidiPermissionContextTests() = default;
75
76 private:
77 // ChromeRenderViewHostTestHarness:
78 void SetUp() override {
79 ChromeRenderViewHostTestHarness::SetUp();
80 #if defined(OS_ANDROID)
81 InfoBarService::CreateForWebContents(web_contents());
82 #else
83 PermissionRequestManager::CreateForWebContents(web_contents());
84 #endif
85 }
86
87 DISALLOW_COPY_AND_ASSIGN(MidiPermissionContextTests);
88 };
89
90 // Web MIDI permission should be denied for insecure origin.
91 TEST_F(MidiPermissionContextTests, TestInsecureRequestingUrl) {
92 TestPermissionContext permission_context(profile());
93 GURL url("http://www.example.com");
94 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url);
95
96 const PermissionRequestID id(
97 web_contents()->GetRenderProcessHost()->GetID(),
98 web_contents()->GetMainFrame()->GetRoutingID(),
99 -1);
100 permission_context.RequestPermission(
101 web_contents(),
102 id, url, true,
103 base::Bind(&TestPermissionContext::TrackPermissionDecision,
104 base::Unretained(&permission_context)));
105
106 EXPECT_TRUE(permission_context.permission_set());
107 EXPECT_FALSE(permission_context.permission_granted());
108 EXPECT_TRUE(permission_context.tab_context_updated());
109
110 ContentSetting setting =
111 HostContentSettingsMapFactory::GetForProfile(profile())
112 ->GetContentSetting(url.GetOrigin(),
113 url.GetOrigin(),
114 CONTENT_SETTINGS_TYPE_MIDI_SYSEX,
115 std::string());
116 EXPECT_EQ(CONTENT_SETTING_ASK, setting);
117 }
118
119 // Web MIDI permission status should be denied for insecure origin.
120 TEST_F(MidiPermissionContextTests, TestInsecureQueryingUrl) {
121 TestPermissionContext permission_context(profile());
122 GURL insecure_url("http://www.example.com");
123 GURL secure_url("https://www.example.com");
124
125 // Check that there is no saved content settings.
126 EXPECT_EQ(CONTENT_SETTING_ASK,
127 HostContentSettingsMapFactory::GetForProfile(profile())
128 ->GetContentSetting(insecure_url.GetOrigin(),
129 insecure_url.GetOrigin(),
130 CONTENT_SETTINGS_TYPE_MIDI_SYSEX,
131 std::string()));
132 EXPECT_EQ(CONTENT_SETTING_ASK,
133 HostContentSettingsMapFactory::GetForProfile(profile())
134 ->GetContentSetting(secure_url.GetOrigin(),
135 insecure_url.GetOrigin(),
136 CONTENT_SETTINGS_TYPE_MIDI_SYSEX,
137 std::string()));
138 EXPECT_EQ(CONTENT_SETTING_ASK,
139 HostContentSettingsMapFactory::GetForProfile(profile())
140 ->GetContentSetting(insecure_url.GetOrigin(),
141 secure_url.GetOrigin(),
142 CONTENT_SETTINGS_TYPE_MIDI_SYSEX,
143 std::string()));
144
145 EXPECT_EQ(CONTENT_SETTING_BLOCK,
146 permission_context
147 .GetPermissionStatus(nullptr /* render_frame_host */,
148 insecure_url, insecure_url)
149 .content_setting);
150
151 EXPECT_EQ(CONTENT_SETTING_BLOCK,
152 permission_context
153 .GetPermissionStatus(nullptr /* render_frame_host */,
154 insecure_url, secure_url)
155 .content_setting);
156 }
OLDNEW
« no previous file with comments | « chrome/browser/media/midi_permission_context.cc ('k') | chrome/browser/media/midi_sysex_permission_context.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698