Fix breakages in https://codereview.chromium.org/1155713003/
[chromium-blink-merge.git] / ios / web / history_state_util_unittest.mm
blob8012053c162300e1724ecc0efaa9c3fb98759905
1 // Copyright 2012 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.
5 #import "ios/web/history_state_util.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "testing/gtest_mac.h"
9 #include "url/gurl.h"
11 namespace web {
12 namespace {
13 struct TestEntry {
14   std::string fromUrl;
15   std::string toUrl;
16   std::string expectedUrl;
19 class HistoryStateUtilTest : public ::testing::Test {
20  protected:
21   static const struct TestEntry tests_[];
24 const struct TestEntry HistoryStateUtilTest::tests_[] = {
25     // Valid absolute changes.
26     { "http://foo.com", "http://foo.com/bar", "http://foo.com/bar" },
27     { "https://foo.com", "https://foo.com/bar", "https://foo.com/bar" },
28     { "http://foo.com/", "http://foo.com#bar", "http://foo.com#bar" },
29     { "http://foo.com:80", "http://foo.com:80/b",  "http://foo.com:80/b"},
30     { "http://foo.com:888", "http://foo.com:888/b",  "http://foo.com:888/b"},
31     // Valid relative changes.
32     { "http://foo.com", "#bar", "http://foo.com#bar" },
33     { "http://foo.com/", "#bar", "http://foo.com/#bar" },
34     { "https://foo.com/", "bar", "https://foo.com/bar" },
35     { "http://foo.com/foo/1", "/bar", "http://foo.com/bar" },
36     { "http://foo.com/foo/1", "bar", "http://foo.com/foo/bar" },
37     { "http://foo.com/", "bar.com", "http://foo.com/bar.com" },
38     { "http://foo.com", "bar.com", "http://foo.com/bar.com" },
39     { "http://foo.com:888", "bar.com", "http://foo.com:888/bar.com" },
40     // Invalid scheme changes.
41     { "http://foo.com", "https://foo.com#bar", "" },
42     { "https://foo.com", "http://foo.com#bar", "" },
43     // Invalid domain changes.
44     { "http://foo.com/bar", "http://bar.com", "" },
45     { "http://foo.com/bar", "http://www.foo.com/bar2", "" },
46     // Valid port change.
47     { "http://foo.com", "http://foo.com:80/bar", "http://foo.com/bar" },
48     { "http://foo.com:80", "http://foo.com/bar", "http://foo.com/bar" },
49     // Invalid port change.
50     { "http://foo.com", "http://foo.com:42/bar", "" },
51     { "http://foo.com:42", "http://foo.com/bar", "" },
52     // Invalid URL.
53     { "http://foo.com", "http://fo o.c om/ba r", "" },
54     { "http://foo.com:80", "bar", "http://foo.com:80/bar" }
57 TEST_F(HistoryStateUtilTest, TestIsHistoryStateChangeValid) {
58   for (size_t i = 0; i < arraysize(tests_); ++i) {
59     GURL fromUrl(tests_[i].fromUrl);
60     GURL toUrl = history_state_util::GetHistoryStateChangeUrl(fromUrl, fromUrl,
61                                                               tests_[i].toUrl);
62     bool expected_result = tests_[i].expectedUrl.size() > 0;
63     bool actual_result = toUrl.is_valid();
64     if (actual_result) {
65       actual_result = history_state_util::IsHistoryStateChangeValid(fromUrl,
66                                                                     toUrl);
67     }
68     EXPECT_EQ(expected_result, actual_result) << tests_[i].fromUrl << " "
69                                               << tests_[i].toUrl;
70   }
73 TEST_F(HistoryStateUtilTest, TestGetHistoryStateChangeUrl) {
74   for (size_t i = 0; i < arraysize(tests_); ++i) {
75     GURL fromUrl(tests_[i].fromUrl);
76     GURL expectedResult(tests_[i].expectedUrl);
77     GURL actualResult = history_state_util::GetHistoryStateChangeUrl(
78         fromUrl, fromUrl, tests_[i].toUrl);
79     EXPECT_EQ(expectedResult, actualResult);
80   }
83 // Ensures that the baseUrl is used to resolve the destination, not currentUrl.
84 TEST_F(HistoryStateUtilTest, TestGetHistoryStateChangeUrlWithBase) {
85   GURL fromUrl("http://foo.com/relative/path");
86   GURL baseUrl("http://foo.com");
87   std::string destination = "bar";
89   GURL result = history_state_util::GetHistoryStateChangeUrl(fromUrl, baseUrl,
90                                                              destination);
91   EXPECT_TRUE(result.is_valid());
92   EXPECT_EQ(GURL("http://foo.com/bar"), result);
95 // Ensures that an invalid baseUrl gracefully returns an invalid destination.
96 TEST_F(HistoryStateUtilTest, TestGetHistoryStateChangeUrlWithInvalidBase) {
97   GURL fromUrl("http://foo.com");
98   GURL baseUrl("http://not a url");
99   std::string destination = "baz";
101   GURL result = history_state_util::GetHistoryStateChangeUrl(fromUrl, baseUrl,
102                                                              destination);
103   EXPECT_FALSE(result.is_valid());
106 }  // anonymous namespace
107 }  // namespace web