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