Back to index

lightning-sunbird  0.9+nobinonly
TestMetricsConfig.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* vim:set ts=2 sw=2 sts=2 et cindent: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the Metrics extension.
00017  *
00018  * The Initial Developer of the Original Code is Google Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2006
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Brian Ryner <bryner@brianryner.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 // Unit test for nsMetricsConfig
00040 
00041 #include "TestCommon.h"
00042 #include "nsMetricsConfig.h"
00043 #include "nsMetricsService.h"
00044 #include "nsXPCOM.h"
00045 #include "nsILocalFile.h"
00046 #ifndef MOZILLA_1_8_BRANCH
00047 #include "nsIClassInfoImpl.h"
00048 #endif
00049 
00050 #include <stdio.h>
00051 
00052 // This singleton must exist in any code that links against libmetrics_s.
00053 // TODO: find a way to declare this in src/ while still allowing it to be
00054 // visible to nsMetricsModule.
00055 NS_DECL_CLASSINFO(nsMetricsService)
00056 
00057 static int gTotalTests = 0;
00058 static int gPassedTests = 0;
00059 
00060 void TestLoad(const char *testdata_path)
00061 {
00062   ++gTotalTests;
00063 
00064   nsMetricsConfig config;
00065   ASSERT_TRUE(config.Init());
00066 
00067   nsCOMPtr<nsILocalFile> dataFile;
00068   NS_NewNativeLocalFile(nsDependentCString(testdata_path),
00069                         PR_TRUE, getter_AddRefs(dataFile));
00070   ASSERT_TRUE(dataFile);
00071 
00072   ASSERT_SUCCESS(dataFile->AppendNative(
00073                      NS_LITERAL_CSTRING("test_config.xml")));
00074   ASSERT_SUCCESS(config.Load(dataFile));
00075 
00076   ASSERT_TRUE(config.IsEventEnabled(NS_LITERAL_STRING(NS_METRICS_NAMESPACE),
00077                                     NS_LITERAL_STRING("foo")));
00078   ASSERT_TRUE(config.IsEventEnabled(NS_LITERAL_STRING(NS_METRICS_NAMESPACE),
00079                                     NS_LITERAL_STRING("bar")));
00080   ASSERT_FALSE(config.IsEventEnabled(NS_LITERAL_STRING(NS_METRICS_NAMESPACE),
00081                                      NS_LITERAL_STRING("baz")));
00082 
00083   ASSERT_TRUE(config.EventLimit() == 200);
00084   ASSERT_TRUE(config.UploadInterval() == 1000);
00085   ASSERT_TRUE(config.HasConfig());
00086   ++gPassedTests;
00087 }
00088 
00089 // Returns true if the contents of |file| match |contents|.
00090 static PRBool CheckFileContents(nsILocalFile *file, const char *contents)
00091 {
00092   nsCString nativePath;
00093   file->GetNativePath(nativePath);
00094 
00095   // Now read in the file contents and compare to the expected output
00096   PRFileInfo info;
00097   ASSERT_TRUE_RET(PR_GetFileInfo(nativePath.get(), &info) == PR_SUCCESS,
00098                   PR_FALSE);
00099 
00100   char *buf = new char[info.size + 1];
00101   ASSERT_TRUE_RET(buf, PR_FALSE);
00102 
00103   PRFileDesc *fd = PR_Open(nativePath.get(), PR_RDONLY, 0);
00104   ASSERT_TRUE_RET(fd, PR_FALSE);
00105 
00106   ASSERT_TRUE_RET(PR_Read(fd, buf, info.size) == info.size, PR_FALSE);
00107   PR_Close(fd);
00108   buf[info.size] = '\0';
00109 
00110   // Leave the file in place if the test failed
00111   ASSERT_TRUE_RET(!strcmp(buf, contents), PR_FALSE);
00112   PR_Delete(nativePath.get());
00113   delete[] buf;
00114   return PR_TRUE;
00115 }
00116 
00117 void TestSave(const char *temp_data_path)
00118 {
00119   ++gTotalTests;
00120   static const char kFilename[] = "test-save.xml";
00121   static const char kExpectedContents[] =
00122     "<response xmlns=\"http://www.mozilla.org/metrics\"><config>"
00123     "<collectors>"
00124       "<collector type=\"uielement\"/>"
00125     "</collectors>"
00126     "<limit events=\"300\"/>"
00127     "<upload interval=\"500\"/>"
00128     "</config></response>";
00129 
00130   nsMetricsConfig config;
00131   ASSERT_TRUE(config.Init());
00132 
00133   // The data file goes to the current directory
00134   config.SetEventEnabled(NS_LITERAL_STRING(NS_METRICS_NAMESPACE),
00135                          NS_LITERAL_STRING("uielement"), PR_TRUE);
00136   config.SetUploadInterval(500);
00137   config.SetEventLimit(300);
00138 
00139   nsCOMPtr<nsILocalFile> outFile;
00140   NS_NewNativeLocalFile(nsDependentCString(temp_data_path),
00141                         PR_TRUE, getter_AddRefs(outFile));
00142   ASSERT_TRUE(outFile);
00143   ASSERT_SUCCESS(outFile->AppendNative(nsDependentCString(kFilename)));
00144 
00145   ASSERT_SUCCESS(config.Save(outFile));
00146   ASSERT_TRUE(CheckFileContents(outFile, kExpectedContents));
00147 
00148   // Now test with no collectors
00149   static const char kExpectedOutputNoEvents[] =
00150     "<response xmlns=\"http://www.mozilla.org/metrics\"><config>"
00151     "<collectors/>"
00152     "<limit events=\"300\"/>"
00153     "<upload interval=\"500\"/>"
00154     "</config></response>";
00155 
00156   config.ClearEvents();
00157   ASSERT_SUCCESS(config.Save(outFile));
00158   ASSERT_TRUE(CheckFileContents(outFile, kExpectedOutputNoEvents));
00159 
00160   ++gPassedTests;
00161 }
00162 
00163 int main(int argc, char **argv)
00164 {
00165   if (argc < 2) {
00166     fprintf(stderr, "Usage: %s test_data_path temp_data_path\n", argv[0]);
00167     return 1;
00168   }
00169 
00170   TestLoad(argv[1]);
00171   TestSave(argv[2]);
00172 
00173   printf("%d/%d tests passed\n", gPassedTests, gTotalTests);
00174   return 0;
00175 }