OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "base/bind.h" | |
6 #include "base/run_loop.h" | |
7 #include "components/web_restrictions/browser/mock_web_restrictions_client.h" | |
8 #include "components/web_restrictions/browser/web_restrictions_client.h" | |
9 #include "content/public/test/test_browser_thread_bundle.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 using web_restrictions::WebRestrictionsClient; | |
13 using web_restrictions::MockWebRestrictionsClient; | |
14 | |
15 namespace { | |
16 | |
17 bool g_returned_result; | |
18 | |
19 void ResultCallback(const base::Closure& quit_closure, bool result) { | |
20 g_returned_result = result; | |
21 quit_closure.Run(); | |
22 } | |
23 | |
24 } // namespace | |
25 | |
26 TEST(WebRestrictionsClientTest, ShouldProceed) { | |
27 // Mock the Java WebRestrictionsClient. The real version | |
28 // would need a content provider to do anything. | |
29 MockWebRestrictionsClient mock; | |
30 content::TestBrowserThreadBundle thread_bundle; | |
31 WebRestrictionsClient client; | |
Bernhard Bauer
2016/02/22 12:39:17
You could also put these into a test fixture.
aberent
2016/02/23 13:52:37
Done.
| |
32 client.SetAuthority("Good"); | |
33 // First call should go to Web Restrictions Content Provider, and return a | |
34 // delayed result. | |
35 g_returned_result = false; | |
36 base::RunLoop run_loop; | |
37 EXPECT_EQ(web_restrictions::PENDING, | |
38 client.ShouldProceed( | |
39 true, GURL("http://example.com"), | |
40 base::Bind(&ResultCallback, run_loop.QuitClosure()))); | |
41 run_loop.Run(); | |
42 EXPECT_TRUE(g_returned_result); | |
43 // A repeated call should go to the cache and return a result immediately. | |
44 EXPECT_EQ(web_restrictions::ALLOW, | |
45 client.ShouldProceed( | |
46 true, GURL("http://example.com"), | |
47 base::Bind(&ResultCallback, run_loop.QuitClosure()))); | |
48 // However a different url should miss the cache | |
49 g_returned_result = false; | |
50 base::RunLoop run_loop2; | |
51 EXPECT_EQ(web_restrictions::PENDING, | |
52 client.ShouldProceed( | |
53 true, GURL("http://example.com/2"), | |
54 base::Bind(&ResultCallback, run_loop2.QuitClosure()))); | |
55 run_loop2.Run(); | |
56 EXPECT_TRUE(g_returned_result); | |
57 // Switching the authority should clear the cache. | |
58 client.SetAuthority("Good2"); | |
59 g_returned_result = false; | |
60 base::RunLoop run_loop3; | |
61 EXPECT_EQ(web_restrictions::PENDING, | |
62 client.ShouldProceed( | |
63 true, GURL("http://example.com/2"), | |
64 base::Bind(&ResultCallback, run_loop3.QuitClosure()))); | |
65 run_loop3.Run(); | |
66 EXPECT_TRUE(g_returned_result); | |
67 // Try getting a bad result | |
68 client.SetAuthority("Bad"); | |
69 g_returned_result = true; | |
70 base::RunLoop run_loop4; | |
71 EXPECT_EQ(web_restrictions::PENDING, | |
72 client.ShouldProceed( | |
73 true, GURL("http://example.com/2"), | |
74 base::Bind(&ResultCallback, run_loop4.QuitClosure()))); | |
75 run_loop4.Run(); | |
76 EXPECT_FALSE(g_returned_result); | |
77 std::string error_html; | |
78 EXPECT_TRUE(client.GetErrorHtml(GURL("http://example.com/2"), &error_html)); | |
79 EXPECT_EQ("http://example.com/2", error_html); | |
80 } | |
81 | |
82 TEST(WebRestrictionsClientTest, RequestPermission) { | |
83 // Mock the Java WebRestrictionsClient. The real version | |
84 // would need a content provider to do anything. | |
85 MockWebRestrictionsClient mock; | |
86 content::TestBrowserThreadBundle thread_bundle; | |
87 WebRestrictionsClient client; | |
88 client.SetAuthority("Good"); | |
89 base::RunLoop run_loop; | |
90 g_returned_result = false; | |
91 client.RequestPermission(GURL("http://example.com"), | |
92 base::Bind(&ResultCallback, run_loop.QuitClosure())); | |
93 run_loop.Run(); | |
94 EXPECT_TRUE(g_returned_result); | |
95 client.SetAuthority("Bad"); | |
96 base::RunLoop run_loop2; | |
97 g_returned_result = true; | |
98 client.RequestPermission( | |
99 GURL("http://example.com"), | |
100 base::Bind(&ResultCallback, run_loop2.QuitClosure())); | |
101 run_loop2.Run(); | |
102 EXPECT_FALSE(g_returned_result); | |
103 } | |
OLD | NEW |