diff options
author | slan <slan@chromium.org> | 2015-06-30 14:40:11 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-06-30 21:41:51 +0000 |
commit | 77d76acfa1fdb7054a371eb34d7aa70d9d13e07d (patch) | |
tree | 32535d06c350fad911a18ec00e439f9eccdff85c /chromecast | |
parent | 5a1ea839a4e82c92f40f57a83ae845d559e63619 (diff) | |
download | chromium_src-77d76acfa1fdb7054a371eb34d7aa70d9d13e07d.zip chromium_src-77d76acfa1fdb7054a371eb34d7aa70d9d13e07d.tar.gz chromium_src-77d76acfa1fdb7054a371eb34d7aa70d9d13e07d.tar.bz2 |
Add unittests for chromecast/base/error_codes.h.
Also fixes bug resulting from const& to temporary object in
chromecast/base/error_codes.cc.
BUG= b/22201989
Review URL: https://codereview.chromium.org/1214263003
Cr-Commit-Position: refs/heads/master@{#336880}
Diffstat (limited to 'chromecast')
-rw-r--r-- | chromecast/base/BUILD.gn | 1 | ||||
-rw-r--r-- | chromecast/base/error_codes.cc | 2 | ||||
-rw-r--r-- | chromecast/base/error_codes_unittest.cc | 65 | ||||
-rw-r--r-- | chromecast/chromecast_tests.gypi | 1 |
4 files changed, 68 insertions, 1 deletions
diff --git a/chromecast/base/BUILD.gn b/chromecast/base/BUILD.gn index 09ad4ce..549fee2 100644 --- a/chromecast/base/BUILD.gn +++ b/chromecast/base/BUILD.gn @@ -35,6 +35,7 @@ source_set("base") { test("cast_base_unittests") { sources = [ + "error_codes_unittest.cc", "path_utils_unittest.cc", "process_utils_unittest.cc", "serializers_unittest.cc", diff --git a/chromecast/base/error_codes.cc b/chromecast/base/error_codes.cc index 7cd3551..e26a820 100644 --- a/chromecast/base/error_codes.cc +++ b/chromecast/base/error_codes.cc @@ -47,7 +47,7 @@ ErrorCode GetInitialErrorCode() { bool SetInitialErrorCode(ErrorCode initial_error_code) { // Note: Do not use Chromium IO methods in this function. When cast_shell // crashes, this function can be called by any thread. - const std::string& error_file_path = GetInitialErrorFilePath().value(); + const std::string error_file_path = GetInitialErrorFilePath().value(); if (initial_error_code > NO_ERROR && initial_error_code <= ERROR_UNKNOWN) { const std::string initial_error_code_str( diff --git a/chromecast/base/error_codes_unittest.cc b/chromecast/base/error_codes_unittest.cc new file mode 100644 index 0000000..293d895 --- /dev/null +++ b/chromecast/base/error_codes_unittest.cc @@ -0,0 +1,65 @@ +// 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 "base/base_paths.h" +#include "base/files/file_path.h" +#include "base/files/file_util.h" +#include "base/memory/scoped_ptr.h" +#include "base/test/scoped_path_override.h" +#include "chromecast/base/error_codes.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace chromecast { + +class ErrorCodesTest : public testing::Test { + protected: + ErrorCodesTest() {} + ~ErrorCodesTest() override {} + + void SetUp() override { + // Set up a temporary directory which will be used as our fake home dir. + ASSERT_TRUE(base::CreateNewTempDirectory("", &fake_home_dir_)); + path_override_.reset( + new base::ScopedPathOverride(base::DIR_HOME, fake_home_dir_)); + } + + base::FilePath fake_home_dir_; + scoped_ptr<base::ScopedPathOverride> path_override_; +}; + +TEST_F(ErrorCodesTest, GetInitialErrorCodeReturnsNoErrorIfMissingFile) { + EXPECT_EQ(NO_ERROR, GetInitialErrorCode()); +} + +TEST_F(ErrorCodesTest, SetInitialErrorCodeSucceedsWithNoError) { + ASSERT_TRUE(SetInitialErrorCode(NO_ERROR)); + + // File should not be written. + ASSERT_FALSE(base::PathExists(fake_home_dir_.Append("initial_error"))); + EXPECT_EQ(NO_ERROR, GetInitialErrorCode()); +} + +TEST_F(ErrorCodesTest, SetInitialErrorCodeSucceedsWithValidErrors) { + // Write initial error and read it from the file. + EXPECT_TRUE(SetInitialErrorCode(ERROR_WEB_CONTENT_RENDER_VIEW_GONE)); + EXPECT_TRUE(base::PathExists(fake_home_dir_.Append("initial_error"))); + EXPECT_EQ(ERROR_WEB_CONTENT_RENDER_VIEW_GONE, GetInitialErrorCode()); + + // File should be updated with most recent error. + EXPECT_TRUE(SetInitialErrorCode(ERROR_UNKNOWN)); + EXPECT_TRUE(base::PathExists(fake_home_dir_.Append("initial_error"))); + EXPECT_EQ(ERROR_UNKNOWN, GetInitialErrorCode()); + + // File should be updated with most recent error. + EXPECT_TRUE(SetInitialErrorCode(ERROR_WEB_CONTENT_NAME_NOT_RESOLVED)); + EXPECT_TRUE(base::PathExists(fake_home_dir_.Append("initial_error"))); + EXPECT_EQ(ERROR_WEB_CONTENT_NAME_NOT_RESOLVED, GetInitialErrorCode()); + + // File should be removed after writing NO_ERROR. + EXPECT_TRUE(SetInitialErrorCode(NO_ERROR)); + EXPECT_FALSE(base::PathExists(fake_home_dir_.Append("initial_error"))); + EXPECT_EQ(NO_ERROR, GetInitialErrorCode()); +} + +} // namespace chromecast diff --git a/chromecast/chromecast_tests.gypi b/chromecast/chromecast_tests.gypi index 6f79b0f..2df61ab0 100644 --- a/chromecast/chromecast_tests.gypi +++ b/chromecast/chromecast_tests.gypi @@ -16,6 +16,7 @@ '../testing/gtest.gyp:gtest', ], 'sources': [ + 'base/error_codes_unittest.cc', 'base/path_utils_unittest.cc', 'base/process_utils_unittest.cc', 'base/serializers_unittest.cc', |