1 // Copyright (c) 2012 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 "chrome/browser/chromeos/extensions/default_app_order.h"
10 #include "base/file_util.h"
11 #include "base/files/file_path.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/path_service.h"
15 #include "base/test/scoped_path_override.h"
16 #include "chromeos/chromeos_paths.h"
17 #include "testing/gtest/include/gtest/gtest.h"
23 const base::FilePath::CharType kTestFile
[] =
24 FILE_PATH_LITERAL("test_default_app_order.json");
27 class DefaultAppOrderTest
: public testing::Test
{
29 DefaultAppOrderTest() {}
30 virtual ~DefaultAppOrderTest() {}
32 // testing::Test overrides:
33 virtual void SetUp() OVERRIDE
{
34 default_app_order::Get(&built_in_default_
);
36 virtual void TearDown() OVERRIDE
{
39 bool IsBuiltInDefault(const std::vector
<std::string
>& apps
) {
40 if (apps
.size() != built_in_default_
.size())
43 for (size_t i
= 0; i
< built_in_default_
.size(); ++i
) {
44 if (built_in_default_
[i
] != apps
[i
])
51 void SetExternalFile(const base::FilePath
& path
) {
52 path_override_
.reset(new base::ScopedPathOverride(
53 chromeos::FILE_DEFAULT_APP_ORDER
, path
));
56 void CreateExternalOrderFile(const std::string
& content
) {
57 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
58 base::FilePath external_file
= temp_dir_
.path().Append(kTestFile
);
59 file_util::WriteFile(external_file
, content
.c_str(), content
.size());
60 SetExternalFile(external_file
);
64 std::vector
<std::string
> built_in_default_
;
66 base::ScopedTempDir temp_dir_
;
67 scoped_ptr
<base::ScopedPathOverride
> path_override_
;
69 DISALLOW_COPY_AND_ASSIGN(DefaultAppOrderTest
);
72 // Tests that the built-in default order is returned when ExternalLoader is not
74 TEST_F(DefaultAppOrderTest
, BuiltInDefault
) {
75 std::vector
<std::string
> apps
;
76 default_app_order::Get(&apps
);
77 EXPECT_TRUE(IsBuiltInDefault(apps
));
80 // Tests external order file overrides built-in default.
81 TEST_F(DefaultAppOrderTest
, ExternalOrder
) {
82 const char kExternalOrder
[] = "[\"app1\",\"app2\",\"app3\"]";
83 CreateExternalOrderFile(std::string(kExternalOrder
));
85 scoped_ptr
<default_app_order::ExternalLoader
> loader(
86 new default_app_order::ExternalLoader(false));
88 std::vector
<std::string
> apps
;
89 default_app_order::Get(&apps
);
90 EXPECT_EQ(3u, apps
.size());
91 EXPECT_EQ(std::string("app1"), apps
[0]);
92 EXPECT_EQ(std::string("app2"), apps
[1]);
93 EXPECT_EQ(std::string("app3"), apps
[2]);
96 // Tests none-existent order file gives built-in default.
97 TEST_F(DefaultAppOrderTest
, NoExternalFile
) {
98 base::ScopedTempDir scoped_tmp_dir
;
99 ASSERT_TRUE(scoped_tmp_dir
.CreateUniqueTempDir());
101 base::FilePath none_existent_file
=
102 scoped_tmp_dir
.path().AppendASCII("none_existent_file");
103 ASSERT_FALSE(base::PathExists(none_existent_file
));
104 SetExternalFile(none_existent_file
);
106 scoped_ptr
<default_app_order::ExternalLoader
> loader(
107 new default_app_order::ExternalLoader(false));
109 std::vector
<std::string
> apps
;
110 default_app_order::Get(&apps
);
111 EXPECT_TRUE(IsBuiltInDefault(apps
));
114 // Tests bad json file gives built-in default.
115 TEST_F(DefaultAppOrderTest
, BadExternalFile
) {
116 const char kExternalOrder
[] = "This is not a valid json.";
117 CreateExternalOrderFile(std::string(kExternalOrder
));
119 scoped_ptr
<default_app_order::ExternalLoader
> loader(
120 new default_app_order::ExternalLoader(false));
122 std::vector
<std::string
> apps
;
123 default_app_order::Get(&apps
);
124 EXPECT_TRUE(IsBuiltInDefault(apps
));
127 } // namespace chromeos