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.
5 #include "base/base_paths.h"
6 #include "base/files/file_path.h"
7 #include "base/files/file_util.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/test/scoped_path_override.h"
10 #include "chromecast/base/error_codes.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace chromecast
{
15 class ErrorCodesTest
: public testing::Test
{
18 ~ErrorCodesTest() override
{}
20 void SetUp() override
{
21 // Set up a temporary directory which will be used as our fake home dir.
22 ASSERT_TRUE(base::CreateNewTempDirectory("", &fake_home_dir_
));
24 new base::ScopedPathOverride(base::DIR_HOME
, fake_home_dir_
));
27 base::FilePath fake_home_dir_
;
28 scoped_ptr
<base::ScopedPathOverride
> path_override_
;
31 TEST_F(ErrorCodesTest
, GetInitialErrorCodeReturnsNoErrorIfMissingFile
) {
32 EXPECT_EQ(NO_ERROR
, GetInitialErrorCode());
35 TEST_F(ErrorCodesTest
, SetInitialErrorCodeSucceedsWithNoError
) {
36 ASSERT_TRUE(SetInitialErrorCode(NO_ERROR
));
38 // File should not be written.
39 ASSERT_FALSE(base::PathExists(fake_home_dir_
.Append("initial_error")));
40 EXPECT_EQ(NO_ERROR
, GetInitialErrorCode());
43 TEST_F(ErrorCodesTest
, SetInitialErrorCodeSucceedsWithValidErrors
) {
44 // Write initial error and read it from the file.
45 EXPECT_TRUE(SetInitialErrorCode(ERROR_WEB_CONTENT_RENDER_VIEW_GONE
));
46 EXPECT_TRUE(base::PathExists(fake_home_dir_
.Append("initial_error")));
47 EXPECT_EQ(ERROR_WEB_CONTENT_RENDER_VIEW_GONE
, GetInitialErrorCode());
49 // File should be updated with most recent error.
50 EXPECT_TRUE(SetInitialErrorCode(ERROR_UNKNOWN
));
51 EXPECT_TRUE(base::PathExists(fake_home_dir_
.Append("initial_error")));
52 EXPECT_EQ(ERROR_UNKNOWN
, GetInitialErrorCode());
54 // File should be updated with most recent error.
55 EXPECT_TRUE(SetInitialErrorCode(ERROR_WEB_CONTENT_NAME_NOT_RESOLVED
));
56 EXPECT_TRUE(base::PathExists(fake_home_dir_
.Append("initial_error")));
57 EXPECT_EQ(ERROR_WEB_CONTENT_NAME_NOT_RESOLVED
, GetInitialErrorCode());
59 // File should be removed after writing NO_ERROR.
60 EXPECT_TRUE(SetInitialErrorCode(NO_ERROR
));
61 EXPECT_FALSE(base::PathExists(fake_home_dir_
.Append("initial_error")));
62 EXPECT_EQ(NO_ERROR
, GetInitialErrorCode());
65 } // namespace chromecast