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

Side by Side Diff: chrome/browser/permissions/permission_manager_unittest.cc

Issue 2446863002: Revert of Add threadsafe version of PermissionManager::GetPermissionStatus (Closed)
Patch Set: Created 4 years, 1 month 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
« no previous file with comments | « chrome/browser/permissions/permission_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/permissions/permission_manager.h" 5 #include "chrome/browser/permissions/permission_manager.h"
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "build/build_config.h" 8 #include "build/build_config.h"
11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
12 #include "chrome/browser/permissions/permission_manager_factory.h" 10 #include "chrome/browser/permissions/permission_manager_factory.h"
13 #include "chrome/test/base/testing_profile.h" 11 #include "chrome/test/base/testing_profile.h"
14 #include "components/content_settings/core/browser/host_content_settings_map.h" 12 #include "components/content_settings/core/browser/host_content_settings_map.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/permission_type.h" 13 #include "content/public/browser/permission_type.h"
17 #include "content/public/test/test_browser_thread_bundle.h" 14 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
19 16
20 using blink::mojom::PermissionStatus; 17 using blink::mojom::PermissionStatus;
21 using content::PermissionType; 18 using content::PermissionType;
22 19
23 namespace { 20 namespace {
24 21
25 class PermissionManagerTestingProfile final : public TestingProfile { 22 class PermissionManagerTestingProfile final : public TestingProfile {
(...skipping 10 matching lines...) Expand all
36 33
37 } // anonymous namespace 34 } // anonymous namespace
38 35
39 class PermissionManagerTest : public testing::Test { 36 class PermissionManagerTest : public testing::Test {
40 public: 37 public:
41 void OnPermissionChange(PermissionStatus permission) { 38 void OnPermissionChange(PermissionStatus permission) {
42 callback_called_ = true; 39 callback_called_ = true;
43 callback_result_ = permission; 40 callback_result_ = permission;
44 } 41 }
45 42
46 void TestGetPermissionStatus();
47
48 protected: 43 protected:
49 PermissionManagerTest() 44 PermissionManagerTest()
50 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), 45 : url_("https://example.com"),
51 url_("https://example.com"),
52 other_url_("https://foo.com"), 46 other_url_("https://foo.com"),
53 callback_called_(false), 47 callback_called_(false),
54 callback_result_(PermissionStatus::ASK) { 48 callback_result_(PermissionStatus::ASK) {}
55 host_content_settings_map_ =
56 HostContentSettingsMapFactory::GetForProfile(&profile_);
57 }
58 49
59 PermissionManager* GetPermissionManager() { 50 PermissionManager* GetPermissionManager() {
60 return profile_.GetPermissionManager(); 51 return profile_.GetPermissionManager();
61 } 52 }
62 53
63 HostContentSettingsMap* GetHostContentSettingsMap() { 54 HostContentSettingsMap* GetHostContentSettingsMap() {
64 return host_content_settings_map_; 55 return HostContentSettingsMapFactory::GetForProfile(&profile_);
65 } 56 }
66 57
67 void CheckPermissionStatus(PermissionType type, 58 void CheckPermissionStatus(PermissionType type,
68 PermissionStatus expected) { 59 PermissionStatus expected) {
69 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { 60 EXPECT_EQ(expected, GetPermissionManager()->GetPermissionStatus(
70 EXPECT_EQ(expected, GetPermissionManager()->GetPermissionStatus( 61 type, url_.GetOrigin(), url_.GetOrigin()));
71 type, url_.GetOrigin(), url_.GetOrigin()));
72 } else {
73 EXPECT_EQ(expected, GetPermissionManager()->GetPermissionStatus(
74 host_content_settings_map_, type,
75 url_.GetOrigin(), url_.GetOrigin()));
76 }
77 } 62 }
78 63
79 void SetPermission(ContentSettingsType type, ContentSetting value) { 64 void SetPermission(ContentSettingsType type, ContentSetting value) {
80 GetHostContentSettingsMap()->SetContentSettingDefaultScope( 65 HostContentSettingsMapFactory::GetForProfile(&profile_)
81 url_, url_, type, std::string(), value); 66 ->SetContentSettingDefaultScope(url_, url_, type, std::string(), value);
82 } 67 }
83 68
84 const GURL& url() const { 69 const GURL& url() const {
85 return url_; 70 return url_;
86 } 71 }
87 72
88 const GURL& other_url() const { 73 const GURL& other_url() const {
89 return other_url_; 74 return other_url_;
90 } 75 }
91 76
92 bool callback_called() const { 77 bool callback_called() const {
93 return callback_called_; 78 return callback_called_;
94 } 79 }
95 80
96 PermissionStatus callback_result() const { return callback_result_; } 81 PermissionStatus callback_result() const { return callback_result_; }
97 82
98 void Reset() { 83 void Reset() {
99 callback_called_ = false; 84 callback_called_ = false;
100 callback_result_ = PermissionStatus::ASK; 85 callback_result_ = PermissionStatus::ASK;
101 } 86 }
102 87
103 private: 88 private:
104 content::TestBrowserThreadBundle thread_bundle_;
105 const GURL url_; 89 const GURL url_;
106 const GURL other_url_; 90 const GURL other_url_;
107 bool callback_called_; 91 bool callback_called_;
108 PermissionStatus callback_result_; 92 PermissionStatus callback_result_;
93 content::TestBrowserThreadBundle thread_bundle_;
109 PermissionManagerTestingProfile profile_; 94 PermissionManagerTestingProfile profile_;
110 HostContentSettingsMap* host_content_settings_map_;
111 }; 95 };
112 96
113 void PermissionManagerTest::TestGetPermissionStatus() { 97 TEST_F(PermissionManagerTest, GetPermissionStatusDefault) {
114 CheckPermissionStatus(PermissionType::MIDI_SYSEX, PermissionStatus::ASK); 98 CheckPermissionStatus(PermissionType::MIDI_SYSEX, PermissionStatus::ASK);
115 CheckPermissionStatus(PermissionType::PUSH_MESSAGING, PermissionStatus::ASK); 99 CheckPermissionStatus(PermissionType::PUSH_MESSAGING, PermissionStatus::ASK);
116 CheckPermissionStatus(PermissionType::NOTIFICATIONS, PermissionStatus::ASK); 100 CheckPermissionStatus(PermissionType::NOTIFICATIONS, PermissionStatus::ASK);
117 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); 101 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK);
118 #if defined(OS_ANDROID) 102 #if defined(OS_ANDROID)
119 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER, 103 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER,
120 PermissionStatus::ASK); 104 PermissionStatus::ASK);
121 #endif 105 #endif
122 } 106 }
123 107
124 TEST_F(PermissionManagerTest, GetPermissionStatusDefault) {
125 TestGetPermissionStatus();
126 }
127
128 TEST_F(PermissionManagerTest, GetPermissionStatusNonMainThread) {
129 base::RunLoop run_loop;
130 content::BrowserThread::PostTaskAndReply(
131 content::BrowserThread::IO, FROM_HERE,
132 base::Bind(&PermissionManagerTest::TestGetPermissionStatus,
133 base::Unretained(this)),
134 run_loop.QuitClosure());
135 run_loop.Run();
136 }
137
138 TEST_F(PermissionManagerTest, GetPermissionStatusAfterSet) { 108 TEST_F(PermissionManagerTest, GetPermissionStatusAfterSet) {
139 SetPermission(CONTENT_SETTINGS_TYPE_GEOLOCATION, CONTENT_SETTING_ALLOW); 109 SetPermission(CONTENT_SETTINGS_TYPE_GEOLOCATION, CONTENT_SETTING_ALLOW);
140 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED); 110 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED);
141 111
142 SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); 112 SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW);
143 CheckPermissionStatus(PermissionType::NOTIFICATIONS, 113 CheckPermissionStatus(PermissionType::NOTIFICATIONS,
144 PermissionStatus::GRANTED); 114 PermissionStatus::GRANTED);
145 CheckPermissionStatus(PermissionType::PUSH_MESSAGING, 115 CheckPermissionStatus(PermissionType::PUSH_MESSAGING,
146 PermissionStatus::GRANTED); 116 PermissionStatus::GRANTED);
147 117
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); 309 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK);
340 GetHostContentSettingsMap()->SetContentSettingDefaultScope( 310 GetHostContentSettingsMap()->SetContentSettingDefaultScope(
341 url(), url(), CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), 311 url(), url(), CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(),
342 CONTENT_SETTING_ALLOW); 312 CONTENT_SETTING_ALLOW);
343 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED); 313 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED);
344 314
345 EXPECT_FALSE(callback_called()); 315 EXPECT_FALSE(callback_called());
346 316
347 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); 317 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id);
348 } 318 }
OLDNEW
« no previous file with comments | « chrome/browser/permissions/permission_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698