summaryrefslogtreecommitdiffstats
path: root/mobile/android/tests/browser/chrome/test_home_provider.html
blob: 542cd82c0a74db77af35d0b8c2b10aabfc4baeb2 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=942288
Migrated from Robocop: https://bugzilla.mozilla.org/show_bug.cgi?id=1184186
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 942288</title>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
  <script type="application/javascript;version=1.7">

  const { utils: Cu } = Components;

  Cu.import("resource://gre/modules/HomeProvider.jsm");
  Cu.import("resource://gre/modules/osfile.jsm");
  Cu.import("resource://gre/modules/Services.jsm");
  Cu.import("resource://gre/modules/Sqlite.jsm");
  Cu.import("resource://gre/modules/Task.jsm");

  const TEST_DATASET_ID = "test-dataset-id";
  const TEST_URL = "http://test.com";
  const TEST_TITLE = "Test";
  const TEST_BACKGROUND_URL = "http://example.com/background";
  const TEST_BACKGROUND_COLOR = "#FF9500";

  const PREF_SYNC_CHECK_INTERVAL_SECS = "home.sync.checkIntervalSecs";
  const TEST_INTERVAL_SECS = 1;

  const DB_PATH = OS.Path.join(OS.Constants.Path.profileDir, "home.sqlite");

  test_request_sync();
  test_periodic_sync();

  function test_request_sync() {
    // The current implementation of requestSync is synchronous.
    let success = HomeProvider.requestSync(TEST_DATASET_ID, function callback(datasetId) {
      is(datasetId, TEST_DATASET_ID, "expected dataset ID");
    });

    ok(success, "requestSync success");
  }

  function test_periodic_sync() {
    SimpleTest.registerCleanupFunction(function() {
      Services.prefs.clearUserPref(PREF_SYNC_CHECK_INTERVAL_SECS);
      HomeProvider.removePeriodicSync(TEST_DATASET_ID);
    });

    // Lower the check interval for testing purposes.
    Services.prefs.setIntPref(PREF_SYNC_CHECK_INTERVAL_SECS, TEST_INTERVAL_SECS);

    HomeProvider.addPeriodicSync(TEST_DATASET_ID, TEST_INTERVAL_SECS, function callback(datasetId) {
      is(datasetId, TEST_DATASET_ID, "expected dataset ID");
    });
  }

  add_task(function* test_save_and_delete() {
    // Use the HomeProvider API to save some data.
    let storage = HomeProvider.getStorage(TEST_DATASET_ID);
    yield storage.save([{
      title: TEST_TITLE,
      url: TEST_URL,
      background_url: TEST_BACKGROUND_URL,
      background_color: TEST_BACKGROUND_COLOR
    }]);

    // Peek in the DB to make sure we have the right data.
    let db = yield Sqlite.openConnection({ path: DB_PATH });

    // Make sure the items table was created.
    ok((yield db.tableExists("items")), "items table exists");

    // Make sure the correct values for the item ended up in there.
    let result = yield db.execute("SELECT * FROM items", null, function onRow(row){
      is(row.getResultByName("dataset_id"), TEST_DATASET_ID, "expected dataset ID");
      is(row.getResultByName("url"), TEST_URL, "expected test url");
      is(row.getResultByName("background_url"), TEST_BACKGROUND_URL, "expected background url");
      is(row.getResultByName("background_color"), TEST_BACKGROUND_COLOR, "expected background color");
    });

    // Use the HomeProvider API to delete the data.
    yield storage.deleteAll();

    // Make sure the data was deleted.
    result = yield db.execute("SELECT * FROM items");
    is(result.length, 0, "length is 0");

    db.close();
  });

  add_task(function* test_row_validation() {
    // Use the HomeProvider API to save some data.
    let storage = HomeProvider.getStorage(TEST_DATASET_ID);

    let invalidRows = [
      { url: "url" },
      { title: "title" },
      { description: "description" },
      { image_url: "image_url" }
    ];

    // None of these save calls should save anything
    for (let row of invalidRows) {
      try {
        yield storage.save([row]);
      } catch (e if e instanceof HomeProvider.ValidationError) {
        // Just catch and ignore validation errors
      }
    }

    // Peek in the DB to make sure we have the right data.
    let db = yield Sqlite.openConnection({ path: DB_PATH });

    // Make sure no data has been saved.
    let result = yield db.execute("SELECT * FROM items");
    is(result.length, 0, "length is 0");

    db.close();
  });

  add_task(function* test_save_transaction() {
    // Use the HomeProvider API to save some data.
    let storage = HomeProvider.getStorage(TEST_DATASET_ID);

    // One valid, one invalid
    let rows = [
      { title: TEST_TITLE, url: TEST_URL },
      { image_url: "image_url" }
    ];

    // Try to save all the rows at once
    try {
      yield storage.save(rows);
    } catch (e if e instanceof HomeProvider.ValidationError) {
      // Just catch and ignore validation errors
    }

    // Peek in the DB to make sure we have the right data.
    let db = yield Sqlite.openConnection({ path: DB_PATH });

    // Make sure no data has been saved.
    let result = yield db.execute("SELECT * FROM items");
    is(result.length, 0, "length is 0");

    db.close();
  });

  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=942288">Mozilla Bug 942288</a>
<br>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1184186">Migrated from Robocop testHomeProvider</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>