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 "testing/gtest/include/gtest/gtest.h"
20 #include "testing/platform_test.h"
22 using component_updater::CldComponentInstallerTraits
;
25 // This has to match what's in cld_component_installer.cc.
26 const base::FilePath::CharType kTestCldDataFileName
[] =
27 FILE_PATH_LITERAL("cld2_data.bin");
31 namespace component_updater
{
33 class CldComponentInstallerTest
: public PlatformTest
{
35 CldComponentInstallerTest() {}
36 virtual void SetUp() override
{
37 PlatformTest::SetUp();
39 // ScopedTempDir automatically does a recursive delete on the entire
40 // directory in its destructor, so no cleanup is required in TearDown.
41 // Note that all files created by this test case are created within the
42 // directory that is created here, so even though they are not explicitly
43 // created *as temp files*, they will still get cleaned up automagically.
44 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
46 // The "latest CLD data file" is a static piece of information, and thus
47 // for correctness we empty it before each test.
48 CldComponentInstallerTraits::SetLatestCldDataFile(base::FilePath());
49 base::FilePath path_now
=
50 CldComponentInstallerTraits::GetLatestCldDataFile();
51 ASSERT_TRUE(path_now
.empty());
55 base::ScopedTempDir temp_dir_
;
56 CldComponentInstallerTraits traits_
;
59 DISALLOW_COPY_AND_ASSIGN(CldComponentInstallerTest
);
62 TEST_F(CldComponentInstallerTest
, SetLatestCldDataFile
) {
63 const base::FilePath
expected(FILE_PATH_LITERAL("test/foo.test"));
64 CldComponentInstallerTraits::SetLatestCldDataFile(expected
);
65 base::FilePath result
=
66 CldComponentInstallerTraits::GetLatestCldDataFile();
67 ASSERT_EQ(expected
, result
);
70 TEST_F(CldComponentInstallerTest
, VerifyInstallation
) {
71 // All files are created within a ScopedTempDir, which deletes all
72 // children when its destructor is called (at the end of each test).
73 const base::DictionaryValue manifest
;
74 ASSERT_FALSE(traits_
.VerifyInstallation(manifest
, temp_dir_
.path()));
75 const base::FilePath data_file_dir
=
76 temp_dir_
.path().Append(FILE_PATH_LITERAL("_platform_specific")).Append(
77 FILE_PATH_LITERAL("all"));
78 ASSERT_TRUE(base::CreateDirectory(data_file_dir
));
79 const base::FilePath data_file
= data_file_dir
.Append(kTestCldDataFileName
);
80 const std::string
test_data("fake cld2 data file content here :)");
81 ASSERT_EQ(static_cast<int32
>(test_data
.length()),
82 base::WriteFile(data_file
, test_data
.c_str(), test_data
.length()));
83 ASSERT_TRUE(traits_
.VerifyInstallation(manifest
, temp_dir_
.path()));
86 TEST_F(CldComponentInstallerTest
, OnCustomInstall
) {
87 const base::DictionaryValue manifest
;
88 const base::FilePath install_dir
;
89 // Sanity: shouldn't crash.
90 ASSERT_TRUE(traits_
.OnCustomInstall(manifest
, install_dir
));
93 TEST_F(CldComponentInstallerTest
, GetInstalledPath
) {
94 const base::FilePath base_dir
;
95 const base::FilePath result
=
96 CldComponentInstallerTraits::GetInstalledPath(base_dir
);
97 ASSERT_TRUE(EndsWith(result
.value(), kTestCldDataFileName
, true));
100 TEST_F(CldComponentInstallerTest
, GetBaseDirectory
) {
101 const base::FilePath result
= traits_
.GetBaseDirectory();
102 ASSERT_FALSE(result
.empty());
105 TEST_F(CldComponentInstallerTest
, GetHash
) {
106 std::vector
<uint8_t> hash
;
107 traits_
.GetHash(&hash
);
108 ASSERT_EQ(static_cast<size_t>(32), hash
.size());
111 TEST_F(CldComponentInstallerTest
, GetName
) {
112 ASSERT_FALSE(traits_
.GetName().empty());
115 TEST_F(CldComponentInstallerTest
, ComponentReady
) {
116 scoped_ptr
<base::DictionaryValue
> manifest
;
117 const base::FilePath
install_dir(FILE_PATH_LITERAL("/foo"));
118 const base::Version
version("1.2.3.4");
119 traits_
.ComponentReady(version
, install_dir
, manifest
.Pass());
120 base::FilePath result
=
121 CldComponentInstallerTraits::GetLatestCldDataFile();
122 ASSERT_TRUE(StartsWith(result
.AsUTF16Unsafe(),
123 install_dir
.AsUTF16Unsafe(),
125 ASSERT_TRUE(EndsWith(result
.value(), kTestCldDataFileName
, true));
128 } // namespace component_updater