Add device_type to SharedPageState as a prelude to removing user_agent_type from...
[chromium-blink-merge.git] / android_webview / native / state_serializer_unittest.cc
blob27b7e5199b93cb6e7c080bfbe5b3456b0574217e
1 // Copyright 2014 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/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 "testing/gtest/include/gtest/gtest.h"
18 #include "url/gurl.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 = blink::WebReferrerPolicyOrigin;
49 const base::string16 title(base::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);
57 const int http_status_code = 404;
59 entry->SetURL(url);
60 entry->SetVirtualURL(virtual_url);
61 entry->SetReferrer(referrer);
62 entry->SetTitle(title);
63 entry->SetPageState(page_state);
64 entry->SetHasPostData(has_post_data);
65 entry->SetOriginalRequestURL(original_request_url);
66 entry->SetBaseURLForDataURL(base_url_for_data_url);
67 entry->SetIsOverridingUserAgent(is_overriding_user_agent);
68 entry->SetTimestamp(timestamp);
69 entry->SetHttpStatusCode(http_status_code);
71 Pickle pickle;
72 bool result = internal::WriteNavigationEntryToPickle(*entry, &pickle);
73 EXPECT_TRUE(result);
75 scoped_ptr<content::NavigationEntry> copy(content::NavigationEntry::Create());
76 PickleIterator iterator(pickle);
77 result = internal::RestoreNavigationEntryFromPickle(&iterator, copy.get());
78 EXPECT_TRUE(result);
80 EXPECT_EQ(url, copy->GetURL());
81 EXPECT_EQ(virtual_url, copy->GetVirtualURL());
82 EXPECT_EQ(referrer.url, copy->GetReferrer().url);
83 EXPECT_EQ(referrer.policy, copy->GetReferrer().policy);
84 EXPECT_EQ(title, copy->GetTitle());
85 EXPECT_EQ(page_state, copy->GetPageState());
86 EXPECT_EQ(has_post_data, copy->GetHasPostData());
87 EXPECT_EQ(original_request_url, copy->GetOriginalRequestURL());
88 EXPECT_EQ(base_url_for_data_url, copy->GetBaseURLForDataURL());
89 EXPECT_EQ(is_overriding_user_agent, copy->GetIsOverridingUserAgent());
90 EXPECT_EQ(timestamp, copy->GetTimestamp());
91 EXPECT_EQ(http_status_code, copy->GetHttpStatusCode());
94 } // namespace android_webview