Index: test/FilterEngine.cpp |
=================================================================== |
--- a/test/FilterEngine.cpp |
+++ b/test/FilterEngine.cpp |
@@ -10,31 +10,73 @@ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* GNU General Public License for more details. |
* |
* You should have received a copy of the GNU General Public License |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
+#include <sstream> |
+ |
+#include "../src/Thread.h" |
#include "BaseJsTest.h" |
namespace |
{ |
typedef std::tr1::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; |
class VeryLazyFileSystem : public LazyFileSystem |
{ |
+ public: |
std::tr1::shared_ptr<std::istream> Read(const std::string& path) const |
{ |
std::string dummyData("# Adblock Plus preferences"); |
return std::tr1::shared_ptr<std::istream>(new std::istringstream(dummyData)); |
} |
}; |
+ class PrefTestFileSystem : public LazyFileSystem |
+ { |
+ public: |
+ std::string prefsContents; |
+ |
+ std::tr1::shared_ptr<std::istream> Read(const std::string& path) const |
+ { |
+ if (path == "prefs.json" && !prefsContents.empty()) |
+ return std::tr1::shared_ptr<std::istream>(new std::istringstream(prefsContents)); |
+ else |
Felix Dahlke
2013/06/05 10:50:52
No else necessary, there's a return above
|
+ return LazyFileSystem::Read(path); |
+ } |
+ |
+ void Write(const std::string& path, std::tr1::shared_ptr<std::istream> content) |
+ { |
+ if (path == "prefs.json") |
+ { |
+ std::stringstream ss; |
+ ss << content->rdbuf(); |
+ prefsContents = ss.str(); |
+ } |
+ else |
+ LazyFileSystem::Write(path, content); |
+ } |
+ |
+ StatResult Stat(const std::string& path) const |
+ { |
+ if (path == "prefs.json") |
+ { |
+ StatResult result; |
+ result.exists = result.isFile = !prefsContents.empty(); |
+ return result; |
+ } |
+ else |
Felix Dahlke
2013/06/05 10:50:52
No else necessary, there's a return above
|
+ return LazyFileSystem::Stat(path); |
+ } |
+ }; |
+ |
template<class FileSystem, class LogSystem> |
class FilterEngineTestGeneric : public BaseJsTest |
{ |
protected: |
FilterEnginePtr filterEngine; |
void SetUp() |
{ |
@@ -189,8 +231,94 @@ TEST_F(FilterEngineTest, FirstRunFlag) |
{ |
ASSERT_FALSE(filterEngine->IsFirstRun()); |
} |
TEST_F(FilterEngineTestNoData, FirstRunFlag) |
{ |
ASSERT_FALSE(filterEngine->IsFirstRun()); |
} |
+ |
+TEST_F(FilterEngineTest, PrefsGetSet) |
+{ |
+ ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile")->AsString()); |
+ ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt()); |
+ ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool()); |
+ ASSERT_TRUE(filterEngine->GetPref("foobar")->IsUndefined()); |
+ |
+ ASSERT_ANY_THROW(filterEngine->SetPref("patternsfile", jsEngine->NewValue(0))); |
+ ASSERT_ANY_THROW(filterEngine->SetPref("patternsbackupinterval", jsEngine->NewValue(true))); |
+ ASSERT_ANY_THROW(filterEngine->SetPref("subscriptions_autoupdate", jsEngine->NewValue("foo"))); |
+ |
+ filterEngine->SetPref("patternsfile", jsEngine->NewValue("filters.ini")); |
+ filterEngine->SetPref("patternsbackupinterval", jsEngine->NewValue(48)); |
+ filterEngine->SetPref("subscriptions_autoupdate", jsEngine->NewValue(false)); |
+ |
+ ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile")->AsString()); |
+ ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval")->AsInt()); |
+ ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool()); |
+} |
+ |
+TEST(FilterEngineTestCustom, PrefsReadWrite) |
Felix Dahlke
2013/06/05 10:50:52
I think it'd be helpful if this was split up into
|
+{ |
+ PrefTestFileSystem* fs = new PrefTestFileSystem; |
+ AdblockPlus::FileSystemPtr fileSystem(fs); |
+ |
+ // Verify that settings persist |
+ { |
+ AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); |
+ jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new ThrowingLogSystem)); |
+ jsEngine->SetFileSystem(fileSystem); |
+ jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest)); |
+ FilterEnginePtr filterEngine(new AdblockPlus::FilterEngine(jsEngine)); |
+ |
+ ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile")->AsString()); |
+ ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt()); |
+ ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool()); |
+ |
+ filterEngine->SetPref("patternsfile", jsEngine->NewValue("filters.ini")); |
+ filterEngine->SetPref("patternsbackupinterval", jsEngine->NewValue(48)); |
+ filterEngine->SetPref("subscriptions_autoupdate", jsEngine->NewValue(false)); |
+ |
+ AdblockPlus::Sleep(100); |
+ |
+ ASSERT_FALSE(fs->prefsContents.empty()); |
+ } |
+ |
+ { |
+ AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New(); |
+ jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new ThrowingLogSystem)); |
+ jsEngine->SetFileSystem(fileSystem); |
+ jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest)); |
+ FilterEnginePtr filterEngine(new AdblockPlus::FilterEngine(jsEngine)); |
+ |
+ ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile")->AsString()); |
+ ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval")->AsInt()); |
+ ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool()); |
+ } |
+ |
+ // Verify that unknown prefs are ignored while others are respected |
+ { |
+ fs->prefsContents = "{\"foobar\":2, \"patternsbackupinterval\": 12}"; |
+ |
+ AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New(); |
+ jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new ThrowingLogSystem)); |
+ jsEngine->SetFileSystem(fileSystem); |
+ jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest)); |
+ FilterEnginePtr filterEngine(new AdblockPlus::FilterEngine(jsEngine)); |
+ |
+ ASSERT_TRUE(filterEngine->GetPref("foobar")->IsUndefined()); |
+ ASSERT_EQ(12, filterEngine->GetPref("patternsbackupinterval")->AsInt()); |
+ } |
+ |
+ // Verify wrong syntax causes a fallback to defaults |
+ { |
+ fs->prefsContents = "{\"patternsbackupinterval\": 6, \"foo\"}"; |
+ |
+ AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New(); |
+ jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem)); |
+ jsEngine->SetFileSystem(fileSystem); |
+ jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest)); |
+ FilterEnginePtr filterEngine(new AdblockPlus::FilterEngine(jsEngine)); |
+ |
+ ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt()); |
+ } |
+} |