1 // Copyright 2014 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.
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "base/version.h"
17 #include "chrome/browser/component_updater/cld_component_installer.h"
18 #include "components/translate/content/browser/browser_cld_data_provider.h"
19 #include "components/translate/content/common/cld_data_source.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "testing/platform_test.h"
23 using component_updater::CldComponentInstallerTraits
;
27 // This has to match what's in cld_component_installer.cc.
28 const base::FilePath::CharType kTestCldDataFileName
[] =
29 FILE_PATH_LITERAL("cld2_data.bin");
33 namespace component_updater
{
35 class CldComponentInstallerTest
: public PlatformTest
{
37 CldComponentInstallerTest() {}
38 void SetUp() override
{
39 PlatformTest::SetUp();
40 translate::CldDataSource::DisableSanityChecksForTest();
42 // ScopedTempDir automatically does a recursive delete on the entire
43 // directory in its destructor, so no cleanup is required in TearDown.
44 // Note that all files created by this test case are created within the
45 // directory that is created here, so even though they are not explicitly
46 // created *as temp files*, they will still get cleaned up automagically.
47 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
49 // The "latest CLD data file" is a static piece of information, and thus
50 // for correctness we empty it before each test.
51 CldComponentInstallerTraits::SetLatestCldDataFile(base::FilePath());
52 base::FilePath path_now
=
53 CldComponentInstallerTraits::GetLatestCldDataFile();
54 ASSERT_TRUE(path_now
.empty());
57 void TearDown() override
{
58 // Restore sanity checks.
59 translate::CldDataSource::EnableSanityChecksForTest();
63 base::ScopedTempDir temp_dir_
;
64 CldComponentInstallerTraits traits_
;
67 DISALLOW_COPY_AND_ASSIGN(CldComponentInstallerTest
);
70 TEST_F(CldComponentInstallerTest
, SetLatestCldDataFile
) {
71 const base::FilePath
expected(FILE_PATH_LITERAL("test/foo.test"));
72 CldComponentInstallerTraits::SetLatestCldDataFile(expected
);
73 base::FilePath result
= CldComponentInstallerTraits::GetLatestCldDataFile();
74 ASSERT_EQ(expected
, result
);
77 TEST_F(CldComponentInstallerTest
, VerifyInstallation
) {
78 // All files are created within a ScopedTempDir, which deletes all
79 // children when its destructor is called (at the end of each test).
80 const base::DictionaryValue manifest
;
81 ASSERT_FALSE(traits_
.VerifyInstallation(manifest
, temp_dir_
.path()));
82 const base::FilePath data_file_dir
=
84 .Append(FILE_PATH_LITERAL("_platform_specific"))
85 .Append(FILE_PATH_LITERAL("all"));
86 ASSERT_TRUE(base::CreateDirectory(data_file_dir
));
87 const base::FilePath data_file
= data_file_dir
.Append(kTestCldDataFileName
);
88 const std::string
test_data("fake cld2 data file content here :)");
89 ASSERT_EQ(static_cast<int32
>(test_data
.length()),
90 base::WriteFile(data_file
, test_data
.c_str(), test_data
.length()));
91 ASSERT_TRUE(traits_
.VerifyInstallation(manifest
, temp_dir_
.path()));
94 TEST_F(CldComponentInstallerTest
, OnCustomInstall
) {
95 const base::DictionaryValue manifest
;
96 const base::FilePath install_dir
;
97 // Sanity: shouldn't crash.
98 ASSERT_TRUE(traits_
.OnCustomInstall(manifest
, install_dir
));
101 TEST_F(CldComponentInstallerTest
, GetInstalledPath
) {
102 const base::FilePath base_dir
;
103 const base::FilePath result
=
104 CldComponentInstallerTraits::GetInstalledPath(base_dir
);
105 ASSERT_TRUE(EndsWith(result
.value(), kTestCldDataFileName
, true));
108 TEST_F(CldComponentInstallerTest
, GetBaseDirectory
) {
109 const base::FilePath result
= traits_
.GetBaseDirectory();
110 ASSERT_FALSE(result
.empty());
113 TEST_F(CldComponentInstallerTest
, GetHash
) {
114 std::vector
<uint8_t> hash
;
115 traits_
.GetHash(&hash
);
116 ASSERT_EQ(static_cast<size_t>(32), hash
.size());
119 TEST_F(CldComponentInstallerTest
, GetName
) {
120 ASSERT_FALSE(traits_
.GetName().empty());
123 TEST_F(CldComponentInstallerTest
, ComponentReady
) {
124 scoped_ptr
<base::DictionaryValue
> manifest
;
125 const base::FilePath
install_dir(FILE_PATH_LITERAL("/foo"));
126 const base::Version
version("1.2.3.4");
127 traits_
.ComponentReady(version
, install_dir
, manifest
.Pass());
128 base::FilePath result
= CldComponentInstallerTraits::GetLatestCldDataFile();
130 StartsWith(result
.AsUTF16Unsafe(), install_dir
.AsUTF16Unsafe(), true));
131 ASSERT_TRUE(EndsWith(result
.value(), kTestCldDataFileName
, true));
134 } // namespace component_updater