Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/bin/test-trial3.py
blob: 76594c81af466091a2244041db454afb95234631 (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
import dbus
import os
import shutil
import popen2
import tempfile

DEFAULT_STORE = "/tmp/store1"

def _create_temp(content):
    pt, pp = tempfile.mkstemp(suffix=".txt")
    os.write(pt, content)
    os.close(pt)
    return pp


def _create_temp_odt(content):
    # This just isn't working on my system
    # first write the contents to a temp file, then convert it
    pt, pp = tempfile.mkstemp(suffix=".txt")
    os.write(pt, content)
    os.close(pt)
    
    f, temp_path = tempfile.mkstemp(suffix='.odt')
    del f
    # my abiword didn't support fd://0
    cmd = 'abiword --to=odt --to-name=%s %s' % (temp_path, pp)
    print cmd
    child_stdout, child_stdin, child_stderr = popen2.popen3(cmd)
    child_stdin.write(content)
    child_stdin.close()
    child_stdout.close()
    child_stderr.close()
    os.unlink(pp)

    return temp_path

def start():
    if os.path.exists(DEFAULT_STORE):
        os.system("rm -rf %s" % DEFAULT_STORE)

    bus = dbus.SessionBus()
    bobj = bus.get_object("org.laptop.sugar.DataStore",
                           "/org/laptop/sugar/DataStore")

    
    ds = dbus.Interface(bobj, dbus_interface='org.laptop.sugar.DataStore')

    mp = ds.mount("hg:%s" % DEFAULT_STORE,
                  dict(title="Primary Storage"))
    return ds, mp

def stop(ds):
    ds.stop()
    


ds, mp = start()
# Browse starts download
print "Download Simulation"
props = {'title': 'Downloading test.pdf from \nhttp://example.org/test.pdf.',
         'mime_type': 'application/pdf',
         'progress': '0',
         'mountpoint' : mp,
         }
uid, vid = ds.checkin(props, '')
print 'Created download: %s %s' % (uid, vid)
assert uid
assert vid == '1'
ds.complete_indexing()

# Browse notifies the DS about the progress
props['uid'] = uid
for i in range(1, 5):
    props['progress'] = str(i * 20)
    props['vid'] = vid
    uid, vid = ds.checkin(props, '')
    print 'Updated download: %s %s %s' % (uid, vid, props['progress'])
    assert uid
    ds.complete_indexing()

# Browse finishes the download
# Now assume we have a file called tests/test.pdf (which there is if
# this is run from the project root)
# Checkin Gives the file to the datastore, it no longer owns it and
# will be removed when the datastore is done with it
source_path = "tests/test.pdf"
dest_path = "/tmp/test.pdf"
# we don't want it to kill our test file so we copy it to /tmp
shutil.copy(source_path, dest_path)

props['title'] = 'File test.pdf downloaded from\nhttp://example.org/test.pdf.'
props['progress'] = '100'
uid, vid = ds.checkin(props, dest_path)
ds.complete_indexing()
print 'Updated download with file: %s %s %s' % (uid, vid, props['progress'])
assert uid
#assert vid == '1'


# Check the DS has removed the file.
assert not os.path.exists(dest_path)

# Journal displays the download
objects, count = ds.find({'title': 'downloaded', 'order_by' : ['-vid']})
props = objects[0]
print 'Query returned: %s' % props['uid'], count
assert props['vid'] == vid, """%s != %s""" % (props['vid'], vid) # the last rev

# Read resumes the entry
props, file_path = ds.checkout(uid, '', '', '', '')
print 'Entry checked out: %s %s\n%s' % (uid, file_path, props)
assert props
assert props['vid'] == vid
assert file_path

# Read saves position and zoom level
props['position'] = '15'
props['zoom_level'] = '150'
props['activity'] = 'org.laptop.sugar.Read'


uid, nvid = ds.checkin(props, file_path)
print 'Updated Read state: %s %s' % (uid, nvid)
assert uid
assert nvid != "1"
assert nvid > vid, "%s < %s" %(nvid, vid)
ds.complete_indexing()


print "DONE"
#stop(ds)

# test_writing
#ds, mp = start()
print "Writing test"
# Create empty entry
props = {'title': 'Write activity', 'mountpoint' : mp}
uid, vid = ds.checkin(props, '')
print 'Created entry: %s %s' % (uid, vid)
assert uid
assert vid == '1'

ds.complete_indexing()

# First checkout
props, file_path = ds.checkout(uid, '', '', '', '')
print 'Entry checked out: %s %s' % (uid, file_path)
assert props
assert props['vid'] == '1'
assert file_path == ''


# Write first contents
file_path = _create_temp('blah blah 1')

props['mountpoint'] = mp
props['mime_type'] = 'text/plain'
uid, vid = ds.checkin(props, file_path)
ds.complete_indexing()
print 'First checkin: %s %s %s' % (uid, vid, file_path)
assert uid
#assert vid == '1'
assert not os.path.exists(file_path)
        
# Second checkout
props, file_path = ds.checkout(uid, '', '', '', '')
print 'Entry checked out: %s %s' % (props['uid'], file_path)
assert props
#assert props['vid'] == '1'
assert file_path

# Write second contents
file_path = _create_temp('blah blah 1\nbleh bleh 2')
props['mime_type'] = 'text/plain'
props['mountpoint'] = mp
uid, vid = ds.checkin(props, file_path)
ds.complete_indexing()
print 'Second checkin: %s %s %s' % (uid, vid, file_path)
assert uid
#assert vid == '2'
assert not os.path.exists(file_path)

print "DONE"
#stop(ds)


# PREVIEW test
#ds, mp = start()
print "preview test"
props = {'title': 'Write activity',
         'preview': dbus.ByteArray('\123\456\789\000\123'),
         'mountpoint' : mp}
uid, vid = ds.checkin(props, '')
print 'Created entry: %s %s' % (uid, vid)
assert uid
assert vid == '1'

props, file_path = ds.checkout(uid, '', '', '', '')
print props

print "DONE"
#stop(ds)