aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/xtremelabs/robolectric/shadows/ShadowIntent.java
blob: 11289602d291a0ecbd9237bf8a8594ee94bc9e1e (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
package com.xtremelabs.robolectric.shadows;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.internal.RealObject;
import com.xtremelabs.robolectric.util.Join;

import java.io.*;
import java.util.*;

import static android.content.Intent.*;
import static com.xtremelabs.robolectric.Robolectric.shadowOf;

@SuppressWarnings({"UnusedDeclaration"})
@Implements(Intent.class)
public class ShadowIntent {
    @RealObject private Intent realIntent;

    private final Bundle extras = new Bundle();
    private String action;
    private ComponentName componentName;
    private String type;
    private Uri data;
    private int flags;
    private Class<?> intentClass;
    private String packageName;
    private final Set<String> categories = new HashSet<String>();
    private String uri;

    public void __constructor__(Context packageContext, Class cls) {
        componentName = new ComponentName(packageContext, cls);
        intentClass = cls;
    }

    public void __constructor__(String action, Uri uri) {
        this.action = action;
        data = uri;
    }

    public void __constructor__(String action) {
        __constructor__(action, null);
    }

    public void __constructor__(Intent intent) {
        ShadowIntent other = shadowOf(intent);
        extras.putAll(other.extras);
        action = other.action;
        componentName = other.componentName;
        type = other.type;
        data = other.data;
        flags = other.flags;
        intentClass = other.intentClass;
        packageName = other.packageName;
        categories.addAll(other.categories);
        uri = other.uri;
    }

    @Implementation
    public static Intent createChooser(Intent target, CharSequence title) {
        Intent intent = new Intent(Intent.ACTION_CHOOSER);
        intent.putExtra(Intent.EXTRA_INTENT, target);
        if (title != null) {
            intent.putExtra(Intent.EXTRA_TITLE, title);
        }
        return intent;
    }

    @Implementation
    public Intent setAction(String action) {
        this.action = action;
        return realIntent;
    }

    @Implementation
    public String getAction() {
        return action;
    }

    @Implementation
    public Intent setType(String type) {
        this.type = type;
        this.data = null;
        return realIntent;
    }

    @Implementation
    public Intent setDataAndType(Uri data, String type) {
        this.data = data;
        this.type = type;
        return realIntent;
    }

    @Implementation
    public String getType() {
        return type;
    }

    @Implementation
    public Intent addCategory(String category) {
        categories.add(category);
        return realIntent;
    }

    @Implementation
    public void removeCategory(String category) {
        categories.remove(category);
    }

    @Implementation
    public boolean hasCategory(String category) {
        return categories.contains(category);
    }

    @Implementation
    public Set<String> getCategories() {
        return categories;
    }

    @Implementation
    public Intent setPackage(String packageName) {
        this.packageName = packageName;
        return realIntent;
    }

    @Implementation
    public String getPackage() {
        return packageName;
    }

    @Implementation
    public Uri getData() {
        return data;
    }

    @Implementation
    public Intent setClass(Context packageContext, Class<?> cls) {
        this.intentClass = cls;
        return realIntent;
    }

    @Implementation
    public Intent setClassName(String packageName, String className) {
        componentName = new ComponentName(packageName, className);
        try {
            this.intentClass = Class.forName(className);
        } catch (ClassNotFoundException e) {
            // ignore
        }
        return realIntent;
    }

    @Implementation
    public Intent setClassName(Context packageContext, String className) {
        componentName = new ComponentName(packageContext.getPackageName(), className);
        return realIntent;
    }

    @Implementation
    public Intent setData(Uri data) {
        this.data = data;
        this.type = null;
        return realIntent;
    }

    @Implementation
    public int getFlags() {
        return flags;
    }

    @Implementation
    public Intent setFlags(int flags) {
        this.flags = flags;
        return realIntent;
    }

    @Implementation
    public Intent addFlags(int flags) {
        this.flags |= flags;
        return realIntent;
    }

    @Implementation
    public Intent putExtras(Bundle src) {
        extras.putAll(src);
        return realIntent;
    }

    @Implementation
    public Intent putExtras(Intent src) {
        ShadowIntent srcShadowIntent = shadowOf(src);
        extras.putAll(srcShadowIntent.extras);
        return realIntent;
    }

    @Implementation
    public Bundle getExtras() {
        return extras != null ? new Bundle(extras) : null;
    }

    @Implementation
    public Intent putExtra(String key, int value) {
        extras.putInt(key, value);
        return realIntent;
    }

    @Implementation
    public Intent putExtra(String key, double value) {
        extras.putDouble(key, value);
        return realIntent;
    }

    @Implementation
    public Intent putExtra(String key, float value) {
        extras.putFloat(key, value);
        return realIntent;
    }

    @Implementation
    public Intent putExtra(String key, long value) {
        extras.putLong(key, value);
        return realIntent;
    }

    @Implementation
    public Intent putExtra(String key, Serializable value) {
        extras.putSerializable(key, serializeCycle(value));
        return realIntent;
    }

    @Implementation
    public Intent putExtra(String key, Parcelable value) {
        extras.putParcelable(key, value);
        return realIntent;
    }

    @Implementation
    public Intent putExtra(String key, Parcelable[] value) {
        extras.putParcelableArray(key, value);
        return realIntent;
    }

    @Implementation
    public Intent putExtra(String key, String value) {
        extras.putString(key, value);
        return realIntent;
    }

    @Implementation
    public Intent putExtra(String key, String[] value) {
        extras.putStringArray(key, value);
        return realIntent;
    }

    @Implementation
    public Intent putExtra(String key, Bundle value) {
        extras.putBundle(key, value);
        return realIntent;
    }

    @Implementation
    public Intent putExtra(String key, boolean value) {
        extras.putBoolean(key, value);
        return realIntent;
    }

    @Implementation
    public Intent putExtra(String key, int[] value) {
        extras.putIntArray(key, value);
        return realIntent;
    }

    @Implementation
    public Intent putExtra(String key, long[] value) {
        extras.putLongArray(key, value);
        return realIntent;
    }

    @Implementation
    public int[] getIntArrayExtra(String name) {
        return extras.getIntArray(name);
    }

    @Implementation
    public long[] getLongArrayExtra(String name) {
        return extras.getLongArray(name);
    }

    @Implementation
    public boolean getBooleanExtra(String name, boolean defaultValue) {
        return extras.getBoolean(name, defaultValue);
    }

    @Implementation
    public String[] getStringArrayExtra(String name) {
        return extras.getStringArray(name);
    }

    @Implementation
    public Intent putExtra(String key, CharSequence value) {
        extras.putCharSequence(key, value);
        return realIntent;
    }

    @Implementation
    public CharSequence getCharSequenceExtra(String name) {
        return extras.getCharSequence(name);
    }

    @Implementation
    public void putExtra(String key, byte[] value) {
        extras.putByteArray(key, value);
    }

    @Implementation
    public Intent putStringArrayListExtra(String key, ArrayList<String> value) {
        extras.putStringArrayList(key, value);
        return realIntent;
    }

    @Implementation
    public ArrayList<String> getStringArrayListExtra(String name) {
        return extras.getStringArrayList(name);
    }

    @Implementation
    public Intent putIntegerArrayListExtra(String key, ArrayList<Integer> value) {
        extras.putIntegerArrayList(key, value);
        return realIntent;
    }

    @Implementation
    public ArrayList<Integer> getIntegerArrayListExtra(String name) {
        return extras.getIntegerArrayList(name);
    }

    @Implementation
    public Intent putParcelableArrayListExtra(String key, ArrayList<Parcelable> value) {
        extras.putParcelableArrayList(key, value);
        return realIntent;
    }

    @Implementation
    public ArrayList<Parcelable> getParcelableArrayListExtra(String key) {
        return extras.getParcelableArrayList(key);
    }

    @Implementation
    public boolean hasExtra(String name) {
        return extras.containsKey(name);
    }

    @Implementation
    public String getStringExtra(String name) {
        return extras.getString(name);
    }

    @Implementation
    public Parcelable getParcelableExtra(String name) {
        return extras.getParcelable(name);
    }

    @Implementation
    public Parcelable[] getParcelableArrayExtra(String name) {
        return extras.getParcelableArray(name);
    }

    @Implementation
    public int getIntExtra(String name, int defaultValue) {
        return extras.getInt(name, defaultValue);
    }

    @Implementation
    public long getLongExtra(String name, long defaultValue) {
        return extras.getLong(name, defaultValue);
    }

    @Implementation
    public double getDoubleExtra(String name, double defaultValue) {
        return extras.getDouble(name, defaultValue);
    }

    @Implementation
    public Bundle getBundleExtra(String name) {
        return extras.getBundle(name);
    }

    @Implementation
    public float getFloatExtra(String name, float defaultValue) {
        return extras.getFloat(name, defaultValue);
    }

    @Implementation
    public byte[] getByteArrayExtra(String name) {
        return extras.getByteArray(name);
    }

    @Implementation
    public Serializable getSerializableExtra(String name) {
        return extras.getSerializable(name);
    }

    @Implementation
    public void removeExtra(String name) {
        extras.remove(name);
    }

    @Implementation
    public Intent setComponent(ComponentName componentName) {
        this.componentName = componentName;
        return realIntent;
    }

    @Implementation
    public ComponentName getComponent() {
        return componentName;
    }

    @Implementation
    public String toURI() {
        return uri;
    }

    @Implementation
    public int fillIn(Intent otherIntent, int flags) {
        int changes = 0;
        ShadowIntent other = shadowOf(otherIntent);

        if (other.action != null && (action == null || (flags & FILL_IN_ACTION) != 0)) {
            action = other.action;
            changes |= FILL_IN_ACTION;
        }
        if ((other.data != null || other.type != null)
                && ((data == null && type == null) || (flags & FILL_IN_DATA) != 0)) {
            data = other.data;
            type = other.type;
            changes |= FILL_IN_DATA;
        }
        if (!other.categories.isEmpty()
                && (categories.isEmpty() || (flags & FILL_IN_CATEGORIES) != 0)) {
            categories.addAll(other.categories);
            changes |= FILL_IN_CATEGORIES;
        }
        if (other.packageName != null
                && (packageName == null || (flags & FILL_IN_PACKAGE) != 0)) {
            packageName = other.packageName;
            changes |= FILL_IN_PACKAGE;
        }
        if (other.componentName != null && (flags & FILL_IN_COMPONENT) != 0) {
            componentName = other.componentName;
            changes |= FILL_IN_COMPONENT;
        }

        extras.putAll(other.extras);
        return changes;
    }

    @Implementation
    // cribbed from Android source
    public boolean filterEquals(Intent other) {
        if (other == null) {
            return false;
        }
        if (getAction() != other.getAction()) {
            if (getAction() != null) {
                if (!getAction().equals(other.getAction())) {
                    return false;
                }
            } else {
                if (!other.getAction().equals(getAction())) {
                    return false;
                }
            }
        }
        if (getData() != other.getData()) {
            if (getData() != null) {
                if (!getData().equals(other.getData())) {
                    return false;
                }
            } else {
                if (!other.getData().equals(getData())) {
                    return false;
                }
            }
        }
        if (getType() != other.getType()) {
            if (getType() != null) {
                if (!getType().equals(other.getType())) {
                    return false;
                }
            } else {
                if (!other.getType().equals(getType())) {
                    return false;
                }
            }
        }
        if (getPackage() != other.getPackage()) {
            if (getPackage() != null) {
                if (!getPackage().equals(other.getPackage())) {
                    return false;
                }
            } else {
                if (!other.getPackage().equals(getPackage())) {
                    return false;
                }
            }
        }
        if (getComponent() != other.getComponent()) {
            if (getComponent() != null) {
                if (!getComponent().equals(other.getComponent())) {
                    return false;
                }
            } else {
                if (!other.getComponent().equals(getComponent())) {
                    return false;
                }
            }
        }
        if (getCategories() != other.getCategories()) {
            if (getCategories() != null) {
                if (!getCategories().equals(other.getCategories())) {
                    return false;
                }
            } else {
                if (!other.getCategories().equals(getCategories())) {
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * Compares an {@code Intent} with a {@code ShadowIntent} (obtained via a call to
     * {@link Robolectric#shadowOf(android.content.Intent)})
     *
     * @param o a {@code ShadowIntent}
     * @return whether they are equivalent
     */
    @Deprecated
    public boolean realIntentEquals(ShadowIntent o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        if (action != null ? !action.equals(o.action) : o.action != null) return false;
        if (componentName != null ? !componentName.equals(o.componentName) : o.componentName != null)
            return false;
        if (data != null ? !data.equals(o.data) : o.data != null) return false;
        if (extras != null ? !extras.equals(o.extras) : o.extras != null) return false;
        if (type != null ? !type.equals(o.type) : o.type != null) return false;
        if (categories != null ? !categories.equals(o.categories) : o.categories != null) return false;

        return true;
    }

    @Override
    @Implementation
    public int hashCode() {
        int result = extras != null ? extras.hashCode() : 0;
        result = 31 * result + (action != null ? action.hashCode() : 0);
        result = 31 * result + (componentName != null ? componentName.hashCode() : 0);
        result = 31 * result + (data != null ? data.hashCode() : 0);
        result = 31 * result + (type != null ? type.hashCode() : 0);
        result = 31 * result + flags;
        return result;
    }

    @Override
    @Implementation
    public boolean equals(Object o) {
        if (!(o instanceof Intent)) return false;
        return realIntentEquals(shadowOf((Intent) o));
    }

    /**
     * Non-Android accessor that returns the {@code Class} object set by
     * {@link #setClass(android.content.Context, Class)}
     *
     * @return the {@code Class} object set by
     *         {@link #setClass(android.content.Context, Class)}
     */
    public Class<?> getIntentClass() {
        return intentClass;
    }

    @Override
    @Implementation
    public String toString() {
        return "Intent{" +
                Join.join(
                        ", ",
                        ifWeHave(componentName, "componentName"),
                        ifWeHave(action, "action"),
                        ifWeHave(extras, "extras"),
                        ifWeHave(data, "data"),
                        ifWeHave(type, "type")
                ) +
                '}';
    }

    @Implementation
    public void writeToParcel(Parcel out, int flags) {
      writeStringToParcel(action, out);
      if (data != null) {
        out.writeInt(1);
        Uri.writeToParcel(out, data);
      } else {
        out.writeInt(0);
      }
      writeStringToParcel(type, out);
      out.writeInt(flags);
      writeStringToParcel(packageName, out);
      ComponentName.writeToParcel(componentName, out);
      out.writeInt(categories.size());
      for (String category : categories) {
        out.writeString(category);
      }
      out.writeBundle(extras);
    }

    @Implementation
    public void readFromParcel(Parcel in) {
      setAction(readStringFromParcel(in));
      if (in.readInt() != 0) {
        data = Uri.CREATOR.createFromParcel(in);
      }
      type = readStringFromParcel(in);
      flags = in.readInt();
      packageName = readStringFromParcel(in);
      componentName = ComponentName.readFromParcel(in);
      int N = in.readInt();
      for (int i = 0; i < N; i++) {
        categories.add(in.readString().intern());
      }
      extras.putAll(in.readBundle());
    }

    private void writeStringToParcel(String s, Parcel out) {
      if (s != null) {
        out.writeInt(1);
        out.writeString(s);
      } else {
        out.writeInt(0);
      }
    }

    private String readStringFromParcel(Parcel in) {
      if (in.readInt() != 0) {
        return in.readString();
      }
      return null;
    }

    private Serializable serializeCycle(Serializable serializable) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream output = new ObjectOutputStream(byteArrayOutputStream);
            output.writeObject(serializable);
            output.close();

            byte[] bytes = byteArrayOutputStream.toByteArray();
            ObjectInputStream input = new ObjectInputStream(new ByteArrayInputStream(bytes));
            return (Serializable) input.readObject();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private String ifWeHave(Object o, String name) {
        if (o == null) return null;
        if (o instanceof Map && ((Map) o).isEmpty()) return null;
        return name + "=" + o;
    }

    public void setURI(String uri) {
        this.uri = uri;
    }
}