Commit 37c66216 authored by Yi Su's avatar Yi Su Committed by Commit Bot

Add search_engine.js and SearchEngineJsTest for OSDD support.

Find the OSDD(Open Search Description Document) <link> in web pages and
return it's URL. This is one of methods that recognize a web page as
a search engine.

Bug: 433824
Cq-Include-Trybots: luci.chromium.try:ios-simulator-cronet;luci.chromium.try:ios-simulator-full-configs
Change-Id: I2c6d31bf48a9de4aedfe884147e55150c0ee23b9
Reviewed-on: https://chromium-review.googlesource.com/c/1290934
Commit-Queue: Yi Su <mrsuyi@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#601150}
parent 5aaf336b
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
import("//ios/web/js_compile.gni")
import("//build/config/features.gni") import("//build/config/features.gni")
import("//rlz/buildflags/buildflags.gni") import("//rlz/buildflags/buildflags.gni")
...@@ -61,3 +62,27 @@ source_set("search_engines") { ...@@ -61,3 +62,27 @@ source_set("search_engines") {
deps += [ "//components/rlz" ] deps += [ "//components/rlz" ]
} }
} }
source_set("unit_tests") {
configs += [ "//build/config/compiler:enable_arc" ]
testonly = true
sources = [
"search_engine_js_unittest.mm",
]
deps = [
":search_engine_js",
"//base:base",
"//base/test:test_support",
"//ios/chrome/browser/web:test_support",
"//ios/web",
"//ios/web/public/test",
"//ios/web/public/test/fakes",
"//testing/gtest",
]
}
js_compile_checked("search_engine_js") {
sources = [
"resources/search_engine.js",
]
}
// Copyright 2018 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.
/**
* @fileoverview Add functionality related to getting image data.
*/
goog.provide('__crWeb.searchEngine');
/**
* Namespace for this file. It depends on |__gCrWeb| having already been
* injected.
*/
__gCrWeb.searchEngine = {};
/**
* Store common namespace object in a global __gCrWeb object referenced by a
* string, so it does not get renamed by closure compiler during the
* minification.
*/
__gCrWeb['searchEngine'] = __gCrWeb.searchEngine;
/* Beginning of anonymous object. */
(function() {
/**
* Find <link> of OSDD(Open Search Description Document) in document and return
* it's URL. If multiple OSDDs are found(which should never happen on a sane web
* site), return the URL of the first OSDD.
* @return {string|undefined} "href" of OSDD <link>, or undefined if not found.
*/
__gCrWeb.searchEngine.getOpenSearchDescriptionDocumentUrl = function() {
var links = document.getElementsByTagName('link');
for (var i = 0; i < links.length; ++i) {
if (links[i].type == 'application/opensearchdescription+xml')
return links[i].href;
}
};
}()); // End of anonymous object
// Copyright 2018 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 "base/macros.h"
#import "ios/web/public/test/web_js_test.h"
#import "ios/web/public/test/web_test_with_web_state.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
// Test fixture for search_engine.js testing.
class SearchEngineJsTest : public web::WebJsTest<web::WebTestWithWebState> {
public:
SearchEngineJsTest()
: web::WebJsTest<web::WebTestWithWebState>(@[ @"search_engine" ]) {}
DISALLOW_COPY_AND_ASSIGN(SearchEngineJsTest);
};
// Tests that __gCrWeb.searchEngine.getOpenSearchDescriptionDocumentUrl returns
// the URL of the first OSDD found in page.
TEST_F(SearchEngineJsTest, TestGetOpenSearchDescriptionDocumentUrlSucceed) {
LoadHtmlAndInject(
@"<html><link rel='search' type='application/opensearchdescription+xml' "
@"title='Chromium Code Search' "
@"href='//cs.chromium.org/codesearch/first_opensearch.xml' />"
@"<link rel='search' type='application/opensearchdescription+xml' "
@"title='Chromium Code Search 2' "
@"href='//cs.chromium.org/codesearch/second_opensearch.xml' />"
@"<link href='/favicon.ico' rel='shortcut icon' "
@"type='image/x-icon'></html>",
GURL("https://cs.chromium.org"));
id result = ExecuteJavaScript(
@"__gCrWeb.searchEngine.getOpenSearchDescriptionDocumentUrl();");
EXPECT_NSEQ(@"https://cs.chromium.org/codesearch/first_opensearch.xml",
result);
}
// Tests that __gCrWeb.searchEngine.getOpenSearchDescriptionDocumentUrl returns
// undefined if no OSDD is found in page.
TEST_F(SearchEngineJsTest, TestGetOpenSearchDescriptionDocumentUrlFail) {
LoadHtmlAndInject(
@"<html><link href='/favicon.ico' rel='shortcut icon' "
@"type='image/x-icon'></html>",
GURL("https://cs.chromium.org"));
id result = ExecuteJavaScript(
@"__gCrWeb.searchEngine.getOpenSearchDescriptionDocumentUrl();");
EXPECT_FALSE(result);
}
...@@ -147,10 +147,10 @@ js_compile_bundle("chrome_bundle_main_frame") { ...@@ -147,10 +147,10 @@ js_compile_bundle("chrome_bundle_main_frame") {
closure_entry_point = "__crWeb.chromeBundleMainFrame" closure_entry_point = "__crWeb.chromeBundleMainFrame"
sources = [ sources = [
"//components/password_manager/ios/resources/password_controller.js", "//components/password_manager/ios/resources/password_controller.js",
"//ios/chrome/browser/search_engines/resources/search_engine.js",
"resources/chrome_bundle_main_frame.js", "resources/chrome_bundle_main_frame.js",
"resources/image_fetch.js", "resources/image_fetch.js",
] ]
deps = [ deps = [
":credential_manager", ":credential_manager",
":payment_request", ":payment_request",
......
...@@ -7,3 +7,4 @@ goog.provide('__crWeb.chromeBundleMainFrame'); ...@@ -7,3 +7,4 @@ goog.provide('__crWeb.chromeBundleMainFrame');
goog.require('__crWeb.imageFetch'); goog.require('__crWeb.imageFetch');
goog.require('__crWeb.passwords'); goog.require('__crWeb.passwords');
goog.require('__crWeb.searchEngine');
...@@ -165,6 +165,7 @@ test("ios_chrome_unittests") { ...@@ -165,6 +165,7 @@ test("ios_chrome_unittests") {
"//ios/chrome/browser/prerender:unit_tests", "//ios/chrome/browser/prerender:unit_tests",
"//ios/chrome/browser/reading_list:unit_tests", "//ios/chrome/browser/reading_list:unit_tests",
"//ios/chrome/browser/safe_mode:unit_tests", "//ios/chrome/browser/safe_mode:unit_tests",
"//ios/chrome/browser/search_engines:unit_tests",
"//ios/chrome/browser/sessions:unit_tests", "//ios/chrome/browser/sessions:unit_tests",
"//ios/chrome/browser/signin:unit_tests", "//ios/chrome/browser/signin:unit_tests",
"//ios/chrome/browser/snapshots:unit_tests", "//ios/chrome/browser/snapshots:unit_tests",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment