| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/crash_keys.h" | 5 #include "chrome/common/crash_keys.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/at_exit.h" | |
| 12 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 13 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 14 #include "base/debug/crash_logging.h" | 13 #include "base/debug/crash_logging.h" |
| 15 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
| 16 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 17 |
| 19 class CrashKeysTest : public testing::Test { | 18 class CrashKeysTest : public testing::Test { |
| 20 public: | 19 public: |
| 21 void SetUp() override { | 20 void SetUp() override { |
| 22 self_ = this; | 21 self_ = this; |
| 23 base::debug::SetCrashKeyReportingFunctions( | 22 base::debug::SetCrashKeyReportingFunctions( |
| 24 &SetCrashKeyValue, &ClearCrashKey); | 23 &SetCrashKeyValue, &ClearCrashKey); |
| 25 crash_keys::RegisterChromeCrashKeys(); | 24 crash_keys::RegisterChromeCrashKeys(); |
| 26 } | 25 } |
| 27 | 26 |
| 28 void TearDown() override { | 27 void TearDown() override { |
| 28 base::debug::ResetCrashLoggingForTesting(); |
| 29 self_ = NULL; | 29 self_ = NULL; |
| 30 } | 30 } |
| 31 | 31 |
| 32 bool HasCrashKey(const std::string& key) { | 32 bool HasCrashKey(const std::string& key) { |
| 33 return keys_.find(key) != keys_.end(); | 33 return keys_.find(key) != keys_.end(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 std::string GetKeyValue(const std::string& key) { | 36 std::string GetKeyValue(const std::string& key) { |
| 37 std::map<std::string, std::string>::const_iterator it = keys_.find(key); | 37 std::map<std::string, std::string>::const_iterator it = keys_.find(key); |
| 38 if (it == keys_.end()) | 38 if (it == keys_.end()) |
| 39 return std::string(); | 39 return std::string(); |
| 40 return it->second; | 40 return it->second; |
| 41 } | 41 } |
| 42 | 42 |
| 43 private: | 43 private: |
| 44 static void SetCrashKeyValue(const base::StringPiece& key, | 44 static void SetCrashKeyValue(const base::StringPiece& key, |
| 45 const base::StringPiece& value) { | 45 const base::StringPiece& value) { |
| 46 self_->keys_[key.as_string()] = value.as_string(); | 46 self_->keys_[key.as_string()] = value.as_string(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 static void ClearCrashKey(const base::StringPiece& key) { | 49 static void ClearCrashKey(const base::StringPiece& key) { |
| 50 self_->keys_.erase(key.as_string()); | 50 self_->keys_.erase(key.as_string()); |
| 51 } | 51 } |
| 52 | 52 |
| 53 static CrashKeysTest* self_; | 53 static CrashKeysTest* self_; |
| 54 | 54 |
| 55 std::map<std::string, std::string> keys_; | 55 std::map<std::string, std::string> keys_; |
| 56 | |
| 57 // The ShadowingAtExitManager will destroy the singleton used to store crash | |
| 58 // key data upon destruction. | |
| 59 base::ShadowingAtExitManager at_exit_manager_; | |
| 60 }; | 56 }; |
| 61 | 57 |
| 62 CrashKeysTest* CrashKeysTest::self_ = NULL; | 58 CrashKeysTest* CrashKeysTest::self_ = NULL; |
| 63 | 59 |
| 64 TEST_F(CrashKeysTest, Switches) { | 60 TEST_F(CrashKeysTest, Switches) { |
| 65 // Set three switches. | 61 // Set three switches. |
| 66 { | 62 { |
| 67 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); | 63 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 68 for (int i = 1; i <= 3; ++i) | 64 for (int i = 1; i <= 3; ++i) |
| 69 command_line.AppendSwitch(base::StringPrintf("--flag-%d", i)); | 65 command_line.AppendSwitch(base::StringPrintf("--flag-%d", i)); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 | 177 |
| 182 crash_keys::SetSwitchesFromCommandLine(&command_line); | 178 crash_keys::SetSwitchesFromCommandLine(&command_line); |
| 183 | 179 |
| 184 EXPECT_EQ("--vv=1", GetKeyValue("switch-1")); | 180 EXPECT_EQ("--vv=1", GetKeyValue("switch-1")); |
| 185 EXPECT_EQ("--vvv", GetKeyValue("switch-2")); | 181 EXPECT_EQ("--vvv", GetKeyValue("switch-2")); |
| 186 EXPECT_EQ("--enable-multi-profiles", GetKeyValue("switch-3")); | 182 EXPECT_EQ("--enable-multi-profiles", GetKeyValue("switch-3")); |
| 187 EXPECT_EQ("--device-management-url=https://foo/bar", GetKeyValue("switch-4")); | 183 EXPECT_EQ("--device-management-url=https://foo/bar", GetKeyValue("switch-4")); |
| 188 EXPECT_FALSE(HasCrashKey("switch-5")); | 184 EXPECT_FALSE(HasCrashKey("switch-5")); |
| 189 } | 185 } |
| 190 #endif | 186 #endif |
| OLD | NEW |