Commit 9b3fdb53 authored by Ewann's avatar Ewann Committed by Commit Bot

[iOS][OpenIn] Adds unit tests

This CL adds parametrized tests in order to be able to test the different file types added with the kExtendOpenInFilesSupport feature.

Bug: 1117398
Change-Id: I1c7fc04cc9ce2c0ac2f9f6a135b50fea524f2267
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2379716
Commit-Queue: Ewann Pellé <ewannpv@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#811240}
parent 10565066
...@@ -38,6 +38,7 @@ source_set("unit_tests") { ...@@ -38,6 +38,7 @@ source_set("unit_tests") {
testonly = true testonly = true
sources = [ "open_in_tab_helper_unittest.mm" ] sources = [ "open_in_tab_helper_unittest.mm" ]
deps = [ deps = [
":features",
":open_in", ":open_in",
"//base", "//base",
"//base/test:test_support", "//base/test:test_support",
......
...@@ -14,6 +14,43 @@ namespace net { ...@@ -14,6 +14,43 @@ namespace net {
class HttpResponseHeaders; class HttpResponseHeaders;
} // namespace net } // namespace net
namespace content_type {
// .pptx extension.
extern const char kMimeTypeMicrosoftPowerPointOpenXML[];
// .docx extension.
extern const char kMimeTypeMicrosoftWordOpenXML[];
// .xlsx extension.
extern const char kMimeTypeMicrosoftExcelOpenXML[];
// .pdf extension.
extern const char kMimeTypePDF[];
// .doc extension.
extern const char kMimeTypeMicrosoftWord[];
// .jpeg or .jpg extension.
extern const char kMimeTypeJPEG[];
// .png extension.
extern const char kMimeTypePNG[];
// .ppt extension.
extern const char kMimeTypeMicrosoftPowerPoint[];
// .rtf extension.
extern const char kMimeTypeRTF[];
// .svg extension.
extern const char kMimeTypeSVG[];
// .xls extension.
extern const char kMimeTypeMicrosoftExcel[];
} // namespace content_type
// Enum used to determine the MIME type of a previewed file. Entries should // Enum used to determine the MIME type of a previewed file. Entries should
// always keep synced with the IOS.OpenIn.MimeType UMA histogram. // always keep synced with the IOS.OpenIn.MimeType UMA histogram.
enum class OpenInMimeType { enum class OpenInMimeType {
......
...@@ -25,45 +25,34 @@ ...@@ -25,45 +25,34 @@
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
namespace { namespace content_type {
// .pptx extension.
const char kMimeTypeMicrosoftPowerPointOpenXML[] = const char kMimeTypeMicrosoftPowerPointOpenXML[] =
"application/vnd.openxmlformats-officedocument.presentationml.presentation"; "application/vnd.openxmlformats-officedocument.presentationml.presentation";
// .docx extension.
const char kMimeTypeMicrosoftWordOpenXML[] = const char kMimeTypeMicrosoftWordOpenXML[] =
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"; "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
// .xlsx extension.
const char kMimeTypeMicrosoftExcelOpenXML[] = const char kMimeTypeMicrosoftExcelOpenXML[] =
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
// .pdf extension.
const char kMimeTypePDF[] = "application/pdf"; const char kMimeTypePDF[] = "application/pdf";
// .doc extension.
const char kMimeTypeMicrosoftWord[] = "application/msword"; const char kMimeTypeMicrosoftWord[] = "application/msword";
// .jpeg or .jpg extension.
const char kMimeTypeJPEG[] = "image/jpeg"; const char kMimeTypeJPEG[] = "image/jpeg";
// .png extension.
const char kMimeTypePNG[] = "image/png"; const char kMimeTypePNG[] = "image/png";
// .ppt extension.
const char kMimeTypeMicrosoftPowerPoint[] = "application/vnd.ms-powerpoint"; const char kMimeTypeMicrosoftPowerPoint[] = "application/vnd.ms-powerpoint";
// .rtf extension.
const char kMimeTypeRTF[] = "application/rtf"; const char kMimeTypeRTF[] = "application/rtf";
// .svg extension.
const char kMimeTypeSVG[] = "image/svg+xml"; const char kMimeTypeSVG[] = "image/svg+xml";
// .xls extension.
const char kMimeTypeMicrosoftExcel[] = "application/vnd.ms-excel"; const char kMimeTypeMicrosoftExcel[] = "application/vnd.ms-excel";
} // namespace } // namespace content_type
// static // static
void OpenInTabHelper::CreateForWebState(web::WebState* web_state) { void OpenInTabHelper::CreateForWebState(web::WebState* web_state) {
...@@ -89,27 +78,27 @@ OpenInTabHelper::~OpenInTabHelper() { ...@@ -89,27 +78,27 @@ OpenInTabHelper::~OpenInTabHelper() {
OpenInMimeType OpenInTabHelper::GetUmaResult( OpenInMimeType OpenInTabHelper::GetUmaResult(
const std::string& mime_type) const { const std::string& mime_type) const {
if (mime_type == kMimeTypePDF) if (mime_type == content_type::kMimeTypePDF)
return OpenInMimeType::kMimeTypePDF; return OpenInMimeType::kMimeTypePDF;
if (mime_type == kMimeTypeMicrosoftWord) if (mime_type == content_type::kMimeTypeMicrosoftWord)
return OpenInMimeType::kMimeTypeMicrosoftWord; return OpenInMimeType::kMimeTypeMicrosoftWord;
if (mime_type == kMimeTypeMicrosoftWordOpenXML) if (mime_type == content_type::kMimeTypeMicrosoftWordOpenXML)
return OpenInMimeType::kMimeTypeMicrosoftWordOpenXML; return OpenInMimeType::kMimeTypeMicrosoftWordOpenXML;
if (mime_type == kMimeTypeJPEG) if (mime_type == content_type::kMimeTypeJPEG)
return OpenInMimeType::kMimeTypeJPEG; return OpenInMimeType::kMimeTypeJPEG;
if (mime_type == kMimeTypePNG) if (mime_type == content_type::kMimeTypePNG)
return OpenInMimeType::kMimeTypePNG; return OpenInMimeType::kMimeTypePNG;
if (mime_type == kMimeTypeMicrosoftPowerPoint) if (mime_type == content_type::kMimeTypeMicrosoftPowerPoint)
return OpenInMimeType::kMimeTypeMicrosoftPowerPoint; return OpenInMimeType::kMimeTypeMicrosoftPowerPoint;
if (mime_type == kMimeTypeMicrosoftPowerPointOpenXML) if (mime_type == content_type::kMimeTypeMicrosoftPowerPointOpenXML)
return OpenInMimeType::kMimeTypeMicrosoftPowerPointOpenXML; return OpenInMimeType::kMimeTypeMicrosoftPowerPointOpenXML;
if (mime_type == kMimeTypeRTF) if (mime_type == content_type::kMimeTypeRTF)
return OpenInMimeType::kMimeTypeRTF; return OpenInMimeType::kMimeTypeRTF;
if (mime_type == kMimeTypeSVG) if (mime_type == content_type::kMimeTypeSVG)
return OpenInMimeType::kMimeTypeSVG; return OpenInMimeType::kMimeTypeSVG;
if (mime_type == kMimeTypeMicrosoftExcel) if (mime_type == content_type::kMimeTypeMicrosoftExcel)
return OpenInMimeType::kMimeTypeMicrosoftExcel; return OpenInMimeType::kMimeTypeMicrosoftExcel;
if (mime_type == kMimeTypeMicrosoftExcelOpenXML) if (mime_type == content_type::kMimeTypeMicrosoftExcelOpenXML)
return OpenInMimeType::kMimeTypeMicrosoftExcelOpenXML; return OpenInMimeType::kMimeTypeMicrosoftExcelOpenXML;
return OpenInMimeType::kMimeTypeNotHandled; return OpenInMimeType::kMimeTypeNotHandled;
} }
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#import "ios/chrome/browser/open_in/features.h"
#import "ios/chrome/browser/open_in/open_in_tab_helper_delegate.h" #import "ios/chrome/browser/open_in/open_in_tab_helper_delegate.h"
#include "ios/chrome/grit/ios_strings.h" #include "ios/chrome/grit/ios_strings.h"
#include "ios/web/public/navigation/navigation_item.h" #include "ios/web/public/navigation/navigation_item.h"
...@@ -56,18 +58,110 @@ ...@@ -56,18 +58,110 @@
namespace { namespace {
const char kContentDispositionWithFileName[] =
"attachment; filename=\"suggested_filename.pdf\"";
const char kPdfContentType[] = "application/pdf";
const char kInvalidFileNameUrl[] = "https://test.test/"; const char kInvalidFileNameUrl[] = "https://test.test/";
const char kValidFileNameUrl[] = "https://test.test/file_name.pdf";
const char kContentDispositionWithoutFileName[] = const char kContentDispositionWithoutFileName[] =
"attachment; parameter=parameter_value"; "attachment; parameter=parameter_value";
const char kHtmlContentType[] = "text/html"; const char kHtmlContentType[] = "text/html";
const char kValidFileNamePDF[] = "https://test.test/file_name.pdf";
const char kValidFileNameMicrosoftPowerPointOpenXML[] =
"https://test.test/file_name.pptx";
// Returns the content type according to the current testing value.
std::string ContentTypeForMimeType(OpenInMimeType parameter) {
switch (parameter) {
case OpenInMimeType::kMimeTypeMicrosoftPowerPointOpenXML:
return content_type::kMimeTypeMicrosoftPowerPointOpenXML;
break;
case OpenInMimeType::kMimeTypeMicrosoftWordOpenXML:
return content_type::kMimeTypeMicrosoftWordOpenXML;
break;
case OpenInMimeType::kMimeTypeMicrosoftExcelOpenXML:
return content_type::kMimeTypeMicrosoftExcelOpenXML;
break;
case OpenInMimeType::kMimeTypePDF:
return content_type::kMimeTypePDF;
break;
case OpenInMimeType::kMimeTypeMicrosoftWord:
return content_type::kMimeTypeMicrosoftWord;
break;
case OpenInMimeType::kMimeTypeJPEG:
return content_type::kMimeTypeJPEG;
break;
case OpenInMimeType::kMimeTypePNG:
return content_type::kMimeTypePNG;
break;
case OpenInMimeType::kMimeTypeMicrosoftPowerPoint:
return content_type::kMimeTypeMicrosoftPowerPoint;
break;
case OpenInMimeType::kMimeTypeRTF:
return content_type::kMimeTypeRTF;
break;
case OpenInMimeType::kMimeTypeSVG:
return content_type::kMimeTypeSVG;
break;
case OpenInMimeType::kMimeTypeMicrosoftExcel:
return content_type::kMimeTypeMicrosoftExcel;
break;
// Should not be reached.
case OpenInMimeType::kMimeTypeNotHandled:
return "";
break;
}
}
// Returns the file extension according to the current testing value.
std::string ExtensionForMimeType(OpenInMimeType parameter) {
switch (parameter) {
case OpenInMimeType::kMimeTypeMicrosoftPowerPointOpenXML:
return ".pptx";
break;
case OpenInMimeType::kMimeTypeMicrosoftWordOpenXML:
return ".docx";
break;
case OpenInMimeType::kMimeTypeMicrosoftExcelOpenXML:
return ".xlsx";
break;
case OpenInMimeType::kMimeTypePDF:
return ".pdf";
break;
case OpenInMimeType::kMimeTypeMicrosoftWord:
return ".doc";
break;
case OpenInMimeType::kMimeTypeJPEG:
return ".jpeg";
break;
case OpenInMimeType::kMimeTypePNG:
return ".png";
break;
case OpenInMimeType::kMimeTypeMicrosoftPowerPoint:
return ".ppt";
break;
case OpenInMimeType::kMimeTypeRTF:
return ".rtf";
break;
case OpenInMimeType::kMimeTypeSVG:
return ".svg";
break;
case OpenInMimeType::kMimeTypeMicrosoftExcel:
return ".xls";
break;
// Should not be reached.
case OpenInMimeType::kMimeTypeNotHandled:
return "";
break;
}
}
// Returns the file name according to the current testing value.
std::string FileNameForMimeType(OpenInMimeType parameter) {
return "filename" + ExtensionForMimeType(parameter);
}
} // namespace } // namespace
// Test fixture for OpenInTabHelper class. // Test fixture for OpenInTabHelper class.
class OpenInTabHelperTest : public PlatformTest { class OpenInTabHelperTest
: public PlatformTest,
public ::testing::WithParamInterface<OpenInMimeType> {
protected: protected:
OpenInTabHelperTest() OpenInTabHelperTest()
: delegate_([[FakeOpenInTabHelperDelegate alloc] init]) { : delegate_([[FakeOpenInTabHelperDelegate alloc] init]) {
...@@ -130,55 +224,116 @@ TEST_F(OpenInTabHelperTest, WebStateObservationDestruction) { ...@@ -130,55 +224,116 @@ TEST_F(OpenInTabHelperTest, WebStateObservationDestruction) {
EXPECT_TRUE(delegate_.openInDestroyed); EXPECT_TRUE(delegate_.openInDestroyed);
} }
// Tests that openIn is enabled for PDF documents and that it uses the file name // Tests that openIn is enabled for exportable files and that it uses the file
// from the content desposition key in the response headers. // name from the content desposition key in the response headers.
TEST_F(OpenInTabHelperTest, OpenInForPDFWithFileNameFromContentDesposition) { TEST_P(OpenInTabHelperTest,
OpenInForExportableFilesWithFileNameFromContentDesposition) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(kExtendOpenInFilesSupport);
ASSERT_FALSE(delegate_.openInDisabled); ASSERT_FALSE(delegate_.openInDisabled);
GURL url(kValidFileNameUrl); const std::string file_name =
NavigateTo(url, kPdfContentType, kContentDispositionWithFileName); FileNameForMimeType(OpenInTabHelperTest::GetParam());
GURL url("https://test.test/" + file_name);
NavigateTo(url,
ContentTypeForMimeType(OpenInTabHelperTest::GetParam()).c_str(),
("attachment; filename=\"suggested_" + file_name + "\"").c_str());
EXPECT_FALSE(delegate_.openInDisabled); EXPECT_FALSE(delegate_.openInDisabled);
EXPECT_EQ(url, delegate_.lastOpenedDocumentURL); EXPECT_EQ(url, delegate_.lastOpenedDocumentURL);
EXPECT_NSEQ(@"suggested_filename.pdf", delegate_.lastSuggestedFileName);
std::string suggested_file_name = "suggested_" + file_name;
EXPECT_NSEQ(base::SysUTF8ToNSString(suggested_file_name),
delegate_.lastSuggestedFileName);
} }
// Tests that openIn is enabled for PDF documents and that it uses the file name // Tests that openIn is enabled for exportable files and that it uses the file
// from the URL if the content desposition key in the response headers doesn't // name from the URL if the content desposition key in the response headers
// have file name. // doesn't have file name.
TEST_F(OpenInTabHelperTest, OpenInForPDFWithFileNameFromURL) { TEST_P(OpenInTabHelperTest, OpenInForExportableFilesWithFileNameFromURL) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(kExtendOpenInFilesSupport);
ASSERT_FALSE(delegate_.openInDisabled); ASSERT_FALSE(delegate_.openInDisabled);
GURL url(kValidFileNameUrl); const std::string file_name =
NavigateTo(url, kPdfContentType, kContentDispositionWithoutFileName); FileNameForMimeType(OpenInTabHelperTest::GetParam());
EXPECT_FALSE(delegate_.openInDisabled); GURL url("https://test.test/" + file_name);
NavigateTo(url,
ContentTypeForMimeType(OpenInTabHelperTest::GetParam()).c_str(),
kContentDispositionWithoutFileName);
EXPECT_FALSE(delegate_.openInDisabled);
EXPECT_EQ(url, delegate_.lastOpenedDocumentURL); EXPECT_EQ(url, delegate_.lastOpenedDocumentURL);
EXPECT_NSEQ(@"file_name.pdf", delegate_.lastSuggestedFileName);
EXPECT_NSEQ(base::SysUTF8ToNSString(file_name),
delegate_.lastSuggestedFileName);
} }
// Tests that openIn is enabled for PDF documents and that it uses the default // Tests that openIn is enabled for exportable files and that it uses the
// file name if neither the URL nor the content desposition key in the response // default file name if neither the URL nor the content desposition key in the
// headers has a file name. // response headers has a file name.
TEST_F(OpenInTabHelperTest, OpenInForPDFWithDefaultFileName) { TEST_P(OpenInTabHelperTest, OpenInForExportableFilesWithDefaultFileName) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(kExtendOpenInFilesSupport);
ASSERT_FALSE(delegate_.openInDisabled); ASSERT_FALSE(delegate_.openInDisabled);
GURL url(kInvalidFileNameUrl); GURL url(kInvalidFileNameUrl);
NavigateTo(url, kPdfContentType, kContentDispositionWithoutFileName); NavigateTo(url,
ContentTypeForMimeType(OpenInTabHelperTest::GetParam()).c_str(),
kContentDispositionWithoutFileName);
EXPECT_FALSE(delegate_.openInDisabled); EXPECT_FALSE(delegate_.openInDisabled);
EXPECT_EQ(url, delegate_.lastOpenedDocumentURL); EXPECT_EQ(url, delegate_.lastOpenedDocumentURL);
std::string default_file_name = std::string default_file_name =
l10n_util::GetStringUTF8(IDS_IOS_OPEN_IN_FILE_DEFAULT_TITLE) + ".pdf"; l10n_util::GetStringUTF8(IDS_IOS_OPEN_IN_FILE_DEFAULT_TITLE) +
ExtensionForMimeType(OpenInTabHelperTest::GetParam());
// .rtf files return a default name without extension.
if (OpenInTabHelperTest::GetParam() == OpenInMimeType::kMimeTypeRTF) {
default_file_name =
l10n_util::GetStringUTF8(IDS_IOS_OPEN_IN_FILE_DEFAULT_TITLE);
}
EXPECT_NSEQ(base::SysUTF8ToNSString(default_file_name), EXPECT_NSEQ(base::SysUTF8ToNSString(default_file_name),
delegate_.lastSuggestedFileName); delegate_.lastSuggestedFileName);
} }
// Tests that openIn is disabled for non PDF documents. // Tests that openIn is disabled for non exportable files.
TEST_F(OpenInTabHelperTest, OpenInDisabledForNonPDF) { TEST_F(OpenInTabHelperTest, OpenInDisabledForNonExportableFiles) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(kExtendOpenInFilesSupport);
ASSERT_FALSE(delegate_.openInDisabled); ASSERT_FALSE(delegate_.openInDisabled);
GURL url(kValidFileNameUrl);
NavigateTo(url, kHtmlContentType, kContentDispositionWithoutFileName); // Testing PDF.
GURL url_pdf(kValidFileNamePDF);
NavigateTo(url_pdf, kHtmlContentType, kContentDispositionWithoutFileName);
EXPECT_EQ(GURL::EmptyGURL(), delegate_.lastOpenedDocumentURL);
EXPECT_FALSE(delegate_.lastSuggestedFileName);
EXPECT_TRUE(delegate_.openInDisabled);
// Testing Microsoft PowerPoint OpenXML.
GURL url_pptx(kValidFileNameMicrosoftPowerPointOpenXML);
NavigateTo(url_pptx, kHtmlContentType, kContentDispositionWithoutFileName);
EXPECT_EQ(GURL::EmptyGURL(), delegate_.lastOpenedDocumentURL); EXPECT_EQ(GURL::EmptyGURL(), delegate_.lastOpenedDocumentURL);
EXPECT_FALSE(delegate_.lastSuggestedFileName); EXPECT_FALSE(delegate_.lastSuggestedFileName);
EXPECT_TRUE(delegate_.openInDisabled); EXPECT_TRUE(delegate_.openInDisabled);
} }
INSTANTIATE_TEST_SUITE_P(
,
OpenInTabHelperTest,
::testing::Values(OpenInMimeType::kMimeTypePDF,
OpenInMimeType::kMimeTypeMicrosoftWord,
OpenInMimeType::kMimeTypeMicrosoftWordOpenXML,
OpenInMimeType::kMimeTypeJPEG,
OpenInMimeType::kMimeTypePNG,
OpenInMimeType::kMimeTypeMicrosoftPowerPoint,
OpenInMimeType::kMimeTypeMicrosoftPowerPointOpenXML,
OpenInMimeType::kMimeTypeRTF,
OpenInMimeType::kMimeTypeSVG,
OpenInMimeType::kMimeTypeMicrosoftExcel,
OpenInMimeType::kMimeTypeMicrosoftExcelOpenXML));
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