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/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/test/scoped_path_override.h"
15 #include "chrome/common/extensions/extension_constants.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 ~DefaultAppOrderTest() override
{}
32 // testing::Test overrides:
33 void SetUp() override
{ default_app_order::Get(&built_in_default_
); }
34 void TearDown() override
{}
36 bool IsBuiltInDefault(const std::vector
<std::string
>& apps
) {
37 if (apps
.size() != built_in_default_
.size())
40 for (size_t i
= 0; i
< built_in_default_
.size(); ++i
) {
41 if (built_in_default_
[i
] != apps
[i
])
48 void SetExternalFile(const base::FilePath
& path
) {
49 path_override_
.reset(new base::ScopedPathOverride(
50 chromeos::FILE_DEFAULT_APP_ORDER
, path
));
53 void CreateExternalOrderFile(const std::string
& content
) {
54 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
55 base::FilePath external_file
= temp_dir_
.path().Append(kTestFile
);
56 base::WriteFile(external_file
, content
.c_str(), content
.size());
57 SetExternalFile(external_file
);
61 std::vector
<std::string
> built_in_default_
;
63 base::ScopedTempDir temp_dir_
;
64 scoped_ptr
<base::ScopedPathOverride
> path_override_
;
66 DISALLOW_COPY_AND_ASSIGN(DefaultAppOrderTest
);
69 // Tests that the built-in default order is returned when ExternalLoader is not
71 TEST_F(DefaultAppOrderTest
, BuiltInDefault
) {
72 std::vector
<std::string
> apps
;
73 default_app_order::Get(&apps
);
74 EXPECT_TRUE(IsBuiltInDefault(apps
));
77 // Tests external order file overrides built-in default.
78 TEST_F(DefaultAppOrderTest
, ExternalOrder
) {
79 const char kExternalOrder
[] = "[\"app1\",\"app2\",\"app3\","
80 "{ \"oem_apps_folder\": true,\"localized_content\": {"
81 " \"default\": {\"name\": \"OEM name\"}}}]";
82 CreateExternalOrderFile(std::string(kExternalOrder
));
84 scoped_ptr
<default_app_order::ExternalLoader
> loader(
85 new default_app_order::ExternalLoader(false));
87 std::vector
<std::string
> apps
;
88 default_app_order::Get(&apps
);
89 EXPECT_EQ(3u, apps
.size());
90 EXPECT_EQ(std::string("app1"), apps
[0]);
91 EXPECT_EQ(std::string("app2"), apps
[1]);
92 EXPECT_EQ(std::string("app3"), apps
[2]);
93 EXPECT_EQ(std::string("OEM name"), default_app_order::GetOemAppsFolderName());
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 TEST_F(DefaultAppOrderTest
, ImportDefault
) {
128 const char kExternalOrder
[] =
130 "{ \"import_default_order\": true }, \"app2\"]";
131 CreateExternalOrderFile(std::string(kExternalOrder
));
133 scoped_ptr
<default_app_order::ExternalLoader
> loader(
134 new default_app_order::ExternalLoader(false));
136 std::vector
<std::string
> apps
;
137 default_app_order::Get(&apps
);
138 EXPECT_EQ(default_app_order::kDefaultAppOrderCount
+ 2, apps
.size());
139 EXPECT_EQ(std::string("app1"), apps
[0]);
140 EXPECT_EQ(extension_misc::kChromeAppId
, apps
[1]);
141 EXPECT_EQ(std::string("app2"),
142 apps
[default_app_order::kDefaultAppOrderCount
+ 1]);
145 } // namespace chromeos