blob: c4974de1a8a178b834020ad297a2e6b20cb3c8e9 [file] [log] [blame]
Avi Drissman82c45002017-11-29 22:32:291// Copyright 2017 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.
4
5#include "components/app_modal/javascript_dialog_manager.h"
6
7// #include <string>
8
9#include "base/strings/utf_string_conversions.h"
10#include "build/build_config.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace app_modal {
14
15TEST(JavaScriptDialogManagerTest, GetTitle) {
16 struct Case {
17 const char* parent_url;
18 const char* alerting_url;
19 const char* expected_titlecase;
20 const char* expected_no_titlecase;
21 const char* expected_android;
22 } cases[] = {
23 // Standard main frame alert.
24 {"http://foo.com/", "http://foo.com/", "foo.com Says", "foo.com says",
25 "foo.com says"},
26
27 // Subframe alert from the same origin.
28 {"http://foo.com/1", "http://foo.com/2", "foo.com Says", "foo.com says",
29 "foo.com says"},
30 // Subframe alert from a different origin.
31 {"http://foo.com/", "http://bar.com/", "An Embedded Page at bar.com Says",
32 "An embedded page at bar.com says", "An embedded page at bar.com says"},
33
34 // file:
Avi Drissman63f67292017-12-01 02:06:4735 // - main frame:
Avi Drissman82c45002017-11-29 22:32:2936 {"file:///path/to/page.html", "file:///path/to/page.html",
37 "This Page Says", "This page says", "This page says"},
Avi Drissman63f67292017-12-01 02:06:4738 // - subframe:
Avi Drissman82c45002017-11-29 22:32:2939 {"http://foo.com/", "file:///path/to/page.html",
40 "An Embedded Page on This Page Says",
41 "An embedded page on this page says",
42 "An embedded page on this page says"},
43
44 // ftp:
Avi Drissman63f67292017-12-01 02:06:4745 // - main frame:
Avi Drissman82c45002017-11-29 22:32:2946 {"ftp://foo.com/path/to/page.html", "ftp://foo.com/path/to/page.html",
47 "foo.com Says", "foo.com says", "ftp://foo.com says"},
Avi Drissman63f67292017-12-01 02:06:4748 // - subframe:
Avi Drissman82c45002017-11-29 22:32:2949 {"http://foo.com/", "ftp://foo.com/path/to/page.html",
50 "An Embedded Page at foo.com Says", "An embedded page at foo.com says",
51 "An embedded page at ftp://foo.com says"},
52
53 // data:
Avi Drissman63f67292017-12-01 02:06:4754 // - main frame:
Avi Drissman82c45002017-11-29 22:32:2955 {"data:blahblah", "data:blahblah", "This Page Says", "This page says",
56 "This page says"},
Avi Drissman63f67292017-12-01 02:06:4757 // - subframe:
Avi Drissman82c45002017-11-29 22:32:2958 {"http://foo.com/", "data:blahblah", "An Embedded Page on This Page Says",
59 "An embedded page on this page says",
60 "An embedded page on this page says"},
61
62 // javascript:
Avi Drissman63f67292017-12-01 02:06:4763 // - main frame:
Avi Drissman82c45002017-11-29 22:32:2964 {"javascript:abc", "javascript:abc", "This Page Says", "This page says",
65 "This page says"},
Avi Drissman63f67292017-12-01 02:06:4766 // - subframe:
Avi Drissman82c45002017-11-29 22:32:2967 {"http://foo.com/", "javascript:abc",
68 "An Embedded Page on This Page Says",
69 "An embedded page on this page says",
70 "An embedded page on this page says"},
71
72 // about:
Avi Drissman63f67292017-12-01 02:06:4773 // - main frame:
Avi Drissman82c45002017-11-29 22:32:2974 {"about:blank", "about:blank", "This Page Says", "This page says",
75 "This page says"},
Avi Drissman63f67292017-12-01 02:06:4776 // - subframe:
Avi Drissman82c45002017-11-29 22:32:2977 {"http://foo.com/", "about:blank", "An Embedded Page on This Page Says",
78 "An embedded page on this page says",
79 "An embedded page on this page says"},
80
81 // blob:
Avi Drissman63f67292017-12-01 02:06:4782 // - main frame:
Avi Drissman82c45002017-11-29 22:32:2983 {"blob:http://foo.com/66666666-6666-6666-6666-666666666666",
84 "blob:http://foo.com/66666666-6666-6666-6666-666666666666",
Avi Drissman63f67292017-12-01 02:06:4785 "foo.com Says", "foo.com says", "foo.com says"},
86 // - subframe:
87 {"http://bar.com/",
Avi Drissman82c45002017-11-29 22:32:2988 "blob:http://foo.com/66666666-6666-6666-6666-666666666666",
Avi Drissman63f67292017-12-01 02:06:4789 "An Embedded Page at foo.com Says", "An embedded page at foo.com says",
90 "An embedded page at foo.com says"},
Avi Drissman82c45002017-11-29 22:32:2991
92 // filesystem:
Avi Drissman63f67292017-12-01 02:06:4793 // - main frame:
94 {"filesystem:http://foo.com/bar.html",
95 "filesystem:http://foo.com/bar.html", "foo.com Says", "foo.com says",
96 "foo.com says"},
97 // - subframe:
98 {"http://bar.com/", "filesystem:http://foo.com/bar.html",
99 "An Embedded Page at foo.com Says", "An embedded page at foo.com says",
100 "An embedded page at foo.com says"},
Avi Drissman82c45002017-11-29 22:32:29101 };
102
103 for (const auto& test_case : cases) {
104 base::string16 result = JavaScriptDialogManager::GetTitleImpl(
105 GURL(test_case.parent_url), GURL(test_case.alerting_url));
106#if defined(OS_MACOSX)
107 EXPECT_EQ(test_case.expected_titlecase, base::UTF16ToUTF8(result));
108#elif defined(OS_ANDROID)
109 EXPECT_EQ(test_case.expected_android, base::UTF16ToUTF8(result));
110#else
111 EXPECT_EQ(test_case.expected_no_titlecase, base::UTF16ToUTF8(result));
112#endif
113 }
114}
115
116} // namespace app_modal