Back to index

lightning-sunbird  0.9+nobinonly
Functions | Variables
TestMetricsConfig.cpp File Reference
#include "TestCommon.h"
#include "nsMetricsConfig.h"
#include "nsMetricsService.h"
#include "nsXPCOM.h"
#include "nsILocalFile.h"
#include "nsIClassInfoImpl.h"
#include <stdio.h>

Go to the source code of this file.

Functions

void TestLoad (const char *testdata_path)
static PRBool CheckFileContents (nsILocalFile *file, const char *contents)
void TestSave (const char *temp_data_path)
int main (int argc, char **argv)

Variables

static int gTotalTests = 0
static int gPassedTests = 0

Function Documentation

static PRBool CheckFileContents ( nsILocalFile file,
const char *  contents 
) [static]

Definition at line 90 of file TestMetricsConfig.cpp.

{
  nsCString nativePath;
  file->GetNativePath(nativePath);

  // Now read in the file contents and compare to the expected output
  PRFileInfo info;
  ASSERT_TRUE_RET(PR_GetFileInfo(nativePath.get(), &info) == PR_SUCCESS,
                  PR_FALSE);

  char *buf = new char[info.size + 1];
  ASSERT_TRUE_RET(buf, PR_FALSE);

  PRFileDesc *fd = PR_Open(nativePath.get(), PR_RDONLY, 0);
  ASSERT_TRUE_RET(fd, PR_FALSE);

  ASSERT_TRUE_RET(PR_Read(fd, buf, info.size) == info.size, PR_FALSE);
  PR_Close(fd);
  buf[info.size] = '\0';

  // Leave the file in place if the test failed
  ASSERT_TRUE_RET(!strcmp(buf, contents), PR_FALSE);
  PR_Delete(nativePath.get());
  delete[] buf;
  return PR_TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

Definition at line 163 of file TestMetricsConfig.cpp.

{
  if (argc < 2) {
    fprintf(stderr, "Usage: %s test_data_path temp_data_path\n", argv[0]);
    return 1;
  }

  TestLoad(argv[1]);
  TestSave(argv[2]);

  printf("%d/%d tests passed\n", gPassedTests, gTotalTests);
  return 0;
}

Here is the call graph for this function:

void TestLoad ( const char *  testdata_path)

Definition at line 60 of file TestMetricsConfig.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void TestSave ( const char *  temp_data_path)

Definition at line 117 of file TestMetricsConfig.cpp.

{
  ++gTotalTests;
  static const char kFilename[] = "test-save.xml";
  static const char kExpectedContents[] =
    "<response xmlns=\"http://www.mozilla.org/metrics\"><config>"
    "<collectors>"
      "<collector type=\"uielement\"/>"
    "</collectors>"
    "<limit events=\"300\"/>"
    "<upload interval=\"500\"/>"
    "</config></response>";

  nsMetricsConfig config;
  ASSERT_TRUE(config.Init());

  // The data file goes to the current directory
  config.SetEventEnabled(NS_LITERAL_STRING(NS_METRICS_NAMESPACE),
                         NS_LITERAL_STRING("uielement"), PR_TRUE);
  config.SetUploadInterval(500);
  config.SetEventLimit(300);

  nsCOMPtr<nsILocalFile> outFile;
  NS_NewNativeLocalFile(nsDependentCString(temp_data_path),
                        PR_TRUE, getter_AddRefs(outFile));
  ASSERT_TRUE(outFile);
  ASSERT_SUCCESS(outFile->AppendNative(nsDependentCString(kFilename)));

  ASSERT_SUCCESS(config.Save(outFile));
  ASSERT_TRUE(CheckFileContents(outFile, kExpectedContents));

  // Now test with no collectors
  static const char kExpectedOutputNoEvents[] =
    "<response xmlns=\"http://www.mozilla.org/metrics\"><config>"
    "<collectors/>"
    "<limit events=\"300\"/>"
    "<upload interval=\"500\"/>"
    "</config></response>";

  config.ClearEvents();
  ASSERT_SUCCESS(config.Save(outFile));
  ASSERT_TRUE(CheckFileContents(outFile, kExpectedOutputNoEvents));

  ++gPassedTests;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

int gPassedTests = 0 [static]

Definition at line 58 of file TestMetricsConfig.cpp.

int gTotalTests = 0 [static]

Definition at line 57 of file TestMetricsConfig.cpp.