| 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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 #include "BaseJsTest.h" | 18 #include "BaseJsTest.h" |
| 19 | 19 |
| 20 namespace | 20 namespace |
| 21 { | 21 { |
| 22 class MockErrorCallback : public AdblockPlus::ErrorCallback | 22 class MockLogSystem : public AdblockPlus::LogSystem |
| 23 { | 23 { |
| 24 public: | 24 public: |
| 25 AdblockPlus::LogSystem::LogLevel lastLogLevel; |
| 25 std::string lastMessage; | 26 std::string lastMessage; |
| 27 std::string lastSource; |
| 26 | 28 |
| 27 void operator()(const std::string& message) | 29 void operator()(AdblockPlus::LogSystem::LogLevel logLevel, |
| 30 const std::string& message, const std::string& source) |
| 28 { | 31 { |
| 32 lastLogLevel = logLevel; |
| 29 lastMessage = message; | 33 lastMessage = message; |
| 34 lastSource = source; |
| 30 } | 35 } |
| 31 }; | 36 }; |
| 32 | 37 |
| 33 typedef std::tr1::shared_ptr<MockErrorCallback> MockErrorCallbackPtr; | 38 typedef std::tr1::shared_ptr<MockLogSystem> MockLogSystemPtr; |
| 34 | 39 |
| 35 class ConsoleJsObjectTest : public BaseJsTest | 40 class ConsoleJsObjectTest : public BaseJsTest |
| 36 { | 41 { |
| 37 protected: | 42 protected: |
| 38 MockErrorCallbackPtr mockErrorCallback; | 43 MockLogSystemPtr mockLogSystem; |
| 39 | 44 |
| 40 void SetUp() | 45 void SetUp() |
| 41 { | 46 { |
| 42 BaseJsTest::SetUp(); | 47 BaseJsTest::SetUp(); |
| 43 mockErrorCallback = MockErrorCallbackPtr(new MockErrorCallback); | 48 mockLogSystem = MockLogSystemPtr(new MockLogSystem); |
| 44 jsEngine->SetErrorCallback(mockErrorCallback); | 49 jsEngine->SetLogSystem(mockLogSystem); |
| 45 } | 50 } |
| 46 }; | 51 }; |
| 47 } | 52 } |
| 48 | 53 |
| 49 TEST_F(ConsoleJsObjectTest, ErrorInvokesErrorCallback) | 54 TEST_F(ConsoleJsObjectTest, ConsoleLogCall) |
| 50 { | 55 { |
| 51 jsEngine->Evaluate("console.error('foo')"); | 56 jsEngine->Evaluate("\n\nconsole.log('foo', 'bar');\n\n", "eval"); |
| 52 ASSERT_EQ("foo", mockErrorCallback->lastMessage); | 57 ASSERT_EQ(AdblockPlus::LogSystem::LOG, mockLogSystem->lastLogLevel); |
| 58 ASSERT_EQ("foo bar", mockLogSystem->lastMessage); |
| 59 ASSERT_EQ("eval:3", mockLogSystem->lastSource); |
| 53 } | 60 } |
| 54 | 61 |
| 55 TEST_F(ConsoleJsObjectTest, ErrorWithMultipleArguments) | 62 TEST_F(ConsoleJsObjectTest, ConsoleDebugCall) |
| 63 { |
| 64 jsEngine->Evaluate("console.debug('foo', 'bar')"); |
| 65 ASSERT_EQ(AdblockPlus::LogSystem::LOG, mockLogSystem->lastLogLevel); |
| 66 ASSERT_EQ("foo bar", mockLogSystem->lastMessage); |
| 67 ASSERT_EQ(":1", mockLogSystem->lastSource); |
| 68 } |
| 69 |
| 70 TEST_F(ConsoleJsObjectTest, ConsoleInfoCall) |
| 71 { |
| 72 jsEngine->Evaluate("console.info('foo', 'bar')"); |
| 73 ASSERT_EQ(AdblockPlus::LogSystem::INFO, mockLogSystem->lastLogLevel); |
| 74 ASSERT_EQ("foo bar", mockLogSystem->lastMessage); |
| 75 ASSERT_EQ(":1", mockLogSystem->lastSource); |
| 76 } |
| 77 |
| 78 TEST_F(ConsoleJsObjectTest, ConsoleWarnCall) |
| 79 { |
| 80 jsEngine->Evaluate("console.warn('foo', 'bar')"); |
| 81 ASSERT_EQ(AdblockPlus::LogSystem::WARN, mockLogSystem->lastLogLevel); |
| 82 ASSERT_EQ("foo bar", mockLogSystem->lastMessage); |
| 83 ASSERT_EQ(":1", mockLogSystem->lastSource); |
| 84 } |
| 85 |
| 86 TEST_F(ConsoleJsObjectTest, ConsoleErrorCall) |
| 56 { | 87 { |
| 57 jsEngine->Evaluate("console.error('foo', 'bar')"); | 88 jsEngine->Evaluate("console.error('foo', 'bar')"); |
| 58 ASSERT_EQ("foobar", mockErrorCallback->lastMessage); | 89 ASSERT_EQ(AdblockPlus::LogSystem::ERROR, mockLogSystem->lastLogLevel); |
| 90 ASSERT_EQ("foo bar", mockLogSystem->lastMessage); |
| 91 ASSERT_EQ(":1", mockLogSystem->lastSource); |
| 59 } | 92 } |
| 60 | 93 |
| 61 TEST_F(ConsoleJsObjectTest, TraceDoesNothing) | 94 TEST_F(ConsoleJsObjectTest, ConsoleTraceCall) |
| 62 { | 95 { |
| 63 jsEngine->SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCall
back)); | 96 jsEngine->Evaluate("\n\ |
| 64 jsEngine->Evaluate("console.trace()"); | 97 function foo()\n\ |
| 98 {\n\ |
| 99 (function() {\n\ |
| 100 console.trace();\n\ |
| 101 })();\n\ |
| 102 }\n\ |
| 103 foo();", "eval"); |
| 104 ASSERT_EQ(AdblockPlus::LogSystem::TRACE, mockLogSystem->lastLogLevel); |
| 105 ASSERT_EQ("\ |
| 106 1: /* anonymous */() at eval:5\n\ |
| 107 2: foo() at eval:6\n\ |
| 108 3: /* anonymous */() at eval:8\n", mockLogSystem->lastMessage); |
| 109 ASSERT_EQ("", mockLogSystem->lastSource); |
| 65 } | 110 } |
| OLD | NEW |