/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "StringPool.h"

#include <string>

#include "androidfw/StringPiece.h"

#include "test/Test.h"
#include "util/Util.h"

using ::android::StringPiece;
using ::android::StringPiece16;
using ::testing::Eq;
using ::testing::Ne;
using ::testing::NotNull;
using ::testing::Pointee;

namespace aapt {

TEST(StringPoolTest, InsertOneString) {
  StringPool pool;

  StringPool::Ref ref = pool.MakeRef("wut");
  EXPECT_THAT(*ref, Eq("wut"));
}

TEST(StringPoolTest, InsertTwoUniqueStrings) {
  StringPool pool;

  StringPool::Ref ref_a = pool.MakeRef("wut");
  StringPool::Ref ref_b = pool.MakeRef("hey");

  EXPECT_THAT(*ref_a, Eq("wut"));
  EXPECT_THAT(*ref_b, Eq("hey"));
}

TEST(StringPoolTest, DoNotInsertNewDuplicateString) {
  StringPool pool;

  StringPool::Ref ref_a = pool.MakeRef("wut");
  StringPool::Ref ref_b = pool.MakeRef("wut");

  EXPECT_THAT(*ref_a, Eq("wut"));
  EXPECT_THAT(*ref_b, Eq("wut"));
  EXPECT_THAT(pool.size(), Eq(1u));
}

TEST(StringPoolTest, MaintainInsertionOrderIndex) {
  StringPool pool;

  StringPool::Ref ref_a = pool.MakeRef("z");
  StringPool::Ref ref_b = pool.MakeRef("a");
  StringPool::Ref ref_c = pool.MakeRef("m");

  EXPECT_THAT(ref_a.index(), Eq(0u));
  EXPECT_THAT(ref_b.index(), Eq(1u));
  EXPECT_THAT(ref_c.index(), Eq(2u));
}

TEST(StringPoolTest, PruneStringsWithNoReferences) {
  StringPool pool;

  StringPool::Ref ref_a = pool.MakeRef("foo");

  {
    StringPool::Ref ref_b = pool.MakeRef("wut");
    EXPECT_THAT(*ref_b, Eq("wut"));
    EXPECT_THAT(pool.size(), Eq(2u));
    pool.Prune();
    EXPECT_THAT(pool.size(), Eq(2u));
  }
  EXPECT_THAT(pool.size(), Eq(2u));

  {
    StringPool::Ref ref_c = pool.MakeRef("bar");
    EXPECT_THAT(pool.size(), Eq(3u));

    pool.Prune();
    EXPECT_THAT(pool.size(), Eq(2u));
  }
  EXPECT_THAT(pool.size(), Eq(2u));

  pool.Prune();
  EXPECT_THAT(pool.size(), Eq(1u));
}

TEST(StringPoolTest, SortAndMaintainIndexesInStringReferences) {
  StringPool pool;

  StringPool::Ref ref_a = pool.MakeRef("z");
  StringPool::Ref ref_b = pool.MakeRef("a");
  StringPool::Ref ref_c = pool.MakeRef("m");

  EXPECT_THAT(*ref_a, Eq("z"));
  EXPECT_THAT(ref_a.index(), Eq(0u));

  EXPECT_THAT(*ref_b, Eq("a"));
  EXPECT_THAT(ref_b.index(), Eq(1u));

  EXPECT_THAT(*ref_c, Eq("m"));
  EXPECT_THAT(ref_c.index(), Eq(2u));

  pool.Sort();

  EXPECT_THAT(*ref_a, Eq("z"));
  EXPECT_THAT(ref_a.index(), Eq(2u));

  EXPECT_THAT(*ref_b, Eq("a"));
  EXPECT_THAT(ref_b.index(), Eq(0u));

  EXPECT_THAT(*ref_c, Eq("m"));
  EXPECT_THAT(ref_c.index(), Eq(1u));
}

TEST(StringPoolTest, SortAndStillDedupe) {
  StringPool pool;

  StringPool::Ref ref_a = pool.MakeRef("z");
  StringPool::Ref ref_b = pool.MakeRef("a");
  StringPool::Ref ref_c = pool.MakeRef("m");

  pool.Sort();

  StringPool::Ref ref_d = pool.MakeRef("z");
  StringPool::Ref ref_e = pool.MakeRef("a");
  StringPool::Ref ref_f = pool.MakeRef("m");

  EXPECT_THAT(ref_d.index(), Eq(ref_a.index()));
  EXPECT_THAT(ref_e.index(), Eq(ref_b.index()));
  EXPECT_THAT(ref_f.index(), Eq(ref_c.index()));
}

TEST(StringPoolTest, AddStyles) {
  StringPool pool;

  StringPool::StyleRef ref = pool.MakeRef(StyleString{{"android"}, {Span{{"b"}, 2, 6}}});
  EXPECT_THAT(ref.index(), Eq(0u));
  EXPECT_THAT(ref->value, Eq("android"));
  ASSERT_THAT(ref->spans.size(), Eq(1u));

  const StringPool::Span& span = ref->spans.front();
  EXPECT_THAT(*span.name, Eq("b"));
  EXPECT_THAT(span.first_char, Eq(2u));
  EXPECT_THAT(span.last_char, Eq(6u));
}

TEST(StringPoolTest, DoNotDedupeStyleWithSameStringAsNonStyle) {
  StringPool pool;

  StringPool::Ref ref = pool.MakeRef("android");

  StyleString str{{"android"}};
  StringPool::StyleRef style_ref = pool.MakeRef(StyleString{{"android"}});

  EXPECT_THAT(ref.index(), Ne(style_ref.index()));
}

TEST(StringPoolTest, StylesAndStringsAreSeparateAfterSorting) {
  StringPool pool;

  StringPool::StyleRef ref_a = pool.MakeRef(StyleString{{"beta"}});
  StringPool::Ref ref_b = pool.MakeRef("alpha");
  StringPool::StyleRef ref_c = pool.MakeRef(StyleString{{"alpha"}});

  EXPECT_THAT(ref_b.index(), Ne(ref_c.index()));

  pool.Sort();

  EXPECT_THAT(ref_c.index(), Eq(0u));
  EXPECT_THAT(ref_a.index(), Eq(1u));
  EXPECT_THAT(ref_b.index(), Eq(2u));
}

TEST(StringPoolTest, FlattenEmptyStringPoolUtf8) {
  using namespace android;  // For NO_ERROR on Windows.

  StringPool pool;
  BigBuffer buffer(1024);
  StringPool::FlattenUtf8(&buffer, pool);

  std::unique_ptr<uint8_t[]> data = util::Copy(buffer);
  ResStringPool test;
  ASSERT_THAT(test.setTo(data.get(), buffer.size()), Eq(NO_ERROR));
}

TEST(StringPoolTest, FlattenOddCharactersUtf16) {
  using namespace android;  // For NO_ERROR on Windows.

  StringPool pool;
  pool.MakeRef("\u093f");
  BigBuffer buffer(1024);
  StringPool::FlattenUtf16(&buffer, pool);

  std::unique_ptr<uint8_t[]> data = util::Copy(buffer);
  ResStringPool test;
  ASSERT_EQ(test.setTo(data.get(), buffer.size()), NO_ERROR);
  size_t len = 0;
  const char16_t* str = test.stringAt(0, &len);
  EXPECT_THAT(len, Eq(1u));
  EXPECT_THAT(str, Pointee(Eq(u'\u093f')));
  EXPECT_THAT(str[1], Eq(0u));
}

constexpr const char* sLongString =
    "バッテリーを長持ちさせるため、バッテリーセーバーは端末のパフォーマンスを抑"
    "え、バイブレーション、位置情報サービス、大半のバックグラウンドデータを制限"
    "します。メール、SMSや、同期を使 "
    "用するその他のアプリは、起動しても更新されないことがあります。バッテリーセ"
    "ーバーは端末の充電中は自動的にOFFになります。";

TEST(StringPoolTest, Flatten) {
  using namespace android;  // For NO_ERROR on Windows.

  StringPool pool;

  StringPool::Ref ref_a = pool.MakeRef("hello");
  StringPool::Ref ref_b = pool.MakeRef("goodbye");
  StringPool::Ref ref_c = pool.MakeRef(sLongString);
  StringPool::Ref ref_d = pool.MakeRef("");
  StringPool::StyleRef ref_e =
      pool.MakeRef(StyleString{{"style"}, {Span{{"b"}, 0, 1}, Span{{"i"}, 2, 3}}});

  // Styles are always first.
  EXPECT_THAT(ref_e.index(), Eq(0u));

  EXPECT_THAT(ref_a.index(), Eq(1u));
  EXPECT_THAT(ref_b.index(), Eq(2u));
  EXPECT_THAT(ref_c.index(), Eq(3u));
  EXPECT_THAT(ref_d.index(), Eq(4u));

  BigBuffer buffers[2] = {BigBuffer(1024), BigBuffer(1024)};
  StringPool::FlattenUtf8(&buffers[0], pool);
  StringPool::FlattenUtf16(&buffers[1], pool);

  // Test both UTF-8 and UTF-16 buffers.
  for (const BigBuffer& buffer : buffers) {
    std::unique_ptr<uint8_t[]> data = util::Copy(buffer);

    ResStringPool test;
    ASSERT_EQ(test.setTo(data.get(), buffer.size()), NO_ERROR);

    EXPECT_THAT(util::GetString(test, 1), Eq("hello"));
    EXPECT_THAT(util::GetString16(test, 1), Eq(u"hello"));

    EXPECT_THAT(util::GetString(test, 2), Eq("goodbye"));
    EXPECT_THAT(util::GetString16(test, 2), Eq(u"goodbye"));

    EXPECT_THAT(util::GetString(test, 3), Eq(sLongString));
    EXPECT_THAT(util::GetString16(test, 3), Eq(util::Utf8ToUtf16(sLongString)));

    size_t len;
    EXPECT_TRUE(test.stringAt(4, &len) != nullptr || test.string8At(4, &len) != nullptr);

    EXPECT_THAT(util::GetString(test, 0), Eq("style"));
    EXPECT_THAT(util::GetString16(test, 0), Eq(u"style"));

    const ResStringPool_span* span = test.styleAt(0);
    ASSERT_THAT(span, NotNull());
    EXPECT_THAT(util::GetString(test, span->name.index), Eq("b"));
    EXPECT_THAT(util::GetString16(test, span->name.index), Eq(u"b"));
    EXPECT_THAT(span->firstChar, Eq(0u));
    EXPECT_THAT(span->lastChar, Eq(1u));
    span++;

    ASSERT_THAT(span->name.index, Ne(ResStringPool_span::END));
    EXPECT_THAT(util::GetString(test, span->name.index), Eq("i"));
    EXPECT_THAT(util::GetString16(test, span->name.index), Eq(u"i"));
    EXPECT_THAT(span->firstChar, Eq(2u));
    EXPECT_THAT(span->lastChar, Eq(3u));
    span++;

    EXPECT_THAT(span->name.index, Eq(ResStringPool_span::END));
  }
}

}  // namespace aapt
