1 // Copyright (c) 2013 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 "base/values.h" 6 #include "chrome/test/chromedriver/capabilities.h" 7 #include "chrome/test/chromedriver/chrome/devtools_event_listener.h" 8 #include "chrome/test/chromedriver/chrome/log.h" 9 #include "chrome/test/chromedriver/chrome/status.h" 10 #include "chrome/test/chromedriver/logging.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 namespace { 14 15 const char* const kAllWdLevels[] = { 16 "ALL", "DEBUG", "INFO", "WARNING", "SEVERE", "OFF" 17 }; 18 19 } 20 21 TEST(Logging, NameLevelConversionHappy) { 22 // All names map to a valid enum value. 23 for (int i = 0; static_cast<size_t>(i) < arraysize(kAllWdLevels); ++i) { 24 Log::Level level = static_cast<Log::Level>(-1); 25 EXPECT_TRUE(WebDriverLog::NameToLevel(kAllWdLevels[i], &level)); 26 EXPECT_LE(Log::kAll, level); 27 EXPECT_GE(Log::kOff, level); 28 } 29 } 30 31 TEST(Logging, NameToLevelErrors) { 32 Log::Level level = static_cast<Log::Level>(-1); 33 EXPECT_FALSE(WebDriverLog::NameToLevel("A", &level)); 34 EXPECT_FALSE(WebDriverLog::NameToLevel("B", &level)); 35 EXPECT_FALSE(WebDriverLog::NameToLevel("H", &level)); 36 EXPECT_FALSE(WebDriverLog::NameToLevel("R", &level)); 37 EXPECT_FALSE(WebDriverLog::NameToLevel("T", &level)); 38 EXPECT_FALSE(WebDriverLog::NameToLevel("Z", &level)); 39 // The level variable was never modified. 40 EXPECT_EQ(static_cast<Log::Level>(-1), level); 41 } 42 43 namespace { 44 45 void ValidateLogEntry(base::ListValue *entries, 46 int index, 47 const std::string& expected_level, 48 const std::string& expected_message) { 49 const base::DictionaryValue *entry; 50 ASSERT_TRUE(entries->GetDictionary(index, &entry)); 51 std::string level; 52 EXPECT_TRUE(entry->GetString("level", &level)); 53 EXPECT_EQ(expected_level, level); 54 std::string message; 55 ASSERT_TRUE(entry->GetString("message", &message)); 56 EXPECT_EQ(expected_message, message); 57 double timestamp = 0; 58 EXPECT_TRUE(entry->GetDouble("timestamp", ×tamp)); 59 EXPECT_LT(0, timestamp); 60 } 61 62 } // namespace 63 64 TEST(WebDriverLog, Levels) { 65 WebDriverLog log("type", Log::kInfo); 66 log.AddEntry(Log::kInfo, std::string("info message")); 67 log.AddEntry(Log::kError, "severe message"); 68 log.AddEntry(Log::kDebug, "debug message"); // Must not log 69 70 scoped_ptr<base::ListValue> entries(log.GetAndClearEntries()); 71 72 ASSERT_EQ(2u, entries->GetSize()); 73 ValidateLogEntry(entries.get(), 0, "INFO", "info message"); 74 ValidateLogEntry(entries.get(), 1, "SEVERE", "severe message"); 75 } 76 77 TEST(WebDriverLog, Off) { 78 WebDriverLog log("type", Log::kOff); 79 log.AddEntry(Log::kError, "severe message"); // Must not log 80 log.AddEntry(Log::kDebug, "debug message"); // Must not log 81 82 scoped_ptr<base::ListValue> entries(log.GetAndClearEntries()); 83 84 ASSERT_EQ(0u, entries->GetSize()); 85 } 86 87 TEST(WebDriverLog, All) { 88 WebDriverLog log("type", Log::kAll); 89 log.AddEntry(Log::kError, "severe message"); 90 log.AddEntry(Log::kDebug, "debug message"); 91 92 scoped_ptr<base::ListValue> entries(log.GetAndClearEntries()); 93 94 ASSERT_EQ(2u, entries->GetSize()); 95 ValidateLogEntry(entries.get(), 0, "SEVERE", "severe message"); 96 ValidateLogEntry(entries.get(), 1, "DEBUG", "debug message"); 97 } 98 99 TEST(Logging, CreatePerformanceLog) { 100 Capabilities capabilities; 101 capabilities.logging_prefs["performance"] = Log::kInfo; 102 capabilities.logging_prefs["browser"] = Log::kInfo; 103 104 ScopedVector<DevToolsEventListener> listeners; 105 ScopedVector<WebDriverLog> logs; 106 Status status = CreateLogs(capabilities, &logs, &listeners); 107 ASSERT_TRUE(status.IsOk()); 108 ASSERT_EQ(2u, logs.size()); 109 ASSERT_EQ(2u, listeners.size()); 110 ASSERT_EQ("performance", logs[0]->type()); 111 ASSERT_EQ("browser", logs[1]->type()); 112 } 113 114 TEST(Logging, IgnoreUnknownLogType) { 115 Capabilities capabilities; 116 capabilities.logging_prefs["gaga"] = Log::kInfo; 117 118 ScopedVector<DevToolsEventListener> listeners; 119 ScopedVector<WebDriverLog> logs; 120 Status status = CreateLogs(capabilities, &logs, &listeners); 121 EXPECT_TRUE(status.IsOk()); 122 ASSERT_EQ(1u, logs.size()); 123 ASSERT_EQ(1u, listeners.size()); 124 ASSERT_EQ("browser", logs[0]->type()); 125 } 126 127 TEST(Logging, DefaultLogs) { 128 Capabilities capabilities; 129 130 ScopedVector<DevToolsEventListener> listeners; 131 ScopedVector<WebDriverLog> logs; 132 Status status = CreateLogs(capabilities, &logs, &listeners); 133 EXPECT_TRUE(status.IsOk()); 134 ASSERT_EQ(1u, logs.size()); 135 ASSERT_EQ(1u, listeners.size()); 136 ASSERT_EQ("browser", logs[0]->type()); 137 } 138