blob: 9082d8977fe0a95d87d46a1e9f770f5d479ea7e3 [file] [log] [blame]
// Copyright (c) 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 "ios/web_view/internal/web_view_web_client.h"
#include "ios/web/common/user_agent.h"
#import "ios/web/common/web_view_creation_util.h"
#import "ios/web/public/test/js_test_util.h"
#include "ios/web/public/test/scoped_testing_web_client.h"
#include "ios/web/public/test/web_test.h"
#import "ios/web_view/internal/cwv_web_view_internal.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/base/resource/resource_bundle.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace ios_web_view {
class WebViewWebClientTest : public web::WebTest {
public:
WebViewWebClientTest() : web::WebTest(std::make_unique<WebViewWebClient>()) {
l10n_util::OverrideLocaleWithCocoaLocale();
ui::ResourceBundle::InitSharedInstanceWithLocale(
l10n_util::GetLocaleOverride(), /*delegate=*/nullptr,
ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
}
~WebViewWebClientTest() override {
ui::ResourceBundle::CleanupSharedInstance();
}
void SetUp() override {
web::WebTest::SetUp();
CWVWebView.customUserAgent = nil;
}
void TearDown() override {
web::WebTest::TearDown();
CWVWebView.customUserAgent = nil;
}
private:
DISALLOW_COPY_AND_ASSIGN(WebViewWebClientTest);
};
// Tests that WebViewWebClientTest's GetUserAgent is configured by CWVWebView.
TEST_F(WebViewWebClientTest, GetUserAgent) {
web::WebClient* web_client = GetWebClient();
// Test user agent when neither nor CWVWebView.userAgentProduct
// CWVWebView.customUserAgent have been set.
std::string user_agent_with_empty_product = web::BuildMobileUserAgent("");
EXPECT_EQ(user_agent_with_empty_product,
web_client->GetUserAgent(web::UserAgentType::NONE));
EXPECT_EQ(user_agent_with_empty_product,
web_client->GetUserAgent(web::UserAgentType::AUTOMATIC));
EXPECT_EQ(user_agent_with_empty_product,
web_client->GetUserAgent(web::UserAgentType::MOBILE));
EXPECT_EQ(user_agent_with_empty_product,
web_client->GetUserAgent(web::UserAgentType::DESKTOP));
// Test user agent when only CWVWebView.userAgentProduct is set.
[CWVWebView setUserAgentProduct:@"FooProduct"];
std::string user_agent_with_product = web::BuildMobileUserAgent("FooProduct");
EXPECT_EQ(user_agent_with_product,
web_client->GetUserAgent(web::UserAgentType::NONE));
EXPECT_EQ(user_agent_with_product,
web_client->GetUserAgent(web::UserAgentType::AUTOMATIC));
EXPECT_EQ(user_agent_with_product,
web_client->GetUserAgent(web::UserAgentType::MOBILE));
EXPECT_EQ(user_agent_with_product,
web_client->GetUserAgent(web::UserAgentType::DESKTOP));
// Test user agent when both CWVWebView.customUserAgent and
// CWVWebView.userAgentProduct are set.
CWVWebView.customUserAgent = @"FooCustomUserAgent";
EXPECT_EQ("FooCustomUserAgent",
web_client->GetUserAgent(web::UserAgentType::NONE));
EXPECT_EQ("FooCustomUserAgent",
web_client->GetUserAgent(web::UserAgentType::AUTOMATIC));
EXPECT_EQ("FooCustomUserAgent",
web_client->GetUserAgent(web::UserAgentType::MOBILE));
EXPECT_EQ("FooCustomUserAgent",
web_client->GetUserAgent(web::UserAgentType::DESKTOP));
}
} // namespace ios_web_view