Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / android_webview / native / state_serializer_unittests.cc
blob7f4690e82146f67f4d6188089fbf6bae372bc6a7
1 // Copyright (c) 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 #include "android_webview/native/state_serializer.h"
7 #include <string>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/pickle.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time.h"
13 #include "content/public/browser/content_browser_client.h"
14 #include "content/public/browser/navigation_entry.h"
15 #include "content/public/common/content_client.h"
16 #include "content/public/common/page_state.h"
17 #include "googleurl/src/gurl.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using std::string;
22 namespace android_webview {
24 TEST(AndroidWebViewStateSerializerTest, TestHeaderSerialization) {
25 Pickle pickle;
26 bool result = internal::WriteHeaderToPickle(&pickle);
27 EXPECT_TRUE(result);
29 PickleIterator iterator(pickle);
30 result = internal::RestoreHeaderFromPickle(&iterator);
31 EXPECT_TRUE(result);
34 TEST(AndroidWebViewStateSerializerTest, TestNavigationEntrySerialization) {
35 // This is required for NavigationEntry::Create.
36 content::ContentClient content_client;
37 content::SetContentClient(&content_client);
38 content::ContentBrowserClient browser_client;
39 content::SetBrowserClientForTesting(&browser_client);
41 scoped_ptr<content::NavigationEntry> entry(
42 content::NavigationEntry::Create());
44 const GURL url("http://url");
45 const GURL virtual_url("http://virtual_url");
46 content::Referrer referrer;
47 referrer.url = GURL("http://referrer_url");
48 referrer.policy = WebKit::WebReferrerPolicyOrigin;
49 const string16 title(UTF8ToUTF16("title"));
50 const content::PageState page_state =
51 content::PageState::CreateFromEncodedData("completely bogus state");
52 const bool has_post_data = true;
53 const GURL original_request_url("http://original_request_url");
54 const GURL base_url_for_data_url("http://base_url");
55 const bool is_overriding_user_agent = true;
56 const base::Time timestamp = base::Time::FromInternalValue(12345);
58 entry->SetURL(url);
59 entry->SetVirtualURL(virtual_url);
60 entry->SetReferrer(referrer);
61 entry->SetTitle(title);
62 entry->SetPageState(page_state);
63 entry->SetHasPostData(has_post_data);
64 entry->SetOriginalRequestURL(original_request_url);
65 entry->SetBaseURLForDataURL(base_url_for_data_url);
66 entry->SetIsOverridingUserAgent(is_overriding_user_agent);
67 entry->SetTimestamp(timestamp);
69 Pickle pickle;
70 bool result = internal::WriteNavigationEntryToPickle(*entry, &pickle);
71 EXPECT_TRUE(result);
73 scoped_ptr<content::NavigationEntry> copy(content::NavigationEntry::Create());
74 PickleIterator iterator(pickle);
75 result = internal::RestoreNavigationEntryFromPickle(&iterator, copy.get());
76 EXPECT_TRUE(result);
78 EXPECT_EQ(url, copy->GetURL());
79 EXPECT_EQ(virtual_url, copy->GetVirtualURL());
80 EXPECT_EQ(referrer.url, copy->GetReferrer().url);
81 EXPECT_EQ(referrer.policy, copy->GetReferrer().policy);
82 EXPECT_EQ(title, copy->GetTitle());
83 EXPECT_EQ(page_state, copy->GetPageState());
84 EXPECT_EQ(has_post_data, copy->GetHasPostData());
85 EXPECT_EQ(original_request_url, copy->GetOriginalRequestURL());
86 EXPECT_EQ(base_url_for_data_url, copy->GetBaseURLForDataURL());
87 EXPECT_EQ(is_overriding_user_agent, copy->GetIsOverridingUserAgent());
88 EXPECT_EQ(timestamp, copy->GetTimestamp());
91 } // namespace android_webview