OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 <string> |
| 6 |
| 7 #include "base/json/json_reader.h" |
| 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/trace_event/heap_profiler_type_name_deduplicator.h" |
| 11 #include "base/values.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace base { |
| 15 namespace trace_event { |
| 16 |
| 17 // Define all strings once, because the deduplicator requires pointer equality, |
| 18 // and string interning is unreliable. |
| 19 const char kInt[] = "int"; |
| 20 const char kBool[] = "bool"; |
| 21 const char kString[] = "string"; |
| 22 const char kNeedsEscape[] = "\"quotes\""; |
| 23 |
| 24 scoped_ptr<Value> DumpAndReadBack(const ConvertableToTraceFormat& convertable) { |
| 25 std::string json; |
| 26 convertable.AppendAsTraceFormat(&json); |
| 27 return JSONReader::Read(json); |
| 28 } |
| 29 |
| 30 TEST(TypeNameDeduplicatorTest, Deduplication) { |
| 31 // The type IDs should be like this: |
| 32 // 0: [unknown] |
| 33 // 1: int |
| 34 // 2: bool |
| 35 // 3: string |
| 36 |
| 37 scoped_refptr<TypeNameDeduplicator> dedup = new TypeNameDeduplicator; |
| 38 ASSERT_EQ(1, dedup->Insert(kInt)); |
| 39 ASSERT_EQ(2, dedup->Insert(kBool)); |
| 40 ASSERT_EQ(3, dedup->Insert(kString)); |
| 41 |
| 42 // Inserting again should return the same IDs. |
| 43 ASSERT_EQ(2, dedup->Insert(kBool)); |
| 44 ASSERT_EQ(1, dedup->Insert(kInt)); |
| 45 ASSERT_EQ(3, dedup->Insert(kString)); |
| 46 |
| 47 // A null pointer should yield type ID 0. |
| 48 ASSERT_EQ(0, dedup->Insert(nullptr)); |
| 49 } |
| 50 |
| 51 TEST(TypeNameDeduplicatorTest, EscapeTypeName) { |
| 52 scoped_refptr<TypeNameDeduplicator> dedup = new TypeNameDeduplicator; |
| 53 ASSERT_EQ(1, dedup->Insert(kNeedsEscape)); |
| 54 |
| 55 // Reading json should not fail, because the type name should have been |
| 56 // escaped properly. |
| 57 scoped_ptr<Value> type_names = DumpAndReadBack(*dedup); |
| 58 ASSERT_NE(nullptr, type_names); |
| 59 |
| 60 const DictionaryValue* dictionary; |
| 61 ASSERT_TRUE(type_names->GetAsDictionary(&dictionary)); |
| 62 |
| 63 // When the type name was inserted, it got ID 1. The exported key "1" |
| 64 // should contain the name, with quotes. |
| 65 std::string type_name; |
| 66 ASSERT_TRUE(dictionary->GetString("1", &type_name)); |
| 67 ASSERT_EQ("\"quotes\"", type_name); |
| 68 } |
| 69 |
| 70 } // namespace trace_event |
| 71 } // namespace base |
OLD | NEW |