OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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/content_settings/permission_context_base.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
9 #include "chrome/browser/content_settings/permission_queue_controller.h" | |
10 #include "chrome/browser/content_settings/permission_request_id.h" | |
11 #include "chrome/browser/infobars/infobar_service.h" | |
12 #include "chrome/common/content_settings.h" | |
13 #include "chrome/common/content_settings_types.h" | |
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
15 #include "chrome/test/base/testing_profile.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 | |
Bernhard Bauer
2014/07/08 12:16:04
Nit: remove one empty line
Miguel Garcia
2014/07/08 15:26:38
Done.
| |
22 class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { | |
23 protected: | |
24 PermissionContextBaseTests() {} | |
25 virtual ~PermissionContextBaseTests() {} | |
26 | |
27 private: | |
28 // ChromeRenderViewHostTestHarness: | |
29 virtual void SetUp() OVERRIDE { | |
30 ChromeRenderViewHostTestHarness::SetUp(); | |
31 InfoBarService::CreateForWebContents(web_contents()); | |
32 } | |
33 | |
34 DISALLOW_COPY_AND_ASSIGN(PermissionContextBaseTests); | |
35 }; | |
36 | |
37 class TestContextBase : public PermissionContextBase { | |
38 public: | |
39 TestContextBase(Profile* profile, | |
40 const ContentSettingsType permission_type) | |
41 : PermissionContextBase(profile, permission_type), | |
42 permission_set_(false), | |
43 permission_granted_(false), | |
44 update_tab_context_(false) {} | |
45 | |
46 virtual ~TestContextBase() {} | |
47 | |
48 PermissionQueueController* GetInfoBarController() { | |
49 return GetQueueController(); | |
50 } | |
51 | |
52 bool PermissionGranted() { | |
Bernhard Bauer
2014/07/08 12:16:04
Nit: very simple accessors are usually in unix_hac
Miguel Garcia
2014/07/08 15:27:02
Done.
| |
53 return permission_granted_; | |
54 } | |
55 | |
56 bool PermissionSet() { | |
57 return permission_set_; | |
58 } | |
59 | |
60 bool TabContextUpdated() { | |
61 return update_tab_context_; | |
62 } | |
63 | |
64 void PermissionCheck(bool granted) { | |
65 permission_set_ = true; | |
66 permission_granted_ = granted; | |
67 } | |
68 | |
69 protected: | |
70 virtual void UpdateTabContext(const PermissionRequestID& id, | |
71 const GURL& requesting_origin, | |
72 bool allowed) { | |
73 update_tab_context_ = true; | |
Bernhard Bauer
2014/07/08 12:16:04
Nit: Name this |tab_context_updated_|?
Miguel Garcia
2014/07/08 15:26:38
Done.
| |
74 } | |
75 | |
76 private: | |
77 bool permission_set_; | |
78 bool permission_granted_; | |
79 bool update_tab_context_; | |
80 }; | |
81 | |
82 // Simulates clicking Accept. The permission should be granted and | |
83 // saved for future use. | |
84 TEST_F(PermissionContextBaseTests, TestAskAndGrant) { | |
85 TestContextBase sut(profile(), | |
Bernhard Bauer
2014/07/08 12:16:04
sut?
Miguel Garcia
2014/07/08 15:26:38
Subject Under Test, it is a common thing in other
| |
86 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); | |
87 GURL url("http://www.google.com"); | |
88 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | |
89 | |
90 const PermissionRequestID id( | |
91 web_contents()->GetRenderProcessHost()->GetID(), | |
92 web_contents()->GetRenderViewHost()->GetRoutingID(), | |
93 -1, GURL()); | |
94 sut.RequestPermission(web_contents(), | |
95 id, url, true, | |
96 base::Bind(&TestContextBase::PermissionCheck, | |
97 base::Unretained(&sut))); | |
98 | |
99 sut.GetInfoBarController()->OnPermissionSet(id, url, url, true, true); | |
100 EXPECT_TRUE(sut.PermissionSet()); | |
101 EXPECT_TRUE(sut.PermissionGranted()); | |
102 EXPECT_TRUE(sut.TabContextUpdated()); | |
103 | |
104 ContentSetting setting = | |
105 profile()->GetHostContentSettingsMap()->GetContentSetting( | |
106 url.GetOrigin(), url.GetOrigin(), | |
107 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, std::string()); | |
108 EXPECT_EQ(CONTENT_SETTING_ALLOW , setting); | |
109 }; | |
110 | |
111 | |
Bernhard Bauer
2014/07/08 12:16:04
Nit: Remove one empty line.
Miguel Garcia
2014/07/08 15:26:38
Done.
| |
112 // Simulates clicking Dismiss (X in the infobar. | |
113 // The permission should be denied but not saved for future use. | |
114 TEST_F(PermissionContextBaseTests, TestAskAndDismiss) { | |
115 TestContextBase sut(profile(), | |
116 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); | |
117 GURL url("http://www.google.es"); | |
118 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | |
119 | |
120 const PermissionRequestID id( | |
121 web_contents()->GetRenderProcessHost()->GetID(), | |
122 web_contents()->GetRenderViewHost()->GetRoutingID(), | |
123 -1, GURL()); | |
124 sut.RequestPermission(web_contents(), | |
125 id, url, true, | |
126 base::Bind(&TestContextBase::PermissionCheck, | |
127 base::Unretained(&sut))); | |
128 | |
129 sut.GetInfoBarController()->OnPermissionSet(id, url, url, false, false); | |
130 EXPECT_TRUE(sut.PermissionSet()); | |
131 EXPECT_FALSE(sut.PermissionGranted()); | |
132 EXPECT_TRUE(sut.TabContextUpdated()); | |
133 | |
134 ContentSetting setting = | |
135 profile()->GetHostContentSettingsMap()->GetContentSetting( | |
136 url.GetOrigin(), url.GetOrigin(), | |
137 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, std::string()); | |
138 EXPECT_EQ(CONTENT_SETTING_ASK , setting); | |
139 }; | |
OLD | NEW |