content_test_helper.cc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2019 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/sessions/content/content_test_helper.h"
  5. #include <memory>
  6. #include "base/strings/utf_string_conversions.h"
  7. #include "base/time/time.h"
  8. #include "components/sessions/content/content_serialized_navigation_builder.h"
  9. #include "components/sessions/core/serialized_navigation_entry_test_helper.h"
  10. #include "content/public/browser/navigation_entry.h"
  11. #include "content/public/browser/navigation_entry_restore_context.h"
  12. #include "content/public/common/referrer.h"
  13. #include "third_party/blink/public/common/page_state/page_state.h"
  14. #include "url/gurl.h"
  15. namespace sessions {
  16. // static
  17. SerializedNavigationEntry ContentTestHelper::CreateNavigation(
  18. const std::string& virtual_url,
  19. const std::string& title) {
  20. std::unique_ptr<content::NavigationEntry> navigation_entry =
  21. content::NavigationEntry::Create();
  22. navigation_entry->SetReferrer(
  23. content::Referrer(GURL("http://www.referrer.com"),
  24. network::mojom::ReferrerPolicy::kDefault));
  25. navigation_entry->SetURL(GURL(virtual_url));
  26. navigation_entry->SetVirtualURL(GURL(virtual_url));
  27. navigation_entry->SetTitle(base::UTF8ToUTF16(title));
  28. navigation_entry->SetTimestamp(base::Time::Now());
  29. navigation_entry->SetHttpStatusCode(200);
  30. // Initialize the NavigationEntry with a dummy PageState with unique
  31. // item and document sequence numbers. The item sequence number in particular
  32. // is important to initialize because it always defaults to the same value,
  33. // and it is checked during restore to find FrameNavigationEntries that can
  34. // be de-duplicated.
  35. static int64_t next_sequence_number = 1;
  36. int64_t item_sequence_number = next_sequence_number++;
  37. int64_t document_sequence_number = next_sequence_number++;
  38. std::unique_ptr<content::NavigationEntryRestoreContext> restore_context =
  39. content::NavigationEntryRestoreContext::Create();
  40. navigation_entry->SetPageState(
  41. blink::PageState::CreateForTestingWithSequenceNumbers(
  42. GURL(virtual_url), item_sequence_number, document_sequence_number),
  43. restore_context.get());
  44. return ContentSerializedNavigationBuilder::FromNavigationEntry(
  45. test_data::kIndex, navigation_entry.get());
  46. }
  47. } // namespace sessions