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

mwm_url_tests.cpp « map_tests « map - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8f295fded188e0527bb4bc26d546f79dba80ede8 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "testing/testing.hpp"

#include "map/framework.hpp"
#include "map/mwm_url.hpp"

#include "coding/uri.hpp"

#include "base/string_format.hpp"

#include "std/random.hpp"

using namespace url_scheme;

namespace
{
  void ToMercatoToLatLon(double & lat, double & lon)
  {
    lon = MercatorBounds::XToLon(MercatorBounds::LonToX(lon));
    lat = MercatorBounds::YToLat(MercatorBounds::LatToY(lat));
  }

  const UserMarkContainer::Type type = UserMarkContainer::API_MARK;
  class ApiTest
  {
  public:
    ApiTest(string const & uriString)
    {
      m_m = &m_fm.GetBookmarkManager();
      m_c = &m_m->UserMarksGetController(type);
      m_api.SetController(m_c);
      m_api.SetUriAndParse(uriString);
    }

    bool IsValid() const { return m_api.IsValid(); }
    m2::RectD GetViewport()
    {
      m2::RectD rect;
      ScalesProcessor scales;
      m_api.GetViewportRect(scales, rect);
      return rect;
    }
    string const & GetAppTitle() { return m_api.GetAppTitle(); }
    bool GoBackOnBalloonClick() { return m_api.GoBackOnBalloonClick(); }
    int GetPointCount() { return m_c->GetUserMarkCount(); }
    string const & GetGlobalBackUrl() { return m_api.GetGlobalBackUrl(); }
    int GetApiVersion() { return m_api.GetApiVersion(); }
    bool TestLatLon(int index, double lat, double lon) const
    {
      double tLat, tLon;
      GetMark(index)->GetLatLon(tLat, tLon);
      return my::AlmostEqualULPs(tLat, lat) && my::AlmostEqualULPs(tLon, lon);
    }

    bool TestName(int index, string const & name)
    {
      return GetMark(index)->GetName() == name;
    }

    bool TestID(int index, string const & id)
    {
      return GetMark(index)->GetID() == id;
    }

  private:
    ApiMarkPoint const * GetMark(int index) const
    {
      TEST_LESS(index, m_c->GetUserMarkCount(), ());
      return static_cast<ApiMarkPoint const *>(m_c->GetUserMark(index));
    }

  private:
    Framework m_fm;
    ParsedMapApi m_api;
    UserMarkContainer::Controller * m_c;
    BookmarkManager * m_m;
  };

  bool IsValid(Framework & fm, string const & uriStrig)
  {
    ParsedMapApi api;
    UserMarkContainer::Type type = UserMarkContainer::API_MARK;
    api.SetController(&fm.GetBookmarkManager().UserMarksGetController(type));
    api.SetUriAndParse(uriStrig);
    bool res = api.IsValid();
    fm.GetBookmarkManager().UserMarksClear(type);
    return res;
  }
}

UNIT_TEST(MapApiSmoke)
{
  string uriString = "mapswithme://map?ll=38.970559,-9.419289&ignoreThisParam=Yes&z=17&n=Point%20Name";
  TEST(Uri(uriString).IsValid(), ());

  ApiTest test(uriString);

  TEST(test.IsValid(), ());
  TEST_EQUAL(test.GetPointCount(), 1, ());
  TEST(test.TestLatLon(0, 38.970559, -9.419289), ());
  TEST(test.TestName(0, "Point Name"), ());
  TEST(test.TestID(0, ""), ());
  TEST_EQUAL(test.GetGlobalBackUrl(), "", ());
}

UNIT_TEST(MapApiInvalidUrl)
{
  Framework fm;
  TEST(!IsValid(fm, "competitors://map?ll=12.3,34.54"), ());
  TEST(!IsValid(fm, "mapswithme://ggg?ll=12.3,34.54"), ());
  TEST(!IsValid(fm, "mwm://"), ("No parameters"));
  TEST(!IsValid(fm, "mapswithme://map?"), ("No longtitude"));
  TEST(!IsValid(fm, "mapswithme://map?ll=1,2,3"), ("Too many values for ll"));
}

UNIT_TEST(MapApiLatLonLimits)
{
  Framework fm;
  TEST(!IsValid(fm, "mapswithme://map?ll=-91,10"), ("Invalid latitude"));
  TEST(!IsValid(fm, "mwm://map?ll=523.55,10"), ("Invalid latitude"));
  TEST(!IsValid(fm, "mapswithme://map?ll=23.55,450"), ("Invalid longtitude"));
  TEST(!IsValid(fm, "mapswithme://map?ll=23.55,-450"), ("Invalid longtitude"));
}

UNIT_TEST(MapApiPointNameBeforeLatLon)
{
  ApiTest test("mapswithme://map?n=Name&ll=1,2");
  TEST(test.IsValid(), ());
  TEST_EQUAL(test.GetPointCount(), 1, ());
  TEST(test.TestName(0, ""), ());
}

UNIT_TEST(MapApiPointNameOverwritten)
{
  ApiTest api("mapswithme://map?ll=1,2&n=A&N=B");
  TEST(api.IsValid(), ());
  TEST_EQUAL(api.GetPointCount(), 1, ());
  TEST(api.TestName(0, "B"), ());
}

UNIT_TEST(MapApiMultiplePoints)
{
  ApiTest api("mwm://map?ll=1.1,1.2&n=A&LL=2.1,2.2&ll=-3.1,-3.2&n=C");
  TEST(api.IsValid(), ());
  TEST_EQUAL(api.GetPointCount(), 3, ());
  TEST(api.TestLatLon(2, 1.1, 1.2), ());
  TEST(api.TestName(2, "A"), ());
  TEST(api.TestLatLon(1, 2.1, 2.2), ());
  TEST(api.TestName(1, ""), ());
  TEST(api.TestLatLon(0, -3.1, -3.2), ());
  TEST(api.TestName(0, "C"), ());
}

UNIT_TEST(MapApiInvalidPointLatLonButValidOtherParts)
{
  ApiTest api("mapswithme://map?ll=1,1,1&n=A&ll=2,2&n=B&ll=3,3,3&n=C");
  TEST(api.IsValid(), ());
  TEST_EQUAL(api.GetPointCount(), 1, ());
  TEST(api.TestLatLon(0, 2, 2), ());
  TEST(api.TestName(0, "B"), ());
}

UNIT_TEST(MapApiPointURLEncoded)
{
  ApiTest api("mwm://map?ll=1,2&n=%D0%9C%D0%B8%D0%BD%D1%81%D0%BA&id=http%3A%2F%2Fmap%3Fll%3D1%2C2%26n%3Dtest");
  TEST(api.IsValid(), ());
  TEST_EQUAL(api.GetPointCount(), 1, ());
  TEST(api.TestName(0, "\xd0\x9c\xd0\xb8\xd0\xbd\xd1\x81\xd0\xba"), ());
  TEST(api.TestID(0, "http://map?ll=1,2&n=test"), ());
}

UNIT_TEST(GlobalBackUrl)
{
  {
    ApiTest api("mwm://map?ll=1,2&n=PointName&backurl=someTestAppBackUrl");
    TEST_EQUAL(api.GetGlobalBackUrl(), "someTestAppBackUrl://", ());
  }
  {
    ApiTest api("mwm://map?ll=1,2&n=PointName&backurl=ge0://");
    TEST_EQUAL(api.GetGlobalBackUrl(), "ge0://", ());
  }
  {
    ApiTest api("mwm://map?ll=1,2&n=PointName&backurl=ge0%3A%2F%2F");
    TEST_EQUAL(api.GetGlobalBackUrl(), "ge0://", ());
  }
  {
    ApiTest api("mwm://map?ll=1,2&n=PointName&backurl=http://mapswithme.com");
    TEST_EQUAL(api.GetGlobalBackUrl(), "http://mapswithme.com", ());
  }
  {
    ApiTest api("mwm://map?ll=1,2&n=PointName&backUrl=someapp://%D0%9C%D0%BE%D0%B1%D0%B8%D0%BB%D1%8C%D0%BD%D1%8B%D0%B5%20%D0%9A%D0%B0%D1%80%D1%82%D1%8B");
    TEST_EQUAL(api.GetGlobalBackUrl(), "someapp://\xd0\x9c\xd0\xbe\xd0\xb1\xd0\xb8\xd0\xbb\xd1\x8c\xd0\xbd\xd1\x8b\xd0\xb5 \xd0\x9a\xd0\xb0\xd1\x80\xd1\x82\xd1\x8b", ());
  }
  {
    ApiTest api("mwm://map?ll=1,2&n=PointName");
    TEST_EQUAL(api.GetGlobalBackUrl(), "", ());
  }
  {
    ApiTest api("mwm://map?ll=1,2&n=PointName&backurl=%D0%BF%D1%80%D0%B8%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5%3A%2F%2F%D0%BE%D1%82%D0%BA%D1%80%D0%BE%D0%B9%D0%A1%D1%81%D1%8B%D0%BB%D0%BA%D1%83");
    TEST_EQUAL(api.GetGlobalBackUrl(), "приложение://откройСсылку", ());
  }
  {
    ApiTest api("mwm://map?ll=1,2&n=PointName&backurl=%D0%BF%D1%80%D0%B8%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5%3A%2F%2F%D0%BE%D1%82%D0%BA%D1%80%D0%BE%D0%B9%D0%A1%D1%81%D1%8B%D0%BB%D0%BA%D1%83");
    TEST_EQUAL(api.GetGlobalBackUrl(), "приложение://откройСсылку", ());
  }
  {
    ApiTest api("mwm://map?ll=1,2&n=PointName&backurl=%E6%88%91%E6%84%9Bmapswithme");
    TEST_EQUAL(api.GetGlobalBackUrl(), "我愛mapswithme://", ());
  }
}

UNIT_TEST(VersionTest)
{
  {
    ApiTest api("mwm://map?ll=1,2&v=1&n=PointName");
    TEST_EQUAL(api.GetApiVersion(), 1, ());
  }
  {
    ApiTest api("mwm://map?ll=1,2&v=kotik&n=PointName");
    TEST_EQUAL(api.GetApiVersion(), 0, ());
  }
  {
    ApiTest api("mwm://map?ll=1,2&v=APacanyVoobsheKotjata&n=PointName");
    TEST_EQUAL(api.GetApiVersion(), 0, ());
  }
  {
    ApiTest api("mwm://map?ll=1,2&n=PointName");
    TEST_EQUAL(api.GetApiVersion(), 0, ());
  }
  {
    ApiTest api("mwm://map?V=666&ll=1,2&n=PointName");
    TEST_EQUAL(api.GetApiVersion(), 666, ());
  }
}

UNIT_TEST(AppNameTest)
{
  {
    ApiTest api("mwm://map?ll=1,2&v=1&n=PointName&appname=Google");
    TEST_EQUAL(api.GetAppTitle(), "Google", ());
  }
  {
    ApiTest api("mwm://map?ll=1,2&v=1&n=PointName&AppName=%D0%AF%D0%BD%D0%B4%D0%B5%D0%BA%D1%81");
    TEST_EQUAL(api.GetAppTitle(), "Яндекс", ());
  }
  {
    ApiTest api("mwm://map?ll=1,2&v=1&n=PointName");
    TEST_EQUAL(api.GetAppTitle(), "", ());
  }
}

namespace
{
string generatePartOfUrl(url_scheme::ApiPoint const & point)
{
  stringstream stream;
  stream << "&ll=" << strings::ToString(point.m_lat)  << "," << strings::ToString(point.m_lon)
         << "&n=" << point.m_name
         << "&id=" << point.m_id;
  return stream.str();
}

string randomString(size_t size, size_t seed)
{
  string result(size, '0');
  mt19937 rng(seed);
  for (size_t i = 0; i < size; ++i)
    result[i] = 'a' + rng() % 26;
  return result;
}

void generateRandomTest(size_t numberOfPoints, size_t stringLength)
{
  vector <url_scheme::ApiPoint> vect(numberOfPoints);
  for (size_t i = 0; i < numberOfPoints; ++i)
  {
    url_scheme::ApiPoint point;
    mt19937 rng(i);
    point.m_lat = rng() % 90;
    point.m_lat *= rng() % 2 == 0 ? 1 : -1;
    point.m_lon = rng() % 180;
    point.m_lon *= rng() % 2 == 0 ? 1 : -1;
    point.m_name = randomString(stringLength, i);
    point.m_id = randomString(stringLength, i);
    vect[i] = point;
  }
  string result = "mapswithme://map?v=1";
  for (size_t i = 0; i < vect.size(); ++i)
    result += generatePartOfUrl(vect[i]);

  ApiTest api(result);
  TEST_EQUAL(api.GetPointCount(), vect.size(), ());
  for (size_t i = 0; i < vect.size();++i)
  {
    /// Mercator defined not on all range of lat\lon values.
    /// Some part of lat\lon is clamp on convertation
    /// By this we convert  source data lat\lon to mercator and then into lat\lon
    /// to emulate core convertions
    double lat = vect[i].m_lat;
    double lon = vect[i].m_lon;
    ToMercatoToLatLon(lat, lon);
    size_t const ix = vect.size() - i - 1;
    TEST(api.TestLatLon(ix, lat, lon), ());
    TEST(api.TestName(ix, vect[i].m_name), ());
    TEST(api.TestID(ix, vect[i].m_id), ());
  }
  TEST_EQUAL(api.GetApiVersion(), 1, ());
}

}

UNIT_TEST(100FullEnteriesRandomTest)
{
  generateRandomTest(100, 10);
}

UNIT_TEST(StressTestRandomTest)
{
  generateRandomTest(10000, 100);
}

UNIT_TEST(MWMApiBalloonActionDefaultTest)
{
  {
    ApiTest api("mapswithme://map?ll=38.970559,-9.419289&ignoreThisParam=Yes&z=17&n=Point%20Name");
    TEST(!api.GoBackOnBalloonClick(), (""));
  }
  {
    ApiTest api("mapswithme://map?ll=38.970559,-9.419289&ignoreThisParam=Yes&z=17&n=Point%20Name&balloonAction=false");
    TEST(api.GoBackOnBalloonClick(), (""));
  }
  {
    ApiTest api("mapswithme://map?ll=38.970559,-9.419289&ignoreThisParam=Yes&z=17&n=Point%20Name&balloonAction=true");
    TEST(api.GoBackOnBalloonClick(), (""));
  }
  {
    ApiTest api("mapswithme://map?ll=38.970559,-9.419289&ignoreThisParam=Yes&z=17&n=Point%20Name&balloonAction=");
    TEST(api.GoBackOnBalloonClick(), (""));
  }
}