Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "chromeos/dbus/ibus/ibus_util.h" | |
| 6 | |
| 7 #include <fstream> | |
| 8 | |
| 9 #include "base/environment.h" | |
| 10 #include "base/file_util.h" | |
| 11 #include "base/string_util.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/scoped_temp_dir.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 namespace chromeos{ | |
| 17 | |
| 18 namespace { | |
| 19 const char kIBusAddressFileEnvKey[] = "IBUS_ADDRESS_FILE"; | |
| 20 | |
| 21 void CreateTextFile(const FilePath& filepath, std::string contents) { | |
|
satorux1
2012/04/20 23:18:40
function comment is missing.
Seigo Nonaka
2012/04/23 23:03:21
Okay but I removed this file.
Thanks
On 2012/04/20
| |
| 22 std::ofstream file; | |
|
satorux1
2012/04/20 23:18:40
don't use this. please just use file_util::WriteFi
Seigo Nonaka
2012/04/23 23:03:21
Okay but I removed this file.
Thanks
On 2012/04/2
| |
| 23 file.open(filepath.value().c_str()); | |
| 24 ASSERT_TRUE(file.is_open()); | |
| 25 file << contents; | |
| 26 file.close(); | |
| 27 } | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 class IBusUtilTest : public testing::Test { | |
| 32 protected: | |
| 33 virtual void SetUp() OVERRIDE { | |
| 34 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 35 env_.reset(base::Environment::Create()); | |
| 36 env_->GetVar(kIBusAddressFileEnvKey, &old_address_file_entry_); | |
| 37 } | |
| 38 | |
| 39 virtual void TearDown() OVERRIDE { | |
| 40 env_->SetVar(kIBusAddressFileEnvKey, old_address_file_entry_); | |
| 41 } | |
| 42 | |
| 43 | |
| 44 ScopedTempDir temp_dir_; | |
| 45 scoped_ptr<base::Environment> env_; | |
| 46 std::string old_address_file_entry_; | |
| 47 }; | |
| 48 | |
| 49 TEST_F(IBusUtilTest, GetIBusAddressTest) { | |
| 50 { | |
| 51 SCOPED_TRACE("Missing environment variables test"); | |
| 52 env_->UnSetVar(kIBusAddressFileEnvKey); | |
| 53 std::string address; | |
| 54 EXPECT_FALSE(IBusUtil::GetIBusAddress(&address)); | |
| 55 } | |
| 56 { | |
|
satorux1
2012/04/20 23:18:40
instead of having many scopes in a single test fun
Seigo Nonaka
2012/04/23 23:03:21
Okay but I removed this file.
Thanks
On 2012/04/2
| |
| 57 SCOPED_TRACE("Not existing target file test"); | |
| 58 FilePath tmp_file_path = | |
| 59 temp_dir_.path().Append(FILE_PATH_LITERAL("ghost_file")); | |
| 60 env_->SetVar(kIBusAddressFileEnvKey, tmp_file_path.value()); | |
| 61 std::string address; | |
| 62 EXPECT_FALSE(IBusUtil::GetIBusAddress(&address)); | |
| 63 } | |
| 64 { | |
| 65 SCOPED_TRACE("Does not have entry test"); | |
| 66 FilePath tmp_file_path = | |
| 67 temp_dir_.path().Append(FILE_PATH_LITERAL("empty_file")); | |
| 68 env_->SetVar(kIBusAddressFileEnvKey, tmp_file_path.value()); | |
| 69 CreateTextFile(tmp_file_path, ""); | |
| 70 std::string address; | |
| 71 EXPECT_FALSE(IBusUtil::GetIBusAddress(&address)); | |
| 72 EXPECT_TRUE(file_util::Delete(tmp_file_path, false)); | |
| 73 } | |
| 74 { | |
| 75 FilePath tmp_file_path = | |
| 76 temp_dir_.path().Append(FILE_PATH_LITERAL("address_file")); | |
| 77 env_->SetVar(kIBusAddressFileEnvKey, tmp_file_path.value()); | |
| 78 | |
| 79 const std::string expected_path_name = "path=to:ibus_daemon;"; | |
| 80 std::vector<std::string> file_contents_lines; | |
| 81 file_contents_lines.push_back("Should be ignored line 1"); | |
| 82 file_contents_lines.push_back("IBUS_ADDRESS=" + expected_path_name); | |
| 83 file_contents_lines.push_back("IBUS_NONSENSE_VALUE=NON_SENSE_VALUE"); | |
| 84 file_contents_lines.push_back("Should be ignored line 2"); | |
| 85 CreateTextFile(tmp_file_path, JoinString(file_contents_lines, '\n')); | |
| 86 | |
| 87 std::string address; | |
| 88 EXPECT_TRUE(IBusUtil::GetIBusAddress(&address)); | |
| 89 EXPECT_EQ(expected_path_name, address); | |
| 90 EXPECT_TRUE(file_util::Delete(tmp_file_path, false)); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 } // namespace chromeos | |
| OLD | NEW |