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/command_listener.h"
11 #include "chrome/test/chromedriver/logging.h"
12 #include "chrome/test/chromedriver/session.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 const char* const kAllWdLevels
[] = {
18 "ALL", "DEBUG", "INFO", "WARNING", "SEVERE", "OFF"
23 TEST(Logging
, NameLevelConversionHappy
) {
24 // All names map to a valid enum value.
25 for (int i
= 0; static_cast<size_t>(i
) < arraysize(kAllWdLevels
); ++i
) {
26 Log::Level level
= static_cast<Log::Level
>(-1);
27 EXPECT_TRUE(WebDriverLog::NameToLevel(kAllWdLevels
[i
], &level
));
28 EXPECT_LE(Log::kAll
, level
);
29 EXPECT_GE(Log::kOff
, level
);
33 TEST(Logging
, NameToLevelErrors
) {
34 Log::Level level
= static_cast<Log::Level
>(-1);
35 EXPECT_FALSE(WebDriverLog::NameToLevel("A", &level
));
36 EXPECT_FALSE(WebDriverLog::NameToLevel("B", &level
));
37 EXPECT_FALSE(WebDriverLog::NameToLevel("H", &level
));
38 EXPECT_FALSE(WebDriverLog::NameToLevel("R", &level
));
39 EXPECT_FALSE(WebDriverLog::NameToLevel("T", &level
));
40 EXPECT_FALSE(WebDriverLog::NameToLevel("Z", &level
));
41 // The level variable was never modified.
42 EXPECT_EQ(static_cast<Log::Level
>(-1), level
);
47 void ValidateLogEntry(base::ListValue
*entries
,
49 const std::string
& expected_level
,
50 const std::string
& expected_message
) {
51 const base::DictionaryValue
*entry
;
52 ASSERT_TRUE(entries
->GetDictionary(index
, &entry
));
54 EXPECT_TRUE(entry
->GetString("level", &level
));
55 EXPECT_EQ(expected_level
, level
);
57 ASSERT_TRUE(entry
->GetString("message", &message
));
58 EXPECT_EQ(expected_message
, message
);
60 EXPECT_TRUE(entry
->GetDouble("timestamp", ×tamp
));
61 EXPECT_LT(0, timestamp
);
66 TEST(WebDriverLog
, Levels
) {
67 WebDriverLog
log("type", Log::kInfo
);
68 log
.AddEntry(Log::kInfo
, std::string("info message"));
69 log
.AddEntry(Log::kError
, "severe message");
70 log
.AddEntry(Log::kDebug
, "debug message"); // Must not log
72 scoped_ptr
<base::ListValue
> entries(log
.GetAndClearEntries());
74 ASSERT_EQ(2u, entries
->GetSize());
75 ValidateLogEntry(entries
.get(), 0, "INFO", "info message");
76 ValidateLogEntry(entries
.get(), 1, "SEVERE", "severe message");
79 TEST(WebDriverLog
, Off
) {
80 WebDriverLog
log("type", Log::kOff
);
81 log
.AddEntry(Log::kError
, "severe message"); // Must not log
82 log
.AddEntry(Log::kDebug
, "debug message"); // Must not log
84 scoped_ptr
<base::ListValue
> entries(log
.GetAndClearEntries());
86 ASSERT_EQ(0u, entries
->GetSize());
89 TEST(WebDriverLog
, All
) {
90 WebDriverLog
log("type", Log::kAll
);
91 log
.AddEntry(Log::kError
, "severe message");
92 log
.AddEntry(Log::kDebug
, "debug message");
94 scoped_ptr
<base::ListValue
> entries(log
.GetAndClearEntries());
96 ASSERT_EQ(2u, entries
->GetSize());
97 ValidateLogEntry(entries
.get(), 0, "SEVERE", "severe message");
98 ValidateLogEntry(entries
.get(), 1, "DEBUG", "debug message");
101 TEST(Logging
, CreatePerformanceLog
) {
102 Capabilities capabilities
;
103 Session
session("test");
104 capabilities
.logging_prefs
["performance"] = Log::kInfo
;
105 capabilities
.logging_prefs
["browser"] = Log::kInfo
;
107 ScopedVector
<DevToolsEventListener
> devtools_listeners
;
108 ScopedVector
<WebDriverLog
> logs
;
109 ScopedVector
<CommandListener
> command_listeners
;
110 Status status
= CreateLogs(capabilities
, &session
, &logs
, &devtools_listeners
,
112 ASSERT_TRUE(status
.IsOk());
113 ASSERT_EQ(2u, logs
.size());
114 ASSERT_EQ(2u, devtools_listeners
.size());
115 ASSERT_EQ(1u, command_listeners
.size());
116 ASSERT_EQ("performance", logs
[0]->type());
117 ASSERT_EQ("browser", logs
[1]->type());
120 TEST(Logging
, IgnoreUnknownLogType
) {
121 Capabilities capabilities
;
122 Session
session("test");
123 capabilities
.logging_prefs
["gaga"] = Log::kInfo
;
125 ScopedVector
<DevToolsEventListener
> devtools_listeners
;
126 ScopedVector
<WebDriverLog
> logs
;
127 ScopedVector
<CommandListener
> command_listeners
;
128 Status status
= CreateLogs(capabilities
, &session
, &logs
, &devtools_listeners
,
130 EXPECT_TRUE(status
.IsOk());
131 ASSERT_EQ(1u, logs
.size());
132 ASSERT_EQ(1u, devtools_listeners
.size());
133 ASSERT_EQ(0u, command_listeners
.size());
134 ASSERT_EQ("browser", logs
[0]->type());
137 TEST(Logging
, DefaultLogs
) {
138 Capabilities capabilities
;
139 Session
session("test");
141 ScopedVector
<DevToolsEventListener
> devtools_listeners
;
142 ScopedVector
<WebDriverLog
> logs
;
143 ScopedVector
<CommandListener
> command_listeners
;
144 Status status
= CreateLogs(capabilities
, &session
, &logs
, &devtools_listeners
,
146 EXPECT_TRUE(status
.IsOk());
147 ASSERT_EQ(1u, logs
.size());
148 ASSERT_EQ(1u, devtools_listeners
.size());
149 ASSERT_EQ(0u, command_listeners
.size());
150 ASSERT_EQ("browser", logs
[0]->type());
153 TEST(Logging
, GetFirstErrorMessage
) {
154 WebDriverLog
log(WebDriverLog::kBrowserType
, Log::kAll
);
157 entry
= log
.GetFirstErrorMessage();
158 ASSERT_TRUE(entry
.empty());
160 log
.AddEntry(Log::kInfo
, "info message");
161 log
.AddEntry(Log::kError
, "first error message");
162 log
.AddEntry(Log::kDebug
, "debug message");
163 log
.AddEntry(Log::kError
, "second error message");
165 entry
= log
.GetFirstErrorMessage();
166 ASSERT_EQ("first error message", entry
);