Index: chromeos/dbus/ibus/ibus_util_unittest.cc |
diff --git a/chromeos/dbus/ibus/ibus_util_unittest.cc b/chromeos/dbus/ibus/ibus_util_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..72ca0ffefac86658d22ac11faa8914cd0c4c1940 |
--- /dev/null |
+++ b/chromeos/dbus/ibus/ibus_util_unittest.cc |
@@ -0,0 +1,94 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chromeos/dbus/ibus/ibus_util.h" |
+ |
+#include <fstream> |
+ |
+#include "base/environment.h" |
+#include "base/file_util.h" |
+#include "base/string_util.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/scoped_temp_dir.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace chromeos{ |
+ |
+namespace { |
+const char kIBusAddressFileEnvKey[] = "IBUS_ADDRESS_FILE"; |
+ |
+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
|
+ 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
|
+ file.open(filepath.value().c_str()); |
+ ASSERT_TRUE(file.is_open()); |
+ file << contents; |
+ file.close(); |
+} |
+ |
+} // namespace |
+ |
+class IBusUtilTest : public testing::Test { |
+ protected: |
+ virtual void SetUp() OVERRIDE { |
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
+ env_.reset(base::Environment::Create()); |
+ env_->GetVar(kIBusAddressFileEnvKey, &old_address_file_entry_); |
+ } |
+ |
+ virtual void TearDown() OVERRIDE { |
+ env_->SetVar(kIBusAddressFileEnvKey, old_address_file_entry_); |
+ } |
+ |
+ |
+ ScopedTempDir temp_dir_; |
+ scoped_ptr<base::Environment> env_; |
+ std::string old_address_file_entry_; |
+}; |
+ |
+TEST_F(IBusUtilTest, GetIBusAddressTest) { |
+ { |
+ SCOPED_TRACE("Missing environment variables test"); |
+ env_->UnSetVar(kIBusAddressFileEnvKey); |
+ std::string address; |
+ EXPECT_FALSE(IBusUtil::GetIBusAddress(&address)); |
+ } |
+ { |
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
|
+ SCOPED_TRACE("Not existing target file test"); |
+ FilePath tmp_file_path = |
+ temp_dir_.path().Append(FILE_PATH_LITERAL("ghost_file")); |
+ env_->SetVar(kIBusAddressFileEnvKey, tmp_file_path.value()); |
+ std::string address; |
+ EXPECT_FALSE(IBusUtil::GetIBusAddress(&address)); |
+ } |
+ { |
+ SCOPED_TRACE("Does not have entry test"); |
+ FilePath tmp_file_path = |
+ temp_dir_.path().Append(FILE_PATH_LITERAL("empty_file")); |
+ env_->SetVar(kIBusAddressFileEnvKey, tmp_file_path.value()); |
+ CreateTextFile(tmp_file_path, ""); |
+ std::string address; |
+ EXPECT_FALSE(IBusUtil::GetIBusAddress(&address)); |
+ EXPECT_TRUE(file_util::Delete(tmp_file_path, false)); |
+ } |
+ { |
+ FilePath tmp_file_path = |
+ temp_dir_.path().Append(FILE_PATH_LITERAL("address_file")); |
+ env_->SetVar(kIBusAddressFileEnvKey, tmp_file_path.value()); |
+ |
+ const std::string expected_path_name = "path=to:ibus_daemon;"; |
+ std::vector<std::string> file_contents_lines; |
+ file_contents_lines.push_back("Should be ignored line 1"); |
+ file_contents_lines.push_back("IBUS_ADDRESS=" + expected_path_name); |
+ file_contents_lines.push_back("IBUS_NONSENSE_VALUE=NON_SENSE_VALUE"); |
+ file_contents_lines.push_back("Should be ignored line 2"); |
+ CreateTextFile(tmp_file_path, JoinString(file_contents_lines, '\n')); |
+ |
+ std::string address; |
+ EXPECT_TRUE(IBusUtil::GetIBusAddress(&address)); |
+ EXPECT_EQ(expected_path_name, address); |
+ EXPECT_TRUE(file_util::Delete(tmp_file_path, false)); |
+ } |
+} |
+ |
+} // namespace chromeos |