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

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

Issue 2439673004: Add threadsafe version of PermissionManager::GetPermissionStatus (Closed)
Patch Set: Use thread_bundle_ REAL_IO_THREAD to ensure multi-thread testing 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"
8 #include "build/build_config.h" 10 #include "build/build_config.h"
9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
10 #include "chrome/browser/permissions/permission_manager_factory.h" 12 #include "chrome/browser/permissions/permission_manager_factory.h"
11 #include "chrome/test/base/testing_profile.h" 13 #include "chrome/test/base/testing_profile.h"
12 #include "components/content_settings/core/browser/host_content_settings_map.h" 14 #include "components/content_settings/core/browser/host_content_settings_map.h"
15 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/permission_type.h" 16 #include "content/public/browser/permission_type.h"
14 #include "content/public/test/test_browser_thread_bundle.h" 17 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
16 19
17 using blink::mojom::PermissionStatus; 20 using blink::mojom::PermissionStatus;
18 using content::PermissionType; 21 using content::PermissionType;
19 22
20 namespace { 23 namespace {
21 24
22 class PermissionManagerTestingProfile final : public TestingProfile { 25 class PermissionManagerTestingProfile final : public TestingProfile {
(...skipping 10 matching lines...) Expand all
33 36
34 } // anonymous namespace 37 } // anonymous namespace
35 38
36 class PermissionManagerTest : public testing::Test { 39 class PermissionManagerTest : public testing::Test {
37 public: 40 public:
38 void OnPermissionChange(PermissionStatus permission) { 41 void OnPermissionChange(PermissionStatus permission) {
39 callback_called_ = true; 42 callback_called_ = true;
40 callback_result_ = permission; 43 callback_result_ = permission;
41 } 44 }
42 45
46 void TestGetPermissionStatus();
47
43 protected: 48 protected:
44 PermissionManagerTest() 49 PermissionManagerTest()
45 : url_("https://example.com"), 50 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD),
51 url_("https://example.com"),
46 other_url_("https://foo.com"), 52 other_url_("https://foo.com"),
47 callback_called_(false), 53 callback_called_(false),
48 callback_result_(PermissionStatus::ASK) {} 54 callback_result_(PermissionStatus::ASK) {
55 host_content_settings_map_ =
56 HostContentSettingsMapFactory::GetForProfile(&profile_);
57 }
49 58
50 PermissionManager* GetPermissionManager() { 59 PermissionManager* GetPermissionManager() {
51 return profile_.GetPermissionManager(); 60 return profile_.GetPermissionManager();
52 } 61 }
53 62
54 HostContentSettingsMap* GetHostContentSettingsMap() { 63 HostContentSettingsMap* GetHostContentSettingsMap() {
55 return HostContentSettingsMapFactory::GetForProfile(&profile_); 64 return host_content_settings_map_;
56 } 65 }
57 66
58 void CheckPermissionStatus(PermissionType type, 67 void CheckPermissionStatus(PermissionType type,
59 PermissionStatus expected) { 68 PermissionStatus expected) {
60 EXPECT_EQ(expected, GetPermissionManager()->GetPermissionStatus( 69 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
61 type, url_.GetOrigin(), url_.GetOrigin())); 70 EXPECT_EQ(expected, GetPermissionManager()->GetPermissionStatus(
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 }
62 } 77 }
63 78
64 void SetPermission(ContentSettingsType type, ContentSetting value) { 79 void SetPermission(ContentSettingsType type, ContentSetting value) {
65 HostContentSettingsMapFactory::GetForProfile(&profile_) 80 GetHostContentSettingsMap()->SetContentSettingDefaultScope(
66 ->SetContentSettingDefaultScope(url_, url_, type, std::string(), value); 81 url_, url_, type, std::string(), value);
67 } 82 }
68 83
69 const GURL& url() const { 84 const GURL& url() const {
70 return url_; 85 return url_;
71 } 86 }
72 87
73 const GURL& other_url() const { 88 const GURL& other_url() const {
74 return other_url_; 89 return other_url_;
75 } 90 }
76 91
77 bool callback_called() const { 92 bool callback_called() const {
78 return callback_called_; 93 return callback_called_;
79 } 94 }
80 95
81 PermissionStatus callback_result() const { return callback_result_; } 96 PermissionStatus callback_result() const { return callback_result_; }
82 97
83 void Reset() { 98 void Reset() {
84 callback_called_ = false; 99 callback_called_ = false;
85 callback_result_ = PermissionStatus::ASK; 100 callback_result_ = PermissionStatus::ASK;
86 } 101 }
87 102
88 private: 103 private:
104 content::TestBrowserThreadBundle thread_bundle_;
89 const GURL url_; 105 const GURL url_;
90 const GURL other_url_; 106 const GURL other_url_;
91 bool callback_called_; 107 bool callback_called_;
92 PermissionStatus callback_result_; 108 PermissionStatus callback_result_;
93 content::TestBrowserThreadBundle thread_bundle_;
94 PermissionManagerTestingProfile profile_; 109 PermissionManagerTestingProfile profile_;
110 HostContentSettingsMap* host_content_settings_map_;
95 }; 111 };
96 112
97 TEST_F(PermissionManagerTest, GetPermissionStatusDefault) { 113 void PermissionManagerTest::TestGetPermissionStatus() {
98 CheckPermissionStatus(PermissionType::MIDI_SYSEX, PermissionStatus::ASK); 114 CheckPermissionStatus(PermissionType::MIDI_SYSEX, PermissionStatus::ASK);
99 CheckPermissionStatus(PermissionType::PUSH_MESSAGING, PermissionStatus::ASK); 115 CheckPermissionStatus(PermissionType::PUSH_MESSAGING, PermissionStatus::ASK);
100 CheckPermissionStatus(PermissionType::NOTIFICATIONS, PermissionStatus::ASK); 116 CheckPermissionStatus(PermissionType::NOTIFICATIONS, PermissionStatus::ASK);
101 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); 117 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK);
102 #if defined(OS_ANDROID) 118 #if defined(OS_ANDROID)
103 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER, 119 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER,
104 PermissionStatus::ASK); 120 PermissionStatus::ASK);
105 #endif 121 #endif
106 } 122 }
107 123
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();
mlamouri (slow - plz ping) 2016/10/24 10:40:51 Looks fine but I would have done this a bit differ
136 }
137
108 TEST_F(PermissionManagerTest, GetPermissionStatusAfterSet) { 138 TEST_F(PermissionManagerTest, GetPermissionStatusAfterSet) {
109 SetPermission(CONTENT_SETTINGS_TYPE_GEOLOCATION, CONTENT_SETTING_ALLOW); 139 SetPermission(CONTENT_SETTINGS_TYPE_GEOLOCATION, CONTENT_SETTING_ALLOW);
110 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED); 140 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED);
111 141
112 SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); 142 SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW);
113 CheckPermissionStatus(PermissionType::NOTIFICATIONS, 143 CheckPermissionStatus(PermissionType::NOTIFICATIONS,
114 PermissionStatus::GRANTED); 144 PermissionStatus::GRANTED);
115 CheckPermissionStatus(PermissionType::PUSH_MESSAGING, 145 CheckPermissionStatus(PermissionType::PUSH_MESSAGING,
116 PermissionStatus::GRANTED); 146 PermissionStatus::GRANTED);
117 147
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); 339 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK);
310 GetHostContentSettingsMap()->SetContentSettingDefaultScope( 340 GetHostContentSettingsMap()->SetContentSettingDefaultScope(
311 url(), url(), CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), 341 url(), url(), CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(),
312 CONTENT_SETTING_ALLOW); 342 CONTENT_SETTING_ALLOW);
313 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED); 343 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED);
314 344
315 EXPECT_FALSE(callback_called()); 345 EXPECT_FALSE(callback_called());
316 346
317 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); 347 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id);
318 } 348 }
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