OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/notifications/desktop_notification_service.h" | 5 #include "chrome/browser/notifications/desktop_notification_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
11 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 11 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
13 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
14 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPresen
ter.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPresen
ter.h" |
17 | 17 |
18 using content::BrowserThread; | |
19 | |
20 namespace { | |
21 | |
22 // The HasPermission method of the DesktopNotificationService wants to be called | |
23 // on the IO thread. This class routes calls to the cache on the IO thread. | |
24 class ThreadProxy : public base::RefCountedThreadSafe<ThreadProxy> { | |
25 public: | |
26 ThreadProxy() | |
27 : io_event_(false, false), | |
28 ui_event_(false, false), | |
29 permission_(WebKit::WebNotificationPresenter::PermissionAllowed) { | |
30 // The current message loop was already initalized by the test superclass. | |
31 ui_thread_.reset( | |
32 new content::TestBrowserThread(BrowserThread::UI, | |
33 base::MessageLoop::current())); | |
34 | |
35 // Create IO thread, start its message loop. | |
36 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO)); | |
37 io_thread_->Start(); | |
38 | |
39 // Calling PauseIOThread() here isn't safe, because the runnable method | |
40 // could complete before the constructor is done, deleting |this|. | |
41 } | |
42 | |
43 // Call the HasPermission method of the DesktopNotificationService on the IO | |
44 // thread and returns the permission setting. | |
45 WebKit::WebNotificationPresenter::Permission ServiceHasPermission( | |
46 DesktopNotificationService* service, | |
47 const GURL& url) { | |
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
49 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
50 base::Bind(&ThreadProxy::ServiceHasPermissionIO, this, service, url)); | |
51 io_event_.Signal(); | |
52 ui_event_.Wait(); // Wait for IO thread to be done. | |
53 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
54 base::Bind(&ThreadProxy::PauseIOThreadIO, this)); | |
55 | |
56 return permission_; | |
57 } | |
58 | |
59 void PauseIOThread() { | |
60 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
61 base::Bind(&ThreadProxy::PauseIOThreadIO, this)); | |
62 } | |
63 | |
64 void DrainIOThread() { | |
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
66 io_event_.Signal(); | |
67 io_thread_->Stop(); | |
68 } | |
69 | |
70 private: | |
71 friend class base::RefCountedThreadSafe<ThreadProxy>; | |
72 ~ThreadProxy() { | |
73 DrainIOThread(); | |
74 } | |
75 | |
76 void PauseIOThreadIO() { | |
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
78 io_event_.Wait(); | |
79 } | |
80 | |
81 void ServiceHasPermissionIO(DesktopNotificationService* service, | |
82 const GURL& url) { | |
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
84 permission_ = service->HasPermission(url); | |
85 ui_event_.Signal(); | |
86 } | |
87 | |
88 base::WaitableEvent io_event_; | |
89 base::WaitableEvent ui_event_; | |
90 scoped_ptr<content::TestBrowserThread> ui_thread_; | |
91 scoped_ptr<content::TestBrowserThread> io_thread_; | |
92 | |
93 WebKit::WebNotificationPresenter::Permission permission_; | |
94 }; | |
95 | |
96 } // namespace | |
97 | |
98 class DesktopNotificationServiceTest : public ChromeRenderViewHostTestHarness { | 18 class DesktopNotificationServiceTest : public ChromeRenderViewHostTestHarness { |
99 public: | 19 protected: |
100 DesktopNotificationServiceTest() { | |
101 } | |
102 | |
103 virtual void SetUp() { | 20 virtual void SetUp() { |
104 ChromeRenderViewHostTestHarness::SetUp(); | 21 ChromeRenderViewHostTestHarness::SetUp(); |
105 proxy_ = new ThreadProxy; | |
106 proxy_->PauseIOThread(); | |
107 | 22 |
108 // Creates the destop notification service. | 23 // Creates the destop notification service. |
109 service_ = DesktopNotificationServiceFactory::GetForProfile(profile()); | 24 service_ = DesktopNotificationServiceFactory::GetForProfile(profile()); |
110 } | 25 } |
111 | 26 |
112 virtual void TearDown() { | |
113 // The io thread's waiting on the io_event_ might hold a ref to |proxy_|, | |
114 // preventing its destruction. Clear that ref. | |
115 proxy_->DrainIOThread(); | |
116 ChromeRenderViewHostTestHarness::TearDown(); | |
117 } | |
118 | |
119 DesktopNotificationService* service_; | 27 DesktopNotificationService* service_; |
120 scoped_refptr<ThreadProxy> proxy_; | |
121 }; | 28 }; |
122 | 29 |
123 TEST_F(DesktopNotificationServiceTest, SettingsForSchemes) { | 30 TEST_F(DesktopNotificationServiceTest, SettingsForSchemes) { |
124 GURL url("file:///html/test.html"); | 31 GURL url("file:///html/test.html"); |
125 | 32 |
126 EXPECT_EQ(CONTENT_SETTING_ASK, | 33 EXPECT_EQ(CONTENT_SETTING_ASK, |
127 service_->GetDefaultContentSetting(NULL)); | 34 service_->GetDefaultContentSetting(NULL)); |
128 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, | 35 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
129 proxy_->ServiceHasPermission(service_, url)); | 36 service_->HasPermission(url)); |
130 | 37 |
131 service_->GrantPermission(url); | 38 service_->GrantPermission(url); |
132 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, | 39 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
133 proxy_->ServiceHasPermission(service_, url)); | 40 service_->HasPermission(url)); |
134 | 41 |
135 service_->DenyPermission(url); | 42 service_->DenyPermission(url); |
136 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, | 43 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
137 proxy_->ServiceHasPermission(service_, url)); | 44 service_->HasPermission(url)); |
138 | 45 |
139 GURL https_url("https://testurl"); | 46 GURL https_url("https://testurl"); |
140 GURL http_url("http://testurl"); | 47 GURL http_url("http://testurl"); |
141 EXPECT_EQ(CONTENT_SETTING_ASK, | 48 EXPECT_EQ(CONTENT_SETTING_ASK, |
142 service_->GetDefaultContentSetting(NULL)); | 49 service_->GetDefaultContentSetting(NULL)); |
143 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, | 50 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
144 proxy_->ServiceHasPermission(service_, http_url)); | 51 service_->HasPermission(http_url)); |
145 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, | 52 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
146 proxy_->ServiceHasPermission(service_, https_url)); | 53 service_->HasPermission(https_url)); |
147 | 54 |
148 service_->GrantPermission(https_url); | 55 service_->GrantPermission(https_url); |
149 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, | 56 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
150 proxy_->ServiceHasPermission(service_, http_url)); | 57 service_->HasPermission(http_url)); |
151 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, | 58 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
152 proxy_->ServiceHasPermission(service_, https_url)); | 59 service_->HasPermission(https_url)); |
153 | 60 |
154 service_->DenyPermission(http_url); | 61 service_->DenyPermission(http_url); |
155 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, | 62 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
156 proxy_->ServiceHasPermission(service_, http_url)); | 63 service_->HasPermission(http_url)); |
157 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, | 64 EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
158 proxy_->ServiceHasPermission(service_, https_url)); | 65 service_->HasPermission(https_url)); |
159 } | 66 } |
160 | 67 |
161 TEST_F(DesktopNotificationServiceTest, GetNotificationsSettings) { | 68 TEST_F(DesktopNotificationServiceTest, GetNotificationsSettings) { |
162 service_->GrantPermission(GURL("http://allowed2.com")); | 69 service_->GrantPermission(GURL("http://allowed2.com")); |
163 service_->GrantPermission(GURL("http://allowed.com")); | 70 service_->GrantPermission(GURL("http://allowed.com")); |
164 service_->DenyPermission(GURL("http://denied2.com")); | 71 service_->DenyPermission(GURL("http://denied2.com")); |
165 service_->DenyPermission(GURL("http://denied.com")); | 72 service_->DenyPermission(GURL("http://denied.com")); |
166 | 73 |
167 ContentSettingsForOneType settings; | 74 ContentSettingsForOneType settings; |
168 service_->GetNotificationsSettings(&settings); | 75 service_->GetNotificationsSettings(&settings); |
(...skipping 18 matching lines...) Expand all Loading... |
187 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard( | 94 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard( |
188 GURL("http://denied2.com")), | 95 GURL("http://denied2.com")), |
189 settings[3].primary_pattern); | 96 settings[3].primary_pattern); |
190 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 97 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
191 settings[3].setting); | 98 settings[3].setting); |
192 EXPECT_EQ(ContentSettingsPattern::Wildcard(), | 99 EXPECT_EQ(ContentSettingsPattern::Wildcard(), |
193 settings[4].primary_pattern); | 100 settings[4].primary_pattern); |
194 EXPECT_EQ(CONTENT_SETTING_ASK, | 101 EXPECT_EQ(CONTENT_SETTING_ASK, |
195 settings[4].setting); | 102 settings[4].setting); |
196 } | 103 } |
OLD | NEW |