0
Files
src/content/browser/browser_url_handler_impl_unittest.cc
Devlin Cronin 9d570a2219 [Extensions UI] Customize the NTP overridden prompt for the default case
If the NTP was overridden from its default state with Google as the
search provider, show a branded dialog and custom message to the user.

This requires a bit of code acrobatics in order to determine what the
previous NTP and search engine will be. In particular, we need to:
1) Determine if the default search is Google
2) Determine if the default NTP is the "secondary" NTP

1) is relatively straightforward; 2) is more complicated. The NTP can
be overridden by several different sources (extensions, policies, or
a different default search provider). In order to accommodate this,
introduce BrowserURLHandler::GetPossibleRewrites(), which iterates over
all rewriters and accumulates the list of rewrites that would happen.
This, in conjunction with also checking for multiple NTP overriding
extensions, allows us to check if the default NTP would be used if the
overriding extension were removed.

Add tests for various pieces:
- BrowserURLHandler::GetPossibleRewrites()
- ExtensionWebUI::GetNumberOfExtensionsOverridingURL()
- The dialog params generated when a non-default search is selected
- The dialog params generated when multiple NTP-overriding extensions
  are installed
- UI browser tests for both the generic and the default-specific
  dialogs.

Bug: 1079364
Change-Id: I6d37eb8fa85cd824155a9907253a57f1d8cfd51c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2202156
Commit-Queue: Devlin <rdevlin.cronin@chromium.org>
Reviewed-by: Elly Fong-Jones <ellyjones@chromium.org>
Reviewed-by: Brian White <bcwhite@chromium.org>
Reviewed-by: Alex Moshchuk <alexmos@chromium.org>
Cr-Commit-Position: refs/heads/master@{#774653}
2020-06-03 16:00:59 +00:00

131 lines
4.5 KiB
C++

// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/browser_url_handler_impl.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace content {
class BrowserURLHandlerImplTest : public testing::Test {
};
// Test URL rewriter that rewrites all "foo://" URLs to "bar://bar".
static bool FooRewriter(GURL* url, BrowserContext* browser_context) {
if (url->scheme() == "foo") {
*url = GURL("bar://bar");
return true;
}
return false;
}
// Test URL rewriter that rewrites all "bar://" URLs to "foo://foo".
static bool BarRewriter(GURL* url, BrowserContext* browser_context) {
if (url->scheme() == "bar") {
*url = GURL("foo://foo");
return true;
}
return false;
}
TEST_F(BrowserURLHandlerImplTest, BasicRewriteAndReverse) {
BrowserTaskEnvironment task_environment;
TestBrowserContext browser_context;
BrowserURLHandlerImpl handler;
handler.AddHandlerPair(FooRewriter, BarRewriter);
GURL url("foo://bar");
GURL original_url(url);
bool reverse_on_redirect = false;
handler.RewriteURLIfNecessary(&url, &browser_context, &reverse_on_redirect);
ASSERT_TRUE(reverse_on_redirect);
ASSERT_EQ("bar://bar", url.spec());
// Check that reversing the URL works.
GURL saved_url(url);
bool reversed = handler.ReverseURLRewrite(&url,
original_url,
&browser_context);
ASSERT_TRUE(reversed);
ASSERT_EQ("foo://foo", url.spec());
// Check that reversing the URL only works with a matching |original_url|.
url = saved_url;
original_url = GURL("bam://bam"); // Won't be matched by FooRewriter.
reversed = handler.ReverseURLRewrite(&url, original_url, &browser_context);
ASSERT_FALSE(reversed);
ASSERT_EQ(saved_url, url);
}
TEST_F(BrowserURLHandlerImplTest, NullHandlerReverse) {
BrowserTaskEnvironment task_environment;
TestBrowserContext browser_context;
BrowserURLHandlerImpl handler;
GURL url("bar://foo");
GURL original_url(url);
handler.AddHandlerPair(BrowserURLHandlerImpl::null_handler(), FooRewriter);
bool reversed = handler.ReverseURLRewrite(&url,
original_url,
&browser_context);
ASSERT_FALSE(reversed);
ASSERT_EQ(original_url, url);
handler.AddHandlerPair(BrowserURLHandlerImpl::null_handler(), BarRewriter);
reversed = handler.ReverseURLRewrite(&url, original_url, &browser_context);
ASSERT_TRUE(reversed);
ASSERT_EQ("foo://foo", url.spec());
}
// Verify that the reverse handler for view-source does not duplicate query
// parameters.
TEST_F(BrowserURLHandlerImplTest, ViewSourceReverse) {
BrowserTaskEnvironment task_environment;
TestBrowserContext browser_context;
BrowserURLHandlerImpl handler;
GURL url("http://foo/?a=1");
GURL original_url("view-source:http://some_url");
bool reversed = handler.ReverseURLRewrite(&url,
original_url,
&browser_context);
ASSERT_TRUE(reversed);
ASSERT_EQ("view-source:http://foo/?a=1", url.spec());
}
// Verify that GetPossibleRewrites retrieves the rewrites from all handlers that
// match, in order of priority.
TEST_F(BrowserURLHandlerImplTest, GetPossibleRewrites) {
BrowserTaskEnvironment task_environment;
TestBrowserContext browser_context;
BrowserURLHandlerImpl handler;
auto rewriter1 = [](GURL* url, BrowserContext* context) {
*url = GURL("https://test1.example");
return true;
};
auto rewriter2 = [](GURL* url, BrowserContext* context) { return false; };
auto rewriter3 = [](GURL* url, BrowserContext* context) {
*url = GURL("https://test3.example");
return true;
};
handler.AddHandlerPair(rewriter1, BrowserURLHandlerImpl::null_handler());
handler.AddHandlerPair(rewriter2, BrowserURLHandlerImpl::null_handler());
handler.AddHandlerPair(rewriter3, BrowserURLHandlerImpl::null_handler());
GURL url("https://example.com");
std::vector<GURL> rewrites =
handler.GetPossibleRewrites(url, &browser_context);
EXPECT_THAT(rewrites, testing::ElementsAre(GURL("https://test1.example"),
GURL("https://test3.example")));
}
} // namespace content