OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 <memory> |
| 6 |
5 #include "base/bind.h" | 7 #include "base/bind.h" |
6 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
7 #include "components/web_restrictions/browser/mock_web_restrictions_client.h" | 9 #include "components/web_restrictions/browser/mock_web_restrictions_client.h" |
8 #include "components/web_restrictions/browser/web_restrictions_client.h" | 10 #include "components/web_restrictions/browser/web_restrictions_client.h" |
9 #include "content/public/test/test_browser_thread_bundle.h" | 11 #include "content/public/test/test_browser_thread_bundle.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 | 13 |
12 using web_restrictions::WebRestrictionsClient; | 14 using web_restrictions::WebRestrictionsClient; |
| 15 using web_restrictions::WebRestrictionsClientResult; |
13 using web_restrictions::MockWebRestrictionsClient; | 16 using web_restrictions::MockWebRestrictionsClient; |
14 | 17 |
15 namespace { | 18 namespace { |
16 | 19 |
17 bool g_returned_result; | 20 bool g_returned_result; |
18 | 21 |
19 void ResultCallback(const base::Closure& quit_closure, bool result) { | 22 void ResultCallback(const base::Closure& quit_closure, bool result) { |
20 g_returned_result = result; | 23 g_returned_result = result; |
21 quit_closure.Run(); | 24 quit_closure.Run(); |
22 } | 25 } |
23 | 26 |
24 } // namespace | 27 } // namespace |
25 | 28 |
26 class WebRestrictionsClientTest : public testing::Test { | 29 class WebRestrictionsClientTest : public testing::Test { |
27 protected: | 30 protected: |
28 // Mock the Java WebRestrictionsClient. The real version | 31 // Mock the Java WebRestrictionsClient. The real version |
29 // would need a content provider to do anything. | 32 // would need a content provider to do anything. |
30 MockWebRestrictionsClient mock_; | 33 MockWebRestrictionsClient mock_; |
31 content::TestBrowserThreadBundle thread_bundle_; | 34 content::TestBrowserThreadBundle thread_bundle_; |
32 WebRestrictionsClient client_; | 35 WebRestrictionsClient client_; |
33 }; | 36 }; |
34 | 37 |
35 TEST_F(WebRestrictionsClientTest, ShouldProceed) { | 38 TEST_F(WebRestrictionsClientTest, ShouldProceed) { |
36 client_.SetAuthority("Good"); | 39 client_.SetAuthority("Good"); |
37 // First call should go to Web Restrictions Content Provider, and return a | 40 // First call should go to Web Restrictions Content Provider, and return a |
38 // delayed result. | 41 // delayed result. |
39 { | 42 { |
40 g_returned_result = false; | 43 g_returned_result = false; |
41 base::RunLoop run_loop; | 44 base::RunLoop run_loop; |
42 EXPECT_EQ(web_restrictions::PENDING, | 45 ASSERT_EQ(web_restrictions::PENDING, |
43 client_.ShouldProceed( | 46 client_.ShouldProceed( |
44 true, GURL("http://example.com"), | 47 true, "http://example.com", |
45 base::Bind(&ResultCallback, run_loop.QuitClosure()))); | 48 base::Bind(&ResultCallback, run_loop.QuitClosure()))); |
46 run_loop.Run(); | 49 run_loop.Run(); |
47 EXPECT_TRUE(g_returned_result); | 50 EXPECT_TRUE(g_returned_result); |
48 EXPECT_EQ(1, client_.GetResultColumnCount(GURL("http://example.com"))); | |
49 } | 51 } |
50 // A repeated call should go to the cache and return a result immediately. | 52 // A repeated call should go to the cache and return a result immediately. |
51 { | 53 { |
52 base::RunLoop run_loop; | 54 base::RunLoop run_loop; |
53 EXPECT_EQ(web_restrictions::ALLOW, | 55 ASSERT_EQ(web_restrictions::ALLOW, |
54 client_.ShouldProceed( | 56 client_.ShouldProceed( |
55 true, GURL("http://example.com"), | 57 true, "http://example.com", |
56 base::Bind(&ResultCallback, run_loop.QuitClosure()))); | 58 base::Bind(&ResultCallback, run_loop.QuitClosure()))); |
57 } | 59 } |
58 // However a different url should miss the cache | 60 // However a different url should miss the cache |
59 { | 61 { |
60 g_returned_result = false; | 62 g_returned_result = false; |
61 base::RunLoop run_loop; | 63 base::RunLoop run_loop; |
62 EXPECT_EQ(web_restrictions::PENDING, | 64 ASSERT_EQ(web_restrictions::PENDING, |
63 client_.ShouldProceed( | 65 client_.ShouldProceed( |
64 true, GURL("http://example.com/2"), | 66 true, "http://example.com/2", |
65 base::Bind(&ResultCallback, run_loop.QuitClosure()))); | 67 base::Bind(&ResultCallback, run_loop.QuitClosure()))); |
66 run_loop.Run(); | 68 run_loop.Run(); |
67 EXPECT_TRUE(g_returned_result); | 69 EXPECT_TRUE(g_returned_result); |
68 } | 70 } |
69 // Switching the authority should clear the cache. | 71 // Switching the authority should clear the cache. |
70 { | 72 { |
71 client_.SetAuthority("Good2"); | 73 client_.SetAuthority("Good2"); |
72 g_returned_result = false; | 74 g_returned_result = false; |
73 base::RunLoop run_loop; | 75 base::RunLoop run_loop; |
74 EXPECT_EQ(web_restrictions::PENDING, | 76 ASSERT_EQ(web_restrictions::PENDING, |
75 client_.ShouldProceed( | 77 client_.ShouldProceed( |
76 true, GURL("http://example.com/2"), | 78 true, "http://example.com/2", |
77 base::Bind(&ResultCallback, run_loop.QuitClosure()))); | 79 base::Bind(&ResultCallback, run_loop.QuitClosure()))); |
78 run_loop.Run(); | 80 run_loop.Run(); |
79 EXPECT_TRUE(g_returned_result); | 81 EXPECT_TRUE(g_returned_result); |
80 } | 82 } |
81 // Try getting a bad result | 83 // Try getting a bad result |
82 { | 84 { |
83 client_.SetAuthority("Bad"); | 85 client_.SetAuthority("Bad"); |
84 g_returned_result = true; | 86 g_returned_result = true; |
85 base::RunLoop run_loop; | 87 base::RunLoop run_loop; |
86 EXPECT_EQ(web_restrictions::PENDING, | 88 ASSERT_EQ(web_restrictions::PENDING, |
87 client_.ShouldProceed( | 89 client_.ShouldProceed( |
88 true, GURL("http://example.com/2"), | 90 true, "http://example.com/2", |
89 base::Bind(&ResultCallback, run_loop.QuitClosure()))); | 91 base::Bind(&ResultCallback, run_loop.QuitClosure()))); |
90 run_loop.Run(); | 92 run_loop.Run(); |
91 EXPECT_FALSE(g_returned_result); | 93 EXPECT_FALSE(g_returned_result); |
92 EXPECT_EQ(3, client_.GetResultColumnCount(GURL("http://example.com/2"))); | 94 std::unique_ptr<const WebRestrictionsClientResult> result = |
93 EXPECT_EQ(42, client_.GetResultIntValue(GURL("http://example.com/2"), 1)); | 95 client_.GetCachedWebRestrictionsResult("http://example.com/2"); |
94 EXPECT_EQ("Error string", | 96 ASSERT_NE(nullptr, result.get()); |
95 client_.GetResultColumnName(GURL("http://example.com/2"), 2)); | 97 EXPECT_EQ(42, result->GetInt(1)); |
96 EXPECT_EQ("http://example.com/2", | 98 EXPECT_EQ("http://example.com/2", result->GetString(2)); |
97 client_.GetResultStringValue(GURL("http://example.com/2"), 2)); | |
98 } | 99 } |
99 } | 100 } |
100 | 101 |
101 TEST_F(WebRestrictionsClientTest, RequestPermission) { | 102 TEST_F(WebRestrictionsClientTest, RequestPermission) { |
102 client_.SetAuthority("Good"); | |
103 { | 103 { |
| 104 client_.SetAuthority("Good"); |
104 base::RunLoop run_loop; | 105 base::RunLoop run_loop; |
105 g_returned_result = false; | 106 g_returned_result = false; |
106 client_.RequestPermission( | 107 client_.RequestPermission( |
107 GURL("http://example.com"), | 108 "http://example.com", |
108 base::Bind(&ResultCallback, run_loop.QuitClosure())); | 109 base::Bind(&ResultCallback, run_loop.QuitClosure())); |
109 run_loop.Run(); | 110 run_loop.Run(); |
110 EXPECT_TRUE(g_returned_result); | 111 EXPECT_TRUE(g_returned_result); |
111 client_.SetAuthority("Bad"); | |
112 } | 112 } |
113 { | 113 { |
| 114 client_.SetAuthority("Bad"); |
114 base::RunLoop run_loop; | 115 base::RunLoop run_loop; |
115 g_returned_result = true; | 116 g_returned_result = true; |
116 client_.RequestPermission( | 117 client_.RequestPermission( |
117 GURL("http://example.com"), | 118 "http://example.com", |
118 base::Bind(&ResultCallback, run_loop.QuitClosure())); | 119 base::Bind(&ResultCallback, run_loop.QuitClosure())); |
119 run_loop.Run(); | 120 run_loop.Run(); |
120 EXPECT_FALSE(g_returned_result); | 121 EXPECT_FALSE(g_returned_result); |
121 } | 122 } |
122 } | 123 } |
OLD | NEW |