~haggai-eran/ubuntu/precise/nux/rtl-fallback-to-unity-2d

« back to all changes in this revision

Viewing changes to tests/gtest-nuxcore-async-file-writer.cpp

  • Committer: Package Import Robot
  • Author(s): Didier Roche
  • Date: 2012-03-12 11:45:31 UTC
  • mfrom: (1.1.37)
  • Revision ID: package-import@ubuntu.com-20120312114531-3zctml10fifuiap8
Tags: 2.6.0-0ubuntu1
* New upstream release.
  - Restore OpenGL default line width and point size (LP: #937444)
  - [unity-5.6] can't enter accents (^o->) in the dash since recent updates
    (LP: #944674)
  - SetEnableView / ViewEnable / ViewDisable give inverted results
    (LP: #938823)
* debian/control:
  - build-dep on libibus-1.0-dev for ibus support

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <string>
 
2
#include <fstream>
 
3
 
 
4
#include <iostream>
 
5
 
 
6
#include <gmock/gmock.h>
 
7
 
 
8
#include <boost/filesystem.hpp>
 
9
 
 
10
#include <glib.h>
 
11
 
 
12
#include "NuxCore/AsyncFileWriter.h"
 
13
 
 
14
#include "Helpers.h"
 
15
 
 
16
namespace bf = boost::filesystem;
 
17
using namespace testing;
 
18
using namespace nux::testing;
 
19
 
 
20
namespace {
 
21
 
 
22
const std::string TEST_ROOT("/tmp/nux-test-cases");
 
23
 
 
24
 
 
25
class TestAsyncfileWriter : public ::testing::Test
 
26
{
 
27
protected:
 
28
  virtual void SetUp() {
 
29
    // Make sure that the tests start with and empty TEST_ROOT.
 
30
    bf::remove_all(TEST_ROOT);
 
31
    bf::create_directories(TEST_ROOT);
 
32
  }
 
33
 
 
34
  virtual void TearDown() {
 
35
    // Delete the unity test directory
 
36
    bf::remove_all(TEST_ROOT);
 
37
  }
 
38
 
 
39
  bool WaitForOpen(nux::AsyncFileWriter& writer, unsigned timeout = 5) {
 
40
    TestCallback opened;
 
41
    TestCallback timed_out;
 
42
    g_timeout_add_seconds(timeout, &TestCallback::glib_callback, &timed_out);
 
43
    writer.opened.connect(opened.sigc_callback());
 
44
 
 
45
    while (!opened.happened && !timed_out.happened) {
 
46
      PumpGObjectMainLoop();
 
47
    }
 
48
    return opened.happened;
 
49
  }
 
50
 
 
51
  bool WaitForClose(nux::AsyncFileWriter& writer, unsigned timeout = 5) {
 
52
    TestCallback closed;
 
53
    TestCallback timed_out;
 
54
    g_timeout_add_seconds(timeout, &TestCallback::glib_callback, &timed_out);
 
55
    writer.closed.connect(closed.sigc_callback());
 
56
 
 
57
    while (!closed.happened && !timed_out.happened) {
 
58
      PumpGObjectMainLoop();
 
59
    }
 
60
    return closed.happened;
 
61
  }
 
62
 
 
63
};
 
64
 
 
65
TEST_F(TestAsyncfileWriter, TestConstructor) {
 
66
  std::string filename(TEST_ROOT + "/empty-file");
 
67
  {
 
68
    nux::AsyncFileWriter writer(filename);
 
69
    bool opened = WaitForOpen(writer);
 
70
    EXPECT_TRUE(opened);
 
71
  }
 
72
  EXPECT_TRUE(bf::exists(filename));
 
73
  EXPECT_THAT(ReadFile(filename), Eq(""));
 
74
}
 
75
 
 
76
TEST_F(TestAsyncfileWriter, TestWrites) {
 
77
  std::string filename(TEST_ROOT + "/write-file");
 
78
  std::string data(200, 'x');
 
79
  {
 
80
    nux::AsyncFileWriter writer(filename);
 
81
    writer.Write(data);
 
82
    writer.Close();
 
83
    bool closed = WaitForClose(writer);
 
84
    EXPECT_TRUE(closed);
 
85
  }
 
86
  EXPECT_THAT(ReadFile(filename), Eq(data));
 
87
}
 
88
 
 
89
TEST_F(TestAsyncfileWriter, TestWriteLots) {
 
90
  std::string filename(TEST_ROOT + "/lots-file");
 
91
  std::string data(200, 'x');
 
92
  const int loop_count = 1000;
 
93
  {
 
94
    nux::AsyncFileWriter writer(filename);
 
95
    for (int i = 0; i < loop_count; ++i) {
 
96
      writer.Write(data);
 
97
    }
 
98
    writer.Close();
 
99
    bool closed = WaitForClose(writer);
 
100
    EXPECT_TRUE(closed);
 
101
  }
 
102
  std::string file_content = ReadFile(filename);
 
103
  EXPECT_THAT(file_content.size(), Eq(data.size() * loop_count));
 
104
  // They are all x's.
 
105
  EXPECT_THAT(file_content, MatchesRegex("^x+$"));
 
106
}
 
107
 
 
108
 
 
109
 
 
110
} // anon namespace