cld_component_installer_unittest.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file. 4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <vector> 6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/file_util.h" 8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/files/file_path.h" 9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/files/scoped_temp_dir.h" 10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/macros.h" 11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/strings/string_util.h" 13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/strings/utf_string_conversions.h" 14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/values.h" 15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/version.h" 16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/component_updater/cld_component_installer.h" 17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/translate/content/browser/data_file_browser_cld_data_provider.h" 18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "testing/platform_test.h" 20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 21116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace { 22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This has to match what's in cld_component_installer.cc. 23116680a4aac90f2aa7413d9095a592090648e557Ben Murdochconst base::FilePath::CharType kTestCldDataFileName[] = 24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch FILE_PATH_LITERAL("cld2_data.bin"); 25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} // namespace 26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace component_updater { 28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class CldComponentInstallerTest : public PlatformTest { 30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public: 31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch CldComponentInstallerTest() {} 32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) virtual void SetUp() OVERRIDE { 33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) PlatformTest::SetUp(); 34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // ScopedTempDir automatically does a recursive delete on the entire 36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // directory in its destructor, so no cleanup is required in TearDown. 37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // Note that all files created by this test case are created within the 38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // directory that is created here, so even though they are not explicitly 39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // created *as temp files*, they will still get cleaned up automagically. 40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // The "latest CLD data file" is a static piece of information, and thus 43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // for correctness we empty it before each test. 44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( 45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::FilePath()); 46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::FilePath path_now = 47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch translate::DataFileBrowserCldDataProvider::GetCldDataFilePath(); 48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_TRUE(path_now.empty()); 49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch protected: 52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::ScopedTempDir temp_dir_; 53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch component_updater::CldComponentInstallerTraits traits_; 54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch private: 56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch DISALLOW_COPY_AND_ASSIGN(CldComponentInstallerTest); 57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}; 58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, SetLatestCldDataFile) { 60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::FilePath expected(FILE_PATH_LITERAL("test/foo.test")); 61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch component_updater::CldComponentInstallerTraits::SetLatestCldDataFile( 62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch expected); 63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::FilePath result = 64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch translate::DataFileBrowserCldDataProvider::GetCldDataFilePath(); 65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_EQ(expected, result); 66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, VerifyInstallation) { 69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // All files are created within a ScopedTempDir, which deletes all 70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // children when its destructor is called (at the end of each test). 71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_FALSE(traits_.VerifyInstallation(temp_dir_.path())); 72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::FilePath data_file_dir = 73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) temp_dir_.path().Append(FILE_PATH_LITERAL("_platform_specific")).Append( 74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) FILE_PATH_LITERAL("all")); 75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_TRUE(base::CreateDirectory(data_file_dir)); 76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const base::FilePath data_file = data_file_dir.Append(kTestCldDataFileName); 77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const std::string test_data("fake cld2 data file content here :)"); 78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_EQ(static_cast<int32>(test_data.length()), 79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::WriteFile(data_file, test_data.c_str(), test_data.length())); 80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_TRUE(traits_.VerifyInstallation(temp_dir_.path())); 81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, OnCustomInstall) { 84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::DictionaryValue manifest; 85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::FilePath install_dir; 86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // Sanity: shouldn't crash. 87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_TRUE(traits_.OnCustomInstall(manifest, install_dir)); 88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, GetInstalledPath) { 91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::FilePath base_dir; 92cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::FilePath result = 93cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) CldComponentInstallerTraits::GetInstalledPath(base_dir); 94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_TRUE(EndsWith(result.value(), kTestCldDataFileName, true)); 95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 96cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, GetBaseDirectory) { 98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const base::FilePath result = traits_.GetBaseDirectory(); 99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_FALSE(result.empty()); 100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, GetHash) { 103cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) std::vector<uint8> hash; 104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch traits_.GetHash(&hash); 105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_EQ(static_cast<size_t>(32), hash.size()); 106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, GetName) { 109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_FALSE(traits_.GetName().empty()); 110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 111cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, ComponentReady) { 113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) scoped_ptr<base::DictionaryValue> manifest; 114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::FilePath install_dir(FILE_PATH_LITERAL("/foo")); 115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::Version version("1.2.3.4"); 116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch traits_.ComponentReady(version, install_dir, manifest.Pass()); 117116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::FilePath result = 118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch translate::DataFileBrowserCldDataProvider::GetCldDataFilePath(); 119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_TRUE(StartsWith(result.AsUTF16Unsafe(), 120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) install_dir.AsUTF16Unsafe(), 121cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) true)); 122116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_TRUE(EndsWith(result.value(), kTestCldDataFileName, true)); 123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 124cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} // namespace component_updater 126