1 // Copyright (c) 2006-2008 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 "chrome/browser/metrics/metrics_response.h" 8 #include "chrome/browser/metrics/metrics_service.h" 9 #include "base/string_util.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 namespace { 13 class MetricsResponseTest : public testing::Test { 14 }; 15 }; 16 17 static const char kNoLogResponse[] = 18 "<response xmlns=\"http://www.mozilla.org/metrics\"><config>" 19 "</config></response>"; 20 21 static const char kLogResponse1[] = 22 "<response xmlns=\"http://www.mozilla.org/metrics\"><config>" 23 "<collectors>" 24 "<collector type=\"profile\"/>" 25 "<collector type=\"document\"/>" 26 "<collector type=\"window\"/>" 27 "<collector type=\"ui\"/>" 28 "</collectors>" 29 "<limit events=\"500\"/><upload interval=\"600\"/>" 30 "</config></response>"; 31 32 static const char kLogResponse2[] = 33 "<response xmlns=\"http://www.mozilla.org/metrics\"><config>" 34 "<collectors>" 35 "<collector type=\"profile\"/>" 36 "<collector type=\"document\"/>" 37 "<collector type=\"window\"/>" 38 "</collectors>" 39 "<limit events=\"250\"/><upload interval=\"900\"/>" 40 "</config></response>"; 41 42 43 static const struct ResponseCase { 44 const char* response_xml; 45 int collectors; 46 int events; 47 int interval; 48 bool profile_active; 49 bool window_active; 50 bool document_active; 51 bool ui_active; 52 } response_cases[] = { 53 { 54 kNoLogResponse, 55 MetricsResponse::COLLECTOR_NONE, 56 0, 0, 57 false, false, false, false 58 }, 59 { 60 kLogResponse1, 61 MetricsResponse::COLLECTOR_PROFILE | 62 MetricsResponse::COLLECTOR_DOCUMENT | 63 MetricsResponse::COLLECTOR_WINDOW | 64 MetricsResponse::COLLECTOR_UI, 65 500, 600, 66 true, true, true, true 67 }, 68 { 69 kLogResponse2, 70 MetricsResponse::COLLECTOR_PROFILE | 71 MetricsResponse::COLLECTOR_DOCUMENT | 72 MetricsResponse::COLLECTOR_WINDOW, 73 250, 900, 74 true, true, true, false 75 }, 76 }; 77 78 TEST(MetricsResponseTest, ParseResponse) { 79 for (size_t i = 0; i < arraysize(response_cases); ++i) { 80 ResponseCase rcase = response_cases[i]; 81 MetricsResponse response(rcase.response_xml); 82 EXPECT_TRUE(response.valid()); 83 EXPECT_EQ(rcase.collectors, response.collectors()) << 84 "Mismatch in case " << i; 85 EXPECT_EQ(rcase.events, response.events()) << "Mismatch in case " << i; 86 EXPECT_EQ(rcase.interval, response.interval()) << "Mismatch in case " << i; 87 EXPECT_EQ(rcase.profile_active, 88 response.collector_active(MetricsResponse::COLLECTOR_PROFILE)) << 89 "Mismatch in case " << i; 90 EXPECT_EQ(rcase.window_active, 91 response.collector_active(MetricsResponse::COLLECTOR_WINDOW)) << 92 "Mismatch in case " << i; 93 EXPECT_EQ(rcase.document_active, 94 response.collector_active(MetricsResponse::COLLECTOR_DOCUMENT)) << 95 "Mismatch in case " << i; 96 EXPECT_EQ(rcase.ui_active, 97 response.collector_active(MetricsResponse::COLLECTOR_UI)) << 98 "Mismatch in case " << i; 99 } 100 } 101 102 static const char* bogus_responses[] = {"", "<respo"}; 103 TEST(MetricsResponseTest, ParseBogusResponse) { 104 for (size_t i = 0; i < arraysize(bogus_responses); ++i) { 105 MetricsResponse response(bogus_responses[i]); 106 EXPECT_FALSE(response.valid()); 107 } 108 } 109