app_modal_dialog_manager_unittest.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // 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. #include "components/javascript_dialogs/app_modal_dialog_manager.h"
  5. #include "base/strings/utf_string_conversions.h"
  6. #include "build/build_config.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. #include "url/origin.h"
  9. namespace javascript_dialogs {
  10. TEST(AppModalDialogManagerTest, GetTitle) {
  11. struct Case {
  12. // The name of the test case.
  13. const char* case_name;
  14. // The URL of the main frame of the page.
  15. const char* main_frame_url;
  16. // Whether the main frame is alerting.
  17. bool is_main_frame;
  18. // If `is_main_frame` is false, the URL of the alerting frame of the page.
  19. const char* alerting_frame_url;
  20. // The expected title for the alert.
  21. const char* expected;
  22. } cases[] = {
  23. // Standard main frame alert.
  24. {"standard", "http://foo.com/", true, "", "foo.com says"},
  25. // Subframe alert from the same origin.
  26. {"subframe same origin", "http://foo.com/1", false, "http://foo.com/2",
  27. "foo.com says"},
  28. // Subframe alert from a different origin.
  29. {"subframe different origin", "http://foo.com/", false, "http://bar.com/",
  30. "An embedded page at bar.com says"},
  31. // file:
  32. // - main frame:
  33. {"file main frame", "file:///path/to/page.html", true, "",
  34. "This page says"},
  35. // - subframe:
  36. {"file subframe", "http://foo.com/", false, "file:///path/to/page.html",
  37. "An embedded page on this page says"},
  38. // data:
  39. // /!\ NOTE that this is for data URLs entered directly in the omnibox.
  40. // For pages that generate frames with data URLs, see the browsertest.
  41. // - main frame:
  42. {"data main frame", "data:blahblah", true, "", "This page says"},
  43. // - subframe:
  44. {"data subframe", "http://foo.com/", false, "data:blahblah",
  45. "An embedded page on this page says"},
  46. // javascript:
  47. // /!\ NOTE that this is for javascript URLs entered directly in the
  48. // omnibox. For pages that generate frames with javascript URLs, see the
  49. // browsertest.
  50. // - main frame:
  51. {"javascript main frame", "javascript:abc", true, "", "This page says"},
  52. // - subframe:
  53. {"javascript subframe", "http://foo.com/", false, "javascript:abc",
  54. "An embedded page on this page says"},
  55. // about:
  56. // /!\ NOTE that this is for about:blank URLs entered directly in the
  57. // omnibox. For pages that generate frames with about:blank URLs, see the
  58. // browsertest.
  59. // - main frame:
  60. {"about main frame", "about:blank", true, "", "This page says"},
  61. // - subframe:
  62. {"about subframe", "http://foo.com/", false, "about:blank",
  63. "An embedded page on this page says"},
  64. // blob:
  65. // - main frame:
  66. {"blob main frame",
  67. "blob:http://foo.com/66666666-6666-6666-6666-666666666666", true, "",
  68. "foo.com says"},
  69. // - subframe:
  70. {"blob subframe", "http://bar.com/", false,
  71. "blob:http://foo.com/66666666-6666-6666-6666-666666666666",
  72. "An embedded page at foo.com says"},
  73. // filesystem:
  74. // - main frame:
  75. {"filesystem main frame", "filesystem:http://foo.com/bar.html", true, "",
  76. "foo.com says"},
  77. // - subframe:
  78. {"filesystem subframe", "http://bar.com/", false,
  79. "filesystem:http://foo.com/bar.html",
  80. "An embedded page at foo.com says"},
  81. };
  82. for (const auto& test_case : cases) {
  83. SCOPED_TRACE(test_case.case_name);
  84. url::Origin main_frame_origin =
  85. url::Origin::Create(GURL(test_case.main_frame_url));
  86. url::Origin alerting_frame_origin =
  87. test_case.is_main_frame
  88. ? main_frame_origin
  89. : url::Origin::Create(GURL(test_case.alerting_frame_url));
  90. std::u16string result = AppModalDialogManager::GetTitleImpl(
  91. main_frame_origin, alerting_frame_origin);
  92. EXPECT_EQ(test_case.expected, base::UTF16ToUTF8(result));
  93. }
  94. }
  95. } // namespace javascript_dialogs