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.
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"
15 const char* const kAllWdLevels
[] = {
16 "ALL", "DEBUG", "INFO", "WARNING", "SEVERE", "OFF"
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
);
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
);
45 void ValidateLogEntry(base::ListValue
*entries
,
47 const std::string
& expected_level
,
48 const std::string
& expected_message
) {
49 const base::DictionaryValue
*entry
;
50 ASSERT_TRUE(entries
->GetDictionary(index
, &entry
));
52 EXPECT_TRUE(entry
->GetString("level", &level
));
53 EXPECT_EQ(expected_level
, level
);
55 ASSERT_TRUE(entry
->GetString("message", &message
));
56 EXPECT_EQ(expected_message
, message
);
58 EXPECT_TRUE(entry
->GetDouble("timestamp", ×tamp
));
59 EXPECT_LT(0, timestamp
);
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
70 scoped_ptr
<base::ListValue
> entries(log
.GetAndClearEntries());
72 ASSERT_EQ(2u, entries
->GetSize());
73 ValidateLogEntry(entries
.get(), 0, "INFO", "info message");
74 ValidateLogEntry(entries
.get(), 1, "SEVERE", "severe message");
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
82 scoped_ptr
<base::ListValue
> entries(log
.GetAndClearEntries());
84 ASSERT_EQ(0u, entries
->GetSize());
87 TEST(WebDriverLog
, All
) {
88 WebDriverLog
log("type", Log::kAll
);
89 log
.AddEntry(Log::kError
, "severe message");
90 log
.AddEntry(Log::kDebug
, "debug message");
92 scoped_ptr
<base::ListValue
> entries(log
.GetAndClearEntries());
94 ASSERT_EQ(2u, entries
->GetSize());
95 ValidateLogEntry(entries
.get(), 0, "SEVERE", "severe message");
96 ValidateLogEntry(entries
.get(), 1, "DEBUG", "debug message");
99 TEST(Logging
, CreatePerformanceLog
) {
100 Capabilities capabilities
;
101 capabilities
.logging_prefs
["performance"] = Log::kInfo
;
102 capabilities
.logging_prefs
["browser"] = Log::kInfo
;
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());
114 TEST(Logging
, IgnoreUnknownLogType
) {
115 Capabilities capabilities
;
116 capabilities
.logging_prefs
["gaga"] = Log::kInfo
;
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());
127 TEST(Logging
, DefaultLogs
) {
128 Capabilities capabilities
;
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());
139 TEST(Logging
, GetFirstErrorMessage
) {
140 WebDriverLog
log(WebDriverLog::kBrowserType
, Log::kAll
);
143 entry
= log
.GetFirstErrorMessage();
144 ASSERT_TRUE(entry
.empty());
146 log
.AddEntry(Log::kInfo
, "info message");
147 log
.AddEntry(Log::kError
, "first error message");
148 log
.AddEntry(Log::kDebug
, "debug message");
149 log
.AddEntry(Log::kError
, "second error message");
151 entry
= log
.GetFirstErrorMessage();
152 ASSERT_EQ("first error message", entry
);