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

Side by Side Diff: chrome/browser/payments/payment_handler_permission_context_unittest.cc

Issue 2946013002: PaymentHandler: Implement requestPermission().
Patch Set: PaymentHandler: Implement requestPermission(). 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 2017 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/payments/payment_handler_permission_context.h"
6 #include "base/bind.h"
7 #include "base/macros.h"
8 #include "build/build_config.h"
9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
10 #include "chrome/browser/permissions/permission_request_id.h"
11 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "components/content_settings/core/browser/host_content_settings_map.h"
14 #include "components/content_settings/core/common/content_settings.h"
15 #include "components/content_settings/core/common/content_settings_types.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/test/mock_render_process_host.h"
18 #include "content/public/test/web_contents_tester.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 #if defined(OS_ANDROID)
22 #include "chrome/browser/infobars/infobar_service.h"
23 #else
24 #include "chrome/browser/permissions/permission_request_manager.h"
25 #endif
26
27 namespace {
28
29 class TestPermissionContext : public PaymentHandlerPermissionContext {
30 public:
31 explicit TestPermissionContext(Profile* profile)
32 : PaymentHandlerPermissionContext(profile),
33 permission_set_(false),
34 permission_granted_(false) {}
35
36 ~TestPermissionContext() override {}
37
38 bool permission_granted() { return permission_granted_; }
39
40 bool permission_set() { return permission_set_; }
41
42 void TrackPermissionDecision(ContentSetting content_setting) {
43 permission_set_ = true;
44 permission_granted_ = content_setting == CONTENT_SETTING_ALLOW;
45 }
46
47 private:
48 bool permission_set_;
49 bool permission_granted_;
50 };
51
52 } // anonymous namespace
53
54 class PaymentHandlerPermissionContextTests
55 : public ChromeRenderViewHostTestHarness {
56 protected:
57 PaymentHandlerPermissionContextTests() = default;
58
59 private:
60 // ChromeRenderViewHostTestHarness:
61 void SetUp() override {
62 ChromeRenderViewHostTestHarness::SetUp();
63 #if defined(OS_ANDROID)
64 InfoBarService::CreateForWebContents(web_contents());
65 #else
66 PermissionRequestManager::CreateForWebContents(web_contents());
67 #endif
68 }
69
70 DISALLOW_COPY_AND_ASSIGN(PaymentHandlerPermissionContextTests);
71 };
72
73 // PaymentHandler permission should be denied for insecure origin.
74 TEST_F(PaymentHandlerPermissionContextTests, TestInsecureRequestingUrl) {
75 TestPermissionContext permission_context(profile());
76 GURL url("http://www.example.com");
77 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url);
78
79 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(),
80 web_contents()->GetMainFrame()->GetRoutingID(),
81 -1);
82 permission_context.RequestPermission(
83 web_contents(), id, url, true,
84 base::Bind(&TestPermissionContext::TrackPermissionDecision,
85 base::Unretained(&permission_context)));
86
87 EXPECT_TRUE(permission_context.permission_set());
88 EXPECT_FALSE(permission_context.permission_granted());
89
90 ContentSetting setting =
91 HostContentSettingsMapFactory::GetForProfile(profile())
92 ->GetContentSetting(url.GetOrigin(), url.GetOrigin(),
93 CONTENT_SETTINGS_TYPE_PAYMENT_HANDLER,
94 std::string());
95 EXPECT_EQ(CONTENT_SETTING_ASK, setting);
96 }
97
98 // PaymentHandler permission status should be denied for insecure origin.
99 TEST_F(PaymentHandlerPermissionContextTests, TestInsecureQueryingUrl) {
100 TestPermissionContext permission_context(profile());
101 GURL insecure_url("http://www.example.com");
102 GURL secure_url("https://www.example.com");
103
104 // Check that there is no saved content settings.
105 EXPECT_EQ(CONTENT_SETTING_ASK,
106 HostContentSettingsMapFactory::GetForProfile(profile())
107 ->GetContentSetting(
108 insecure_url.GetOrigin(), insecure_url.GetOrigin(),
109 CONTENT_SETTINGS_TYPE_PAYMENT_HANDLER, std::string()));
110 EXPECT_EQ(CONTENT_SETTING_ASK,
111 HostContentSettingsMapFactory::GetForProfile(profile())
112 ->GetContentSetting(
113 secure_url.GetOrigin(), insecure_url.GetOrigin(),
114 CONTENT_SETTINGS_TYPE_PAYMENT_HANDLER, std::string()));
115 EXPECT_EQ(CONTENT_SETTING_ASK,
116 HostContentSettingsMapFactory::GetForProfile(profile())
117 ->GetContentSetting(
118 insecure_url.GetOrigin(), secure_url.GetOrigin(),
119 CONTENT_SETTINGS_TYPE_PAYMENT_HANDLER, std::string()));
120
121 EXPECT_EQ(CONTENT_SETTING_BLOCK,
122 permission_context
123 .GetPermissionStatus(nullptr /* render_frame_host */,
124 insecure_url, insecure_url)
125 .content_setting);
126
127 EXPECT_EQ(CONTENT_SETTING_BLOCK,
128 permission_context
129 .GetPermissionStatus(nullptr /* render_frame_host */,
130 secure_url, insecure_url)
131 .content_setting);
132
133 EXPECT_EQ(CONTENT_SETTING_BLOCK,
134 permission_context
135 .GetPermissionStatus(nullptr /* render_frame_host */,
136 insecure_url, secure_url)
137 .content_setting);
138 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698