error_codes_unittest.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "chromecast/base/error_codes.h"
  5. #include <memory>
  6. #include "base/base_paths.h"
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/files/scoped_temp_dir.h"
  10. #include "base/test/scoped_path_override.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace chromecast {
  13. class ErrorCodesTest : public testing::Test {
  14. protected:
  15. ErrorCodesTest() {}
  16. ~ErrorCodesTest() override {}
  17. void SetUp() override {
  18. // Set up a temporary directory which will be used as our fake home dir.
  19. ASSERT_TRUE(fake_home_dir_.CreateUniqueTempDir());
  20. path_override_.reset(
  21. new base::ScopedPathOverride(base::DIR_HOME, fake_home_dir_.GetPath()));
  22. }
  23. base::FilePath home_path() const { return fake_home_dir_.GetPath(); }
  24. private:
  25. base::ScopedTempDir fake_home_dir_;
  26. std::unique_ptr<base::ScopedPathOverride> path_override_;
  27. };
  28. TEST_F(ErrorCodesTest, GetInitialErrorCodeReturnsNoErrorIfMissingFile) {
  29. EXPECT_EQ(NO_ERROR, GetInitialErrorCode());
  30. }
  31. TEST_F(ErrorCodesTest, SetInitialErrorCodeSucceedsWithNoError) {
  32. ASSERT_TRUE(SetInitialErrorCode(NO_ERROR));
  33. // File should not be written.
  34. ASSERT_FALSE(base::PathExists(home_path().Append("initial_error")));
  35. EXPECT_EQ(NO_ERROR, GetInitialErrorCode());
  36. }
  37. TEST_F(ErrorCodesTest, SetInitialErrorCodeSucceedsWithValidErrors) {
  38. // Write initial error and read it from the file.
  39. EXPECT_TRUE(SetInitialErrorCode(ERROR_WEB_CONTENT_RENDER_VIEW_GONE));
  40. EXPECT_TRUE(base::PathExists(home_path().Append("initial_error")));
  41. EXPECT_EQ(ERROR_WEB_CONTENT_RENDER_VIEW_GONE, GetInitialErrorCode());
  42. // File should be updated with most recent error.
  43. EXPECT_TRUE(SetInitialErrorCode(ERROR_UNKNOWN));
  44. EXPECT_TRUE(base::PathExists(home_path().Append("initial_error")));
  45. EXPECT_EQ(ERROR_UNKNOWN, GetInitialErrorCode());
  46. // File should be updated with most recent error.
  47. EXPECT_TRUE(SetInitialErrorCode(ERROR_WEB_CONTENT_NAME_NOT_RESOLVED));
  48. EXPECT_TRUE(base::PathExists(home_path().Append("initial_error")));
  49. EXPECT_EQ(ERROR_WEB_CONTENT_NAME_NOT_RESOLVED, GetInitialErrorCode());
  50. // File should be removed after writing NO_ERROR.
  51. EXPECT_TRUE(SetInitialErrorCode(NO_ERROR));
  52. EXPECT_FALSE(base::PathExists(home_path().Append("initial_error")));
  53. EXPECT_EQ(NO_ERROR, GetInitialErrorCode());
  54. }
  55. } // namespace chromecast