Welcome to mirror list, hosted at ThFree Co, Russian Federation.

data_source_test.cpp « indexer_tests « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6bf21c00246135fbe18eca3e74f50f54d4000e73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "testing/testing.hpp"

#include "indexer/data_header.hpp"
#include "indexer/data_source.hpp"
#include "indexer/feature_source.hpp"
#include "indexer/mwm_set.hpp"

#include "coding/file_name_utils.hpp"
#include "coding/internal/file_data.hpp"

#include "platform/country_file.hpp"
#include "platform/local_country_file.hpp"
#include "platform/platform.hpp"

#include "base/logging.hpp"
#include "base/macros.hpp"
#include "base/scope_guard.hpp"
#include "base/stl_add.hpp"

#include "std/algorithm.hpp"
#include "std/bind.hpp"
#include "std/string.hpp"

using platform::CountryFile;
using platform::LocalCountryFile;

namespace
{
class DataSourceTest : public MwmSet::Observer
{
public:
  DataSourceTest() : m_dataSource(make_unique<FeatureSourceFactory>())
  {
    TEST(m_dataSource.AddObserver(*this), ());
  }

  ~DataSourceTest() override { TEST(m_dataSource.RemoveObserver(*this), ()); }

  void ExpectRegistered(platform::LocalCountryFile const & localFile)
  {
    AddEvent(m_expected, MwmSet::Event::TYPE_REGISTERED, localFile);
  }

  void ExpectUpdated(platform::LocalCountryFile const & newFile,
                     platform::LocalCountryFile const & oldFile)
  {
    AddEvent(m_expected, MwmSet::Event::TYPE_UPDATED, newFile, oldFile);
  }

  void ExpectDeregistered(platform::LocalCountryFile const & localFile)
  {
    AddEvent(m_expected, MwmSet::Event::TYPE_DEREGISTERED, localFile);
  }

  // Checks expectations and clears them.
  bool CheckExpectations()
  {
    bool ok = true;
    if (m_actual != m_expected)
    {
      LOG(LINFO, ("Check failed. Expected:", m_expected, "actual:", m_actual));
      ok = false;
    }
    m_actual.clear();
    m_expected.clear();
    return ok;
  }

  // MwmSet::Observer overrides:
  void OnMapRegistered(platform::LocalCountryFile const & localFile) override
  {
    AddEvent(m_actual, MwmSet::Event::TYPE_REGISTERED, localFile);
  }

  void OnMapUpdated(platform::LocalCountryFile const & newFile,
                    platform::LocalCountryFile const & oldFile) override
  {
    AddEvent(m_actual, MwmSet::Event::TYPE_UPDATED, newFile, oldFile);
  }

  void OnMapDeregistered(platform::LocalCountryFile const & localFile) override
  {
    AddEvent(m_actual, MwmSet::Event::TYPE_DEREGISTERED, localFile);
  }

protected:
  template <typename... TArgs>
  void AddEvent(vector<MwmSet::Event> & events, TArgs... args)
  {
    events.emplace_back(forward<TArgs>(args)...);
  }

  DataSource m_dataSource;
  vector<MwmSet::Event> m_expected;
  vector<MwmSet::Event> m_actual;
};
}  // namespace

UNIT_CLASS_TEST(DataSourceTest, Parse)
{
  UNUSED_VALUE(m_dataSource.RegisterMap(platform::LocalCountryFile::MakeForTesting("minsk-pass")));

  // Make sure that index is actually parsed.
  m_dataSource.ForEachInScale([](FeatureType &) { return; }, 15);
}

UNIT_CLASS_TEST(DataSourceTest, StatusNotifications)
{
  string const mapsDir = GetPlatform().WritableDir();
  CountryFile const country("minsk-pass");

  // These two classes point to the same file, but will be considered
  // by Index as distinct files because versions are artificially set
  // to different numbers.
  LocalCountryFile const file1(mapsDir, country, 1 /* version */);
  LocalCountryFile const file2(mapsDir, country, 2 /* version */);

  MwmSet::MwmId id1;

  // Checks that observers are triggered after map registration.
  {
    auto result = m_dataSource.RegisterMap(file1);
    TEST_EQUAL(MwmSet::RegResult::Success, result.second, ());

    id1 = result.first;
    TEST(id1.IsAlive(), ());

    ExpectRegistered(file1);
    TEST(CheckExpectations(), ());
  }

  // Checks that map can't registered twice.
  {
    auto result = m_dataSource.RegisterMap(file1);
    TEST_EQUAL(MwmSet::RegResult::VersionAlreadyExists, result.second, ());

    TEST(result.first.IsAlive(), ());
    TEST_EQUAL(id1, result.first, ());

    TEST(CheckExpectations(), ());
  }

  // Checks that observers are notified when map is updated.
  MwmSet::MwmId id2;
  {
    auto result = m_dataSource.RegisterMap(file2);
    TEST_EQUAL(MwmSet::RegResult::Success, result.second, ());

    id2 = result.first;
    TEST(id2.IsAlive(), ());
    TEST_NOT_EQUAL(id1, id2, ());

    ExpectUpdated(file2, file1);
    TEST(CheckExpectations(), ());
  }

  // Tries to deregister a map in presence of an active handle. Map
  // should be marked "to be removed" but can't be deregistered. After
  // leaving the inner block the map should be deregistered.
  {
    {
      MwmSet::MwmHandle const handle = m_dataSource.GetMwmHandleByCountryFile(country);
      TEST(handle.IsAlive(), ());

      TEST(!m_dataSource.DeregisterMap(country), ());
      TEST(CheckExpectations(), ());
    }

    ExpectDeregistered(file2);
    TEST(CheckExpectations(), ());
  }
}