OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2013 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 25 matching lines...) Expand all Loading... |
36 throw std::runtime_error("Failed writing to file " + ToUtf8String(filePath
)); | 36 throw std::runtime_error("Failed writing to file " + ToUtf8String(filePath
)); |
37 } | 37 } |
38 | 38 |
39 void RemoveLocale(const std::wstring& basePath, const std::wstring& locale) | 39 void RemoveLocale(const std::wstring& basePath, const std::wstring& locale) |
40 { | 40 { |
41 std::wstring filePath = basePath + locale + L".ini"; | 41 std::wstring filePath = basePath + locale + L".ini"; |
42 ::DeleteFileW(filePath.c_str()); | 42 ::DeleteFileW(filePath.c_str()); |
43 } | 43 } |
44 } | 44 } |
45 | 45 |
| 46 namespace |
| 47 { |
| 48 // See http://blogs.msdn.com/b/oldnewthing/archive/2004/10/25/247180.aspx |
| 49 EXTERN_C IMAGE_DOS_HEADER __ImageBase; |
| 50 } |
| 51 |
| 52 std::wstring GetDllDir() |
| 53 { |
| 54 std::vector<WCHAR> path(MAX_PATH); |
| 55 DWORD length = GetModuleFileNameW((HINSTANCE)&__ImageBase, &path[0], path.size
()); |
| 56 |
| 57 while (length == path.size()) |
| 58 { |
| 59 // Buffer too small, double buffer size |
| 60 path.resize(path.size() * 2); |
| 61 length = GetModuleFileNameW((HINSTANCE)&__ImageBase, &path[0], path.size()); |
| 62 } |
| 63 |
| 64 try |
| 65 { |
| 66 if (length == 0) |
| 67 throw std::runtime_error("Failed determining module path"); |
| 68 |
| 69 std::vector<WCHAR>::reverse_iterator it = std::find(path.rbegin(), path.rend
(), L'\\'); |
| 70 if (it == path.rend()) |
| 71 throw std::runtime_error("Unexpected plugin path, no backslash found"); |
| 72 |
| 73 return std::wstring(path.begin(), it.base()); |
| 74 } |
| 75 catch (const std::exception&) |
| 76 { |
| 77 return std::wstring(); |
| 78 } |
| 79 } |
| 80 |
46 class DictionaryTest : public ::testing::Test | 81 class DictionaryTest : public ::testing::Test |
47 { | 82 { |
48 protected: | 83 protected: |
49 std::wstring basePath; | 84 std::wstring basePath; |
50 | 85 |
51 void SetUp() | 86 void SetUp() |
52 { | 87 { |
53 basePath = GetDllDir() + L"locales\\"; | 88 basePath = GetDllDir() + L"locales\\"; |
54 ::CreateDirectoryW(basePath.c_str(), NULL); | 89 ::CreateDirectoryW(basePath.c_str(), NULL); |
55 | 90 |
(...skipping 11 matching lines...) Expand all Loading... |
67 | 102 |
68 RemoveLocale(basePath, L"en"); | 103 RemoveLocale(basePath, L"en"); |
69 RemoveLocale(basePath, L"es-ES"); | 104 RemoveLocale(basePath, L"es-ES"); |
70 RemoveLocale(basePath, L"ru"); | 105 RemoveLocale(basePath, L"ru"); |
71 ::RemoveDirectoryW(basePath.c_str()); | 106 ::RemoveDirectoryW(basePath.c_str()); |
72 } | 107 } |
73 }; | 108 }; |
74 | 109 |
75 TEST_F(DictionaryTest, DefaultLocale) | 110 TEST_F(DictionaryTest, DefaultLocale) |
76 { | 111 { |
77 Dictionary::Create(L"en"); | 112 Dictionary::Create(L"en", basePath); |
78 Dictionary* dict = Dictionary::GetInstance(); | 113 Dictionary* dict = Dictionary::GetInstance(); |
79 | 114 |
80 ASSERT_TRUE(dict); | 115 ASSERT_TRUE(dict); |
81 ASSERT_EQ(L"bar", dict->Lookup("general", "foo")); | 116 ASSERT_EQ(L"bar", dict->Lookup("general", "foo")); |
82 ASSERT_EQ(L"y", dict->Lookup("x", "x")); | 117 ASSERT_EQ(L"y", dict->Lookup("x", "x")); |
83 ASSERT_NE(L"bar", dict->Lookup("", "foo")); | 118 ASSERT_NE(L"bar", dict->Lookup("", "foo")); |
84 ASSERT_NE(L"bar", dict->Lookup("x", "foo")); | 119 ASSERT_NE(L"bar", dict->Lookup("x", "foo")); |
85 ASSERT_NE(L"bar", dict->Lookup("generalsomething", "foo")); | 120 ASSERT_NE(L"bar", dict->Lookup("generalsomething", "foo")); |
86 ASSERT_FALSE(dict->Lookup("foo", "bar").empty()); | 121 ASSERT_FALSE(dict->Lookup("foo", "bar").empty()); |
87 } | 122 } |
88 | 123 |
89 TEST_F(DictionaryTest, LocaleFallback) | 124 TEST_F(DictionaryTest, LocaleFallback) |
90 { | 125 { |
91 Dictionary::Create(L"es-ES"); | 126 Dictionary::Create(L"es-ES", basePath); |
92 Dictionary* dict = Dictionary::GetInstance(); | 127 Dictionary* dict = Dictionary::GetInstance(); |
93 | 128 |
94 ASSERT_TRUE(dict); | 129 ASSERT_TRUE(dict); |
95 ASSERT_EQ(L"esbar", dict->Lookup("general", "foo")); | 130 ASSERT_EQ(L"esbar", dict->Lookup("general", "foo")); |
96 ASSERT_EQ(L"y", dict->Lookup("x", "x")); | 131 ASSERT_EQ(L"y", dict->Lookup("x", "x")); |
97 } | 132 } |
98 | 133 |
99 TEST_F(DictionaryTest, LocaleFallbackShort) | 134 TEST_F(DictionaryTest, LocaleFallbackShort) |
100 { | 135 { |
101 Dictionary::Create(L"ru-RU"); | 136 Dictionary::Create(L"ru-RU", basePath); |
102 Dictionary* dict = Dictionary::GetInstance(); | 137 Dictionary* dict = Dictionary::GetInstance(); |
103 | 138 |
104 ASSERT_TRUE(dict); | 139 ASSERT_TRUE(dict); |
105 ASSERT_EQ(L"\u0442\u0435\u0441\u0442", dict->Lookup("general", "foo")); | 140 ASSERT_EQ(L"\u0442\u0435\u0441\u0442", dict->Lookup("general", "foo")); |
106 ASSERT_EQ(L"y", dict->Lookup("x", "x")); | 141 ASSERT_EQ(L"y", dict->Lookup("x", "x")); |
107 } | 142 } |
OLD | NEW |