chromium/third_party/blink/common/page_state/page_state_serialization_unittest.cc

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>

#include <cmath>

#include "base/base64.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/pickle.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/loader/http_body_element_type.h"
#include "third_party/blink/public/common/page_state/page_state_serialization.h"

namespace blink {
namespace {

base::FilePath GetFilePath() {}

//-----------------------------------------------------------------------------

template <typename T>
void ExpectEquality(const T& expected, const T& actual) {}

template <typename T>
void ExpectEquality(const std::vector<T>& expected,
                    const std::vector<T>& actual) {}

template <>
void ExpectEquality(const network::DataElement& expected,
                    const network::DataElement& actual) {}

template <>
void ExpectEquality(const ExplodedHttpBody& expected,
                    const ExplodedHttpBody& actual) {}

template <>
void ExpectEquality(const ExplodedFrameState& expected,
                    const ExplodedFrameState& actual) {}

void ExpectEquality(const ExplodedPageState& expected,
                    const ExplodedPageState& actual) {}

//-----------------------------------------------------------------------------

class PageStateSerializationTest : public testing::Test {};

TEST_F(PageStateSerializationTest, InitiatorOriginAssign) {}

TEST_F(PageStateSerializationTest, BasicEmpty) {}

TEST_F(PageStateSerializationTest, BasicFrame) {}

TEST_F(PageStateSerializationTest, BasicFramePOST) {}

TEST_F(PageStateSerializationTest, BasicFrameSet) {}

TEST_F(PageStateSerializationTest, BasicFrameSetPOST) {}

TEST_F(PageStateSerializationTest, BadMessagesTest1) {}

TEST_F(PageStateSerializationTest, BadMessagesTest2) {}

// Tests that LegacyEncodePageState, which uses the pre-mojo serialization
// format, produces the exact same blob as it did when the test was written.
// This ensures that the implementation is frozen, which is needed to correctly
// test compatibility and migration.
TEST_F(PageStateSerializationTest, LegacyEncodePageStateFrozen) {}

TEST_F(PageStateSerializationTest, ScrollAnchorSelectorLengthLimited) {}

// Change to #if 1 to enable this code. Run this test to generate data, based on
// the current serialization format, for the BackwardsCompat_vXX tests. This
// will generate an expected.dat in the temp directory, which should be moved
// //third_party/blink/common/page_state/test_data/serialized_vXX.dat. A
// corresponding test case for that version should also then be added below. You
// need to add such a test for any addition/change to the schema of serialized
// page state. If you're adding a field whose type is defined externally of
// page_state.mojom, add an backwards compat test for that field specifically
// by dumping a state object with only that field populated. See, e.g.,
// BackwardsCompat_UrlString as an example.
//
// IMPORTANT: this code dumps the serialization as the *current* version, so if
// generating a backwards compat test for v23, the tree must be synced to a
// revision where page_state_serialization.cc:kCurrentVersion == 23.
#if 0
TEST_F(PageStateSerializationTest, DumpExpectedPageStateForBackwardsCompat) {
  // Populate |state| with test data suitable for testing the current (i.e. the
  // latest) version of serialization.  This is accomplished by asking for test
  // data for version 9999 - a future, hypothetical version number.
  ExplodedPageState state;
  PopulatePageStateForBackwardsCompatTest(&state, 9999);

  std::string encoded;
  EncodePageState(state, &encoded);

  std::string base64 = base::Base64Encode(encoded);

  base::FilePath path;
  base::PathService::Get(base::DIR_TEMP, &path);
  path = path.AppendASCII("expected.dat");

  FILE* fp = base::OpenFile(path, "wb");
  ASSERT_TRUE(fp);

  const size_t kRowSize = 76;
  for (size_t offset = 0; offset < base64.size(); offset += kRowSize) {
    size_t length = std::min(base64.size() - offset, kRowSize);
    std::string segment(&base64[offset], length);
    segment.push_back('\n');
    ASSERT_EQ(1U, fwrite(segment.data(), segment.size(), 1, fp));
  }

  fclose(fp);
}
#endif

#if !BUILDFLAG(IS_ANDROID)
// TODO(darin): Re-enable for Android once this test accounts for systems with
//              a device scale factor not equal to 2.
TEST_F(PageStateSerializationTest, BackwardsCompat_v11) {}
#endif

TEST_F(PageStateSerializationTest, BackwardsCompat_v12) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v13) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v14) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v15) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v16) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v18) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v20) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v21) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v22) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v23) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v24) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v25) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v26) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v27) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v28) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v29) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v30) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v31) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v32) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_v33) {}

// Add your new backwards compat test for future versions *above* this
// comment block; field-specific tests go *below* this comment block.
// Any field additions require a new version and backcompat test; only fields
// with external type definitions require their own dedicated test.
// See DumpExpectedPageStateForBackwardsCompat for more details.
// If any of the below tests fail, you likely made a backwards incompatible
// change to a definition that page_state.mojom relies on. Ideally you should
// find a way to avoid making this change; if that's not possible, contact the
// page state serialization owners to figure out a resolution.

TEST_F(PageStateSerializationTest, BackwardsCompat_ReferencedFiles) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_UrlString) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_Referrer) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_Target) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_StateObject) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_DocumentState) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_ScrollRestorationType) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_VisualViewportScrollOffset) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_ScrollOffset) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_ReferrerPolicy) {}

TEST_F(PageStateSerializationTest, BackwardsCompat_HttpBody) {}

// Add new backwards compat field-specific tests here.  See comment above for
// where to put backwards compat version tests.

}  // namespace
}  // namespace blink