Commit fde41d98 authored by Sebastien's avatar Sebastien Committed by Commit Bot

Added utility functions for parsing HighlightedGamesResponse.

Bug: 1018201
Change-Id: I7848d39c74e17a8c397847c0aa96e8612bc14669
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1933588
Commit-Queue: Sebastien Lalancette <seblalancette@chromium.org>
Reviewed-by: default avatarChris Sharp <csharp@chromium.org>
Cr-Commit-Position: refs/heads/master@{#720721}
parent f4f18095
......@@ -11,7 +11,22 @@ namespace games {
namespace {
const size_t kMaxCatalogSizeInBytes = 1024 * 1024; // 1 MB
const size_t kMaxFileSizeInBytes = 1024 * 1024; // 1 MB
bool ParseProtoFromFile(const base::FilePath& file_path,
google::protobuf::MessageLite* out_proto) {
if (!out_proto) {
return false;
}
std::string file_content;
if (!base::ReadFileToStringWithMaxSize(file_path, &file_content,
kMaxFileSizeInBytes)) {
return false;
}
return out_proto->ParseFromString(file_content);
}
} // namespace
......@@ -21,16 +36,13 @@ DataFilesParser::~DataFilesParser() = default;
bool DataFilesParser::TryParseCatalog(const base::FilePath& install_dir,
GamesCatalog* out_catalog) {
const auto catalog_path = GetGamesCatalogPath(install_dir);
std::string file_content;
if (!base::ReadFileToStringWithMaxSize(catalog_path, &file_content,
kMaxCatalogSizeInBytes)) {
return false;
}
return ParseProtoFromFile(GetGamesCatalogPath(install_dir), out_catalog);
}
out_catalog->ParseFromString(file_content);
return true;
bool DataFilesParser::TryParseHighlightedGames(
const base::FilePath& install_dir,
HighlightedGamesResponse* out_response) {
return ParseProtoFromFile(GetHighlightedGamesPath(install_dir), out_response);
}
} // namespace games
......@@ -7,6 +7,7 @@
#include "base/files/file_path.h"
#include "components/games/core/proto/games_catalog.pb.h"
#include "components/games/core/proto/highlighted_games.pb.h"
namespace games {
......@@ -18,6 +19,9 @@ class DataFilesParser {
virtual bool TryParseCatalog(const base::FilePath& install_dir,
GamesCatalog* out_catalog);
virtual bool TryParseHighlightedGames(const base::FilePath& install_dir,
HighlightedGamesResponse* out_response);
DISALLOW_COPY_AND_ASSIGN(DataFilesParser);
};
......
......@@ -9,6 +9,7 @@
#include "base/files/scoped_temp_dir.h"
#include "components/games/core/games_utils.h"
#include "components/games/core/proto/games_catalog.pb.h"
#include "components/games/core/proto/highlighted_games.pb.h"
#include "components/games/core/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -18,6 +19,12 @@ class DataFilesParserTest : public testing::Test {
protected:
void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
void WriteStringToFile(const base::FilePath& file_path,
const std::string& string_data) {
ASSERT_NE(
-1, base::WriteFile(file_path, string_data.data(), string_data.size()));
}
DataFilesParser parser_;
base::ScopedTempDir temp_dir_;
};
......@@ -27,17 +34,58 @@ TEST_F(DataFilesParserTest, TryParseCatalog_FileDoesNotExist) {
EXPECT_FALSE(parser_.TryParseCatalog(temp_dir_.GetPath(), &catalog));
}
TEST_F(DataFilesParserTest, TryParseCatalog_NullOutPtr) {
EXPECT_FALSE(parser_.TryParseCatalog(temp_dir_.GetPath(), nullptr));
}
TEST_F(DataFilesParserTest, TryParseCatalog_BadProto) {
WriteStringToFile(GetGamesCatalogPath(temp_dir_.GetPath()),
"well something is odd");
GamesCatalog test_catalog;
EXPECT_FALSE(parser_.TryParseCatalog(temp_dir_.GetPath(), &test_catalog));
}
TEST_F(DataFilesParserTest, TryParseCatalog_Success) {
GamesCatalog expected_catalog = test::CreateGamesCatalogWithOneGame();
std::string serialized_proto = expected_catalog.SerializeAsString();
const auto catalog_path = GetGamesCatalogPath(temp_dir_.GetPath());
ASSERT_NE(-1, base::WriteFile(catalog_path, serialized_proto.data(),
serialized_proto.size()));
WriteStringToFile(GetGamesCatalogPath(temp_dir_.GetPath()),
expected_catalog.SerializeAsString());
GamesCatalog test_catalog;
EXPECT_TRUE(parser_.TryParseCatalog(temp_dir_.GetPath(), &test_catalog));
EXPECT_TRUE(test::AreProtosEqual(expected_catalog, test_catalog));
}
TEST_F(DataFilesParserTest, TryParseHighlightedGames_FileDoesNotExist) {
HighlightedGamesResponse highlighted_games;
EXPECT_FALSE(parser_.TryParseHighlightedGames(temp_dir_.GetPath(),
&highlighted_games));
}
TEST_F(DataFilesParserTest, TryParseHighlightedGames_NullOutPtr) {
EXPECT_FALSE(parser_.TryParseHighlightedGames(temp_dir_.GetPath(), nullptr));
}
TEST_F(DataFilesParserTest, TryParseHighlightedGames_BadData) {
GamesCatalog bad_proto = test::CreateGamesCatalogWithOneGame();
WriteStringToFile(GetHighlightedGamesPath(temp_dir_.GetPath()),
"well something is odd");
HighlightedGamesResponse test_response;
EXPECT_FALSE(
parser_.TryParseHighlightedGames(temp_dir_.GetPath(), &test_response));
}
TEST_F(DataFilesParserTest, TryParseHighlightedGames_Success) {
HighlightedGamesResponse expected_response =
test::CreateHighlightedGamesResponse();
WriteStringToFile(GetHighlightedGamesPath(temp_dir_.GetPath()),
expected_response.SerializeAsString());
HighlightedGamesResponse test_response;
EXPECT_TRUE(
parser_.TryParseHighlightedGames(temp_dir_.GetPath(), &test_response));
EXPECT_TRUE(test::AreProtosEqual(expected_response, test_response));
}
} // namespace games
......@@ -9,4 +9,7 @@ namespace games {
const base::FilePath::CharType kGamesCatalogFileName[] =
FILE_PATH_LITERAL("games-catalog.pb");
const base::FilePath::CharType kHighlightedGamesFileName[] =
FILE_PATH_LITERAL("highlighted-games.pb");
} // namespace games
......@@ -10,6 +10,7 @@
namespace games {
extern const base::FilePath::CharType kGamesCatalogFileName[];
extern const base::FilePath::CharType kHighlightedGamesFileName[];
} // namespace games
......
......@@ -10,4 +10,8 @@ const base::FilePath GetGamesCatalogPath(const base::FilePath& dir) {
return dir.Append(kGamesCatalogFileName);
}
const base::FilePath GetHighlightedGamesPath(const base::FilePath& dir) {
return dir.Append(kHighlightedGamesFileName);
}
} // namespace games
......@@ -12,6 +12,7 @@
namespace games {
const base::FilePath GetGamesCatalogPath(const base::FilePath& dir);
const base::FilePath GetHighlightedGamesPath(const base::FilePath& dir);
} // namespace games
......
......@@ -35,6 +35,26 @@ Game CreateGame(int id) {
return game;
}
Date CreateDate(int year, int month, int day) {
Date date;
date.set_year(year);
date.set_month(month);
date.set_day(day);
return date;
}
HighlightedGamesResponse CreateHighlightedGamesResponse() {
HighlightedGame highlighted_game;
highlighted_game.mutable_start_date()->CopyFrom(CreateDate(2019, 10, 10));
highlighted_game.mutable_end_date()->CopyFrom(CreateDate(2019, 10, 17));
highlighted_game.set_game_id(1);
HighlightedGamesResponse highlighted_games;
highlighted_games.mutable_games()->Add(std::move(highlighted_game));
return highlighted_games;
}
bool AreProtosEqual(const google::protobuf::MessageLite& lhs,
const google::protobuf::MessageLite& rhs) {
return lhs.SerializeAsString() == rhs.SerializeAsString();
......
......@@ -7,9 +7,11 @@
#include <vector>
#include "components/games/core/proto/date.pb.h"
#include "components/games/core/proto/game.pb.h"
#include "components/games/core/proto/game_image.pb.h"
#include "components/games/core/proto/games_catalog.pb.h"
#include "components/games/core/proto/highlighted_games.pb.h"
namespace games {
namespace test {
......@@ -20,6 +22,10 @@ GamesCatalog CreateGamesCatalogWithOneGame();
Game CreateGame(int id = 1);
Date CreateDate(int year, int month, int day);
HighlightedGamesResponse CreateHighlightedGamesResponse();
bool AreProtosEqual(const google::protobuf::MessageLite& lhs,
const google::protobuf::MessageLite& rhs);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment