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

#include "storage/browser/blob/blob_storage_registry.h"

#include "base/callback.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace storage {
namespace {
using Entry = BlobStorageRegistry::Entry;
using BlobState = BlobStorageRegistry::BlobState;

TEST(BlobStorageRegistry, UUIDRegistration) {
  const std::string kBlob1 = "Blob1";
  BlobStorageRegistry registry;

  EXPECT_FALSE(registry.DeleteEntry(kBlob1));
  EXPECT_EQ(0u, registry.blob_count());

  Entry* entry = registry.CreateEntry(kBlob1);
  ASSERT_NE(nullptr, entry);
  EXPECT_EQ(BlobState::RESERVED, entry->state);
  EXPECT_EQ(1u, entry->refcount);
  EXPECT_FALSE(entry->exceeded_memory);
  EXPECT_FALSE(entry->data.get() || entry->data_builder.get());
  EXPECT_EQ(0u, entry->construction_complete_callbacks.size());

  EXPECT_EQ(entry, registry.GetEntry(kBlob1));
  EXPECT_TRUE(registry.DeleteEntry(kBlob1));
  entry = registry.CreateEntry(kBlob1);

  EXPECT_TRUE(entry->TestAndSetState(BlobState::RESERVED,
                                     BlobState::ASYNC_TRANSPORTATION));
  EXPECT_FALSE(
      entry->TestAndSetState(BlobState::CONSTRUCTION, BlobState::RESERVED));
  EXPECT_FALSE(entry->TestAndSetState(BlobState::ACTIVE, BlobState::RESERVED));
  EXPECT_TRUE(entry->TestAndSetState(BlobState::ASYNC_TRANSPORTATION,
                                     BlobState::CONSTRUCTION));
  EXPECT_EQ(BlobState::CONSTRUCTION, entry->state);
  EXPECT_EQ(1u, registry.blob_count());
}

TEST(BlobStorageRegistry, URLRegistration) {
  const std::string kBlob = "Blob1";
  const std::string kBlob2 = "Blob2";
  const GURL kURL = GURL("blob://Blob1");
  const GURL kURL2 = GURL("blob://Blob2");
  BlobStorageRegistry registry;

  EXPECT_FALSE(registry.IsURLMapped(kURL));
  EXPECT_EQ(nullptr, registry.GetEntryFromURL(kURL, nullptr));
  EXPECT_FALSE(registry.DeleteURLMapping(kURL, nullptr));
  EXPECT_FALSE(registry.CreateUrlMapping(kURL, kBlob));
  EXPECT_EQ(0u, registry.url_count());
  Entry* entry = registry.CreateEntry(kBlob);

  EXPECT_FALSE(registry.IsURLMapped(kURL));
  EXPECT_TRUE(registry.CreateUrlMapping(kURL, kBlob));
  EXPECT_FALSE(registry.CreateUrlMapping(kURL, kBlob2));

  EXPECT_TRUE(registry.IsURLMapped(kURL));
  EXPECT_EQ(entry, registry.GetEntryFromURL(kURL, nullptr));
  std::string uuid;
  EXPECT_EQ(entry, registry.GetEntryFromURL(kURL, &uuid));
  EXPECT_EQ(kBlob, uuid);
  EXPECT_EQ(1u, registry.url_count());

  registry.CreateEntry(kBlob2);
  EXPECT_TRUE(registry.CreateUrlMapping(kURL2, kBlob2));
  EXPECT_EQ(2u, registry.url_count());
  EXPECT_TRUE(registry.DeleteURLMapping(kURL2, &uuid));
  EXPECT_EQ(kBlob2, uuid);
  EXPECT_FALSE(registry.IsURLMapped(kURL2));

  // Both urls point to the same blob.
  EXPECT_TRUE(registry.CreateUrlMapping(kURL2, kBlob));
  std::string uuid2;
  EXPECT_EQ(registry.GetEntryFromURL(kURL, &uuid),
            registry.GetEntryFromURL(kURL2, &uuid2));
  EXPECT_EQ(uuid, uuid2);
}

}  // namespace
}  // namespace storage
