1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is the Metrics extension.
18 * The Initial Developer of the Original Code is Google Inc.
19 * Portions created by the Initial Developer are Copyright (C) 2006
20 * the Initial Developer. All Rights Reserved.
23 * Brian Ryner <bryner@brianryner.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 // Unit test for nsMetricsConfig
41 #include "TestCommon.h"
42 #include "nsMetricsConfig.h"
43 #include "nsMetricsService.h"
45 #include "nsILocalFile.h"
46 #include "nsIClassInfoImpl.h"
50 // This singleton must exist in any code that links against libmetrics_s.
51 // TODO: find a way to declare this in src/ while still allowing it to be
52 // visible to nsMetricsModule.
53 NS_DECL_CLASSINFO(nsMetricsService
)
55 static int gTotalTests
= 0;
56 static int gPassedTests
= 0;
58 void TestLoad(const char *testdata_path
)
62 nsMetricsConfig config
;
63 ASSERT_TRUE(config
.Init());
65 nsCOMPtr
<nsILocalFile
> dataFile
;
66 NS_NewNativeLocalFile(nsDependentCString(testdata_path
),
67 PR_TRUE
, getter_AddRefs(dataFile
));
68 ASSERT_TRUE(dataFile
);
70 ASSERT_SUCCESS(dataFile
->AppendNative(
71 NS_LITERAL_CSTRING("test_config.xml")));
72 ASSERT_SUCCESS(config
.Load(dataFile
));
74 ASSERT_TRUE(config
.IsEventEnabled(NS_LITERAL_STRING(NS_METRICS_NAMESPACE
),
75 NS_LITERAL_STRING("foo")));
76 ASSERT_TRUE(config
.IsEventEnabled(NS_LITERAL_STRING(NS_METRICS_NAMESPACE
),
77 NS_LITERAL_STRING("bar")));
78 ASSERT_FALSE(config
.IsEventEnabled(NS_LITERAL_STRING(NS_METRICS_NAMESPACE
),
79 NS_LITERAL_STRING("baz")));
81 ASSERT_TRUE(config
.EventLimit() == 200);
82 ASSERT_TRUE(config
.UploadInterval() == 1000);
83 ASSERT_TRUE(config
.HasConfig());
87 // Returns true if the contents of |file| match |contents|.
88 static PRBool
CheckFileContents(nsILocalFile
*file
, const char *contents
)
91 file
->GetNativePath(nativePath
);
93 // Now read in the file contents and compare to the expected output
95 ASSERT_TRUE_RET(PR_GetFileInfo(nativePath
.get(), &info
) == PR_SUCCESS
,
98 char *buf
= new char[info
.size
+ 1];
99 ASSERT_TRUE_RET(buf
, PR_FALSE
);
101 PRFileDesc
*fd
= PR_Open(nativePath
.get(), PR_RDONLY
, 0);
102 ASSERT_TRUE_RET(fd
, PR_FALSE
);
104 ASSERT_TRUE_RET(PR_Read(fd
, buf
, info
.size
) == info
.size
, PR_FALSE
);
106 buf
[info
.size
] = '\0';
108 // Leave the file in place if the test failed
109 ASSERT_TRUE_RET(!strcmp(buf
, contents
), PR_FALSE
);
110 PR_Delete(nativePath
.get());
115 void TestSave(const char *temp_data_path
)
118 static const char kFilename
[] = "test-save.xml";
119 static const char kExpectedContents
[] =
120 "<response xmlns=\"http://www.mozilla.org/metrics\"><config>"
122 "<collector type=\"uielement\"/>"
124 "<limit events=\"300\"/>"
125 "<upload interval=\"500\"/>"
126 "</config></response>";
128 nsMetricsConfig config
;
129 ASSERT_TRUE(config
.Init());
131 // The data file goes to the current directory
132 config
.SetEventEnabled(NS_LITERAL_STRING(NS_METRICS_NAMESPACE
),
133 NS_LITERAL_STRING("uielement"), PR_TRUE
);
134 config
.SetUploadInterval(500);
135 config
.SetEventLimit(300);
137 nsCOMPtr
<nsILocalFile
> outFile
;
138 NS_NewNativeLocalFile(nsDependentCString(temp_data_path
),
139 PR_TRUE
, getter_AddRefs(outFile
));
140 ASSERT_TRUE(outFile
);
141 ASSERT_SUCCESS(outFile
->AppendNative(nsDependentCString(kFilename
)));
143 ASSERT_SUCCESS(config
.Save(outFile
));
144 ASSERT_TRUE(CheckFileContents(outFile
, kExpectedContents
));
146 // Now test with no collectors
147 static const char kExpectedOutputNoEvents
[] =
148 "<response xmlns=\"http://www.mozilla.org/metrics\"><config>"
150 "<limit events=\"300\"/>"
151 "<upload interval=\"500\"/>"
152 "</config></response>";
154 config
.ClearEvents();
155 ASSERT_SUCCESS(config
.Save(outFile
));
156 ASSERT_TRUE(CheckFileContents(outFile
, kExpectedOutputNoEvents
));
161 int main(int argc
, char **argv
)
164 fprintf(stderr
, "Usage: %s test_data_path temp_data_path\n", argv
[0]);
171 printf("%d/%d tests passed\n", gPassedTests
, gTotalTests
);