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"
16 std::string expectedUrl;
19 class HistoryStateUtilTest : public ::testing::Test {
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", "" },
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", "" },
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,
62 bool expected_result = tests_[i].expectedUrl.size() > 0;
63 bool actual_result = toUrl.is_valid();
65 actual_result = history_state_util::IsHistoryStateChangeValid(fromUrl,
68 EXPECT_EQ(expected_result, actual_result) << tests_[i].fromUrl << " "
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);
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,
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,
103 EXPECT_FALSE(result.is_valid());
106 } // anonymous namespace