1 // Copyright 2015 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.
5 #include "base/strings/stringprintf.h"
6 #include "chrome/browser/search_engines/template_url_service_factory.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/test/base/in_process_browser_test.h"
11 #include "chrome/test/base/ui_test_utils.h"
12 #include "components/search_engines/template_url.h"
13 #include "components/search_engines/template_url_service.h"
14 #include "net/test/embedded_test_server/embedded_test_server.h"
15 #include "net/test/embedded_test_server/http_request.h"
16 #include "net/test/embedded_test_server/http_response.h"
18 using net::test_server::BasicHttpResponse
;
19 using net::test_server::EmbeddedTestServer
;
20 using net::test_server::HttpRequest
;
21 using net::test_server::HttpResponse
;
25 class TemplateURLServiceObserver
{
27 TemplateURLServiceObserver(TemplateURLService
* service
, base::RunLoop
* loop
)
30 template_url_sub_
= service
->RegisterOnLoadedCallback(base::Bind(
31 &TemplateURLServiceObserver::StopLoop
, base::Unretained(this)));
34 ~TemplateURLServiceObserver() {}
37 void StopLoop() { runner_
->Quit(); }
38 base::RunLoop
* runner_
;
39 scoped_ptr
<TemplateURLService::Subscription
> template_url_sub_
;
41 DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceObserver
);
44 testing::AssertionResult
VerifyTemplateURLServiceLoad(
45 TemplateURLService
* service
) {
46 if (service
->loaded())
47 return testing::AssertionSuccess();
49 TemplateURLServiceObserver
observer(service
, &runner
);
51 if (service
->loaded())
52 return testing::AssertionSuccess();
53 return testing::AssertionFailure() << "TemplateURLService isn't loaded";
58 class SearchEngineTabHelperBrowserTest
: public InProcessBrowserTest
{
60 SearchEngineTabHelperBrowserTest() {}
61 ~SearchEngineTabHelperBrowserTest() override
{}
64 scoped_ptr
<HttpResponse
> HandleRequest(const GURL
& osdd_xml_url
,
65 const HttpRequest
& request
) {
66 std::string html
= base::StringPrintf(
69 " <link rel='search' type='application/opensearchdescription+xml'"
71 " title='ExampleSearch'>"
74 osdd_xml_url
.spec().c_str());
76 scoped_ptr
<BasicHttpResponse
> http_response(new BasicHttpResponse());
77 http_response
->set_code(net::HTTP_OK
);
78 http_response
->set_content(html
);
79 http_response
->set_content_type("text/html");
80 return http_response
.Pass();
83 // Starts a test server that serves a page pointing to a opensearch descriptor
84 // from a file:// url.
85 bool StartTestServer() {
86 GURL file_url
= ui_test_utils::GetTestUrl(
88 base::FilePath().AppendASCII("simple_open_search.xml"));
89 embedded_test_server()->RegisterRequestHandler(
90 base::Bind(&SearchEngineTabHelperBrowserTest::HandleRequest
,
91 base::Unretained(this), file_url
));
92 return embedded_test_server()->InitializeAndWaitUntilReady();
95 void SetUpOnMainThread() override
{ ASSERT_TRUE(StartTestServer()); }
97 DISALLOW_COPY_AND_ASSIGN(SearchEngineTabHelperBrowserTest
);
100 IN_PROC_BROWSER_TEST_F(SearchEngineTabHelperBrowserTest
,
101 IgnoreSearchDescriptionsFromFileURLs
) {
102 TemplateURLService
* url_service
=
103 TemplateURLServiceFactory::GetForProfile(browser()->profile());
104 ASSERT_TRUE(url_service
);
105 VerifyTemplateURLServiceLoad(url_service
);
106 TemplateURLService::TemplateURLVector template_urls
=
107 url_service
->GetTemplateURLs();
108 // Navigate to a page with a search descriptor. Path doesn't matter as the
109 // test server always serves the same HTML.
110 GURL
url(embedded_test_server()->GetURL("/"));
111 ui_test_utils::NavigateToURL(browser(), url
);
112 // No new search engines should be added.
113 EXPECT_EQ(template_urls
, url_service
->GetTemplateURLs());