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/web_restrictions_client.h" | |
8 #include "content/public/test/test_browser_thread_bundle.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 | |
11 /* | |
12 * Tests of C++ WebRestrictionsClient. Note that these tests use a dummy | |
13 * version of the Java WebRestrictionsClient which gives predefined | |
14 * responses based on the authority and the URL. They will not work if | |
15 * built with the real version of the Java WebRestrictionsClient. | |
Bernhard Bauer
2016/02/19 13:46:26
This seems very fragile. How do you know that the
aberent
2016/02/19 19:40:09
Done.
| |
16 */ | |
17 | |
18 using web_restrictions::WebRestrictionsClient; | |
19 | |
20 namespace { | |
21 | |
22 bool callback_called; | |
Bernhard Bauer
2016/02/19 13:46:26
This one isn't really necessary -- the run loop wi
aberent
2016/02/19 19:40:08
Done.
| |
23 bool returned_result; | |
Bernhard Bauer
2016/02/19 13:46:26
|g_returned_result|?
aberent
2016/02/19 19:40:08
Done.
| |
24 | |
25 void ResultCallback(base::Closure quit_closure, bool result) { | |
Bernhard Bauer
2016/02/19 13:46:26
Pass the closure by const reference.
aberent
2016/02/19 19:40:08
Done.
| |
26 callback_called = true; | |
27 returned_result = result; | |
28 quit_closure.Run(); | |
29 } | |
30 } // namespace | |
Bernhard Bauer
2016/02/19 13:46:26
Nit: Empty line before this one.
aberent
2016/02/19 19:40:08
Done.
| |
31 | |
32 TEST(WebRestrictionsClientTest, TestShouldProceed) { | |
33 content::TestBrowserThreadBundle thread_bundle; | |
34 WebRestrictionsClient client; | |
35 client.SetAuthority("Good"); | |
36 // First call should go to Web Restrictions Content Provider, and return a | |
37 // delayed result. | |
38 callback_called = false; | |
39 returned_result = false; | |
40 base::RunLoop run_loop; | |
41 EXPECT_EQ(web_restrictions::PENDING, | |
42 client.ShouldProceed( | |
43 true, GURL("http://example.com"), | |
44 base::Bind(&ResultCallback, run_loop.QuitClosure()))); | |
45 run_loop.Run(); | |
46 EXPECT_TRUE(callback_called); | |
47 EXPECT_TRUE(returned_result); | |
48 // A repeated call should go to the cache and return a result immediately. | |
49 EXPECT_EQ(web_restrictions::ALLOW, | |
50 client.ShouldProceed( | |
51 true, GURL("http://example.com"), | |
52 base::Bind(&ResultCallback, run_loop.QuitClosure()))); | |
53 // However a different url should miss the cache | |
54 callback_called = false; | |
55 returned_result = false; | |
56 base::RunLoop run_loop2; | |
57 EXPECT_EQ(web_restrictions::PENDING, | |
58 client.ShouldProceed( | |
59 true, GURL("http://example.com/2"), | |
60 base::Bind(&ResultCallback, run_loop2.QuitClosure()))); | |
61 run_loop2.Run(); | |
62 EXPECT_TRUE(callback_called); | |
63 EXPECT_TRUE(returned_result); | |
64 // Switching the authority should clear the cache. | |
65 client.SetAuthority("Good2"); | |
66 callback_called = false; | |
67 returned_result = false; | |
68 base::RunLoop run_loop3; | |
69 EXPECT_EQ(web_restrictions::PENDING, | |
70 client.ShouldProceed( | |
71 true, GURL("http://example.com/2"), | |
72 base::Bind(&ResultCallback, run_loop3.QuitClosure()))); | |
73 run_loop3.Run(); | |
74 EXPECT_TRUE(callback_called); | |
75 EXPECT_TRUE(returned_result); | |
76 // Try getting a bad result | |
77 client.SetAuthority("Bad"); | |
78 callback_called = false; | |
79 returned_result = true; | |
80 base::RunLoop run_loop4; | |
81 EXPECT_EQ(web_restrictions::PENDING, | |
82 client.ShouldProceed( | |
83 true, GURL("http://example.com/2"), | |
84 base::Bind(&ResultCallback, run_loop4.QuitClosure()))); | |
85 run_loop4.Run(); | |
86 EXPECT_TRUE(callback_called); | |
87 EXPECT_FALSE(returned_result); | |
88 std::string error_html; | |
89 EXPECT_TRUE(client.GetErrorHtml(GURL("http://example.com/2"), &error_html)); | |
90 EXPECT_EQ("http://example.com/2", error_html); | |
91 } | |
92 | |
93 TEST(WebRestrictionsClientTest, TestRequestPermission) { | |
94 content::TestBrowserThreadBundle thread_bundle; | |
95 WebRestrictionsClient client; | |
96 client.SetAuthority("Good"); | |
97 base::RunLoop run_loop; | |
98 callback_called = false; | |
99 returned_result = false; | |
100 client.RequestPermission(GURL("http://example.com"), | |
101 base::Bind(&ResultCallback, run_loop.QuitClosure())); | |
102 run_loop.Run(); | |
103 EXPECT_TRUE(callback_called); | |
104 EXPECT_TRUE(returned_result); | |
105 client.SetAuthority("Bad"); | |
106 base::RunLoop run_loop2; | |
107 callback_called = false; | |
108 returned_result = true; | |
109 client.RequestPermission( | |
110 GURL("http://example.com"), | |
111 base::Bind(&ResultCallback, run_loop2.QuitClosure())); | |
112 run_loop2.Run(); | |
113 EXPECT_TRUE(callback_called); | |
114 EXPECT_FALSE(returned_result); | |
115 } | |
OLD | NEW |