| 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/test/base/testing_browser_process_test.h" | |
| 6 #include "chrome/test/base/testing_profile.h" | |
| 7 #include "content/browser/browser_url_handler.h" | 5 #include "content/browser/browser_url_handler.h" |
| 6 #include "content/test/test_browser_context.h" |
| 8 #include "googleurl/src/gurl.h" | 7 #include "googleurl/src/gurl.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 9 |
| 11 class BrowserURLHandlerTest : public TestingBrowserProcessTest { | 10 class BrowserURLHandlerTest : public testing::Test { |
| 12 }; | 11 }; |
| 13 | 12 |
| 14 // Test URL rewriter that rewrites all "foo://" URLs to "bar://bar". | 13 // Test URL rewriter that rewrites all "foo://" URLs to "bar://bar". |
| 15 static bool FooRewriter(GURL* url, content::BrowserContext* browser_context) { | 14 static bool FooRewriter(GURL* url, content::BrowserContext* browser_context) { |
| 16 if (url->scheme() == "foo") { | 15 if (url->scheme() == "foo") { |
| 17 *url = GURL("bar://bar"); | 16 *url = GURL("bar://bar"); |
| 18 return true; | 17 return true; |
| 19 } | 18 } |
| 20 return false; | 19 return false; |
| 21 } | 20 } |
| 22 | 21 |
| 23 // Test URL rewriter that rewrites all "bar://" URLs to "foo://foo". | 22 // Test URL rewriter that rewrites all "bar://" URLs to "foo://foo". |
| 24 static bool BarRewriter(GURL* url, content::BrowserContext* browser_context) { | 23 static bool BarRewriter(GURL* url, content::BrowserContext* browser_context) { |
| 25 if (url->scheme() == "bar") { | 24 if (url->scheme() == "bar") { |
| 26 *url = GURL("foo://foo"); | 25 *url = GURL("foo://foo"); |
| 27 return true; | 26 return true; |
| 28 } | 27 } |
| 29 return false; | 28 return false; |
| 30 } | 29 } |
| 31 | 30 |
| 32 TEST_F(BrowserURLHandlerTest, BasicRewriteAndReverse) { | 31 TEST_F(BrowserURLHandlerTest, BasicRewriteAndReverse) { |
| 33 TestingProfile profile; | 32 TestBrowserContext browser_context; |
| 34 BrowserURLHandler handler; | 33 BrowserURLHandler handler; |
| 35 | 34 |
| 36 handler.AddHandlerPair(FooRewriter, BarRewriter); | 35 handler.AddHandlerPair(FooRewriter, BarRewriter); |
| 37 | 36 |
| 38 GURL url("foo://bar"); | 37 GURL url("foo://bar"); |
| 39 GURL original_url(url); | 38 GURL original_url(url); |
| 40 bool reverse_on_redirect = false; | 39 bool reverse_on_redirect = false; |
| 41 handler.RewriteURLIfNecessary(&url, &profile, &reverse_on_redirect); | 40 handler.RewriteURLIfNecessary(&url, &browser_context, &reverse_on_redirect); |
| 42 ASSERT_TRUE(reverse_on_redirect); | 41 ASSERT_TRUE(reverse_on_redirect); |
| 43 ASSERT_EQ("bar://bar", url.spec()); | 42 ASSERT_EQ("bar://bar", url.spec()); |
| 44 | 43 |
| 45 // Check that reversing the URL works. | 44 // Check that reversing the URL works. |
| 46 GURL saved_url(url); | 45 GURL saved_url(url); |
| 47 bool reversed = handler.ReverseURLRewrite(&url, original_url, &profile); | 46 bool reversed = handler.ReverseURLRewrite(&url, |
| 47 original_url, |
| 48 &browser_context); |
| 48 ASSERT_TRUE(reversed); | 49 ASSERT_TRUE(reversed); |
| 49 ASSERT_EQ("foo://foo", url.spec()); | 50 ASSERT_EQ("foo://foo", url.spec()); |
| 50 | 51 |
| 51 // Check that reversing the URL only works with a matching |original_url|. | 52 // Check that reversing the URL only works with a matching |original_url|. |
| 52 url = saved_url; | 53 url = saved_url; |
| 53 original_url = GURL("bam://bam"); // Won't be matched by FooRewriter. | 54 original_url = GURL("bam://bam"); // Won't be matched by FooRewriter. |
| 54 reversed = handler.ReverseURLRewrite(&url, original_url, &profile); | 55 reversed = handler.ReverseURLRewrite(&url, original_url, &browser_context); |
| 55 ASSERT_FALSE(reversed); | 56 ASSERT_FALSE(reversed); |
| 56 ASSERT_EQ(saved_url, url); | 57 ASSERT_EQ(saved_url, url); |
| 57 } | 58 } |
| 58 | 59 |
| 59 TEST_F(BrowserURLHandlerTest, NullHandlerReverse) { | 60 TEST_F(BrowserURLHandlerTest, NullHandlerReverse) { |
| 60 TestingProfile profile; | 61 TestBrowserContext browser_context; |
| 61 BrowserURLHandler handler; | 62 BrowserURLHandler handler; |
| 62 | 63 |
| 63 GURL url("bar://foo"); | 64 GURL url("bar://foo"); |
| 64 GURL original_url(url); | 65 GURL original_url(url); |
| 65 | 66 |
| 66 handler.AddHandlerPair(BrowserURLHandler::null_handler(), FooRewriter); | 67 handler.AddHandlerPair(BrowserURLHandler::null_handler(), FooRewriter); |
| 67 bool reversed = handler.ReverseURLRewrite(&url, original_url, &profile); | 68 bool reversed = handler.ReverseURLRewrite(&url, |
| 69 original_url, |
| 70 &browser_context); |
| 68 ASSERT_FALSE(reversed); | 71 ASSERT_FALSE(reversed); |
| 69 ASSERT_EQ(original_url, url); | 72 ASSERT_EQ(original_url, url); |
| 70 | 73 |
| 71 handler.AddHandlerPair(BrowserURLHandler::null_handler(), BarRewriter); | 74 handler.AddHandlerPair(BrowserURLHandler::null_handler(), BarRewriter); |
| 72 reversed = handler.ReverseURLRewrite(&url, original_url, &profile); | 75 reversed = handler.ReverseURLRewrite(&url, original_url, &browser_context); |
| 73 ASSERT_TRUE(reversed); | 76 ASSERT_TRUE(reversed); |
| 74 ASSERT_EQ("foo://foo", url.spec()); | 77 ASSERT_EQ("foo://foo", url.spec()); |
| 75 } | 78 } |
| OLD | NEW |