summaryrefslogtreecommitdiff
path: root/base/test/android/junit/src/org/chromium/base/test/params/ExampleParameterizedTest.java
blob: 6ffccad44bc64dbeb2071207d103f50da843b051 (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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.base.test.params;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
import org.junit.runner.RunWith;

import org.chromium.base.test.params.ParameterAnnotations.ClassParameter;
import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameter;
import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameterAfter;
import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameterBefore;
import org.chromium.base.test.params.ParameterAnnotations.UseRunnerDelegate;

import java.util.Arrays;
import java.util.List;

/**
 * Example test that uses ParameterizedRunner
 */
@RunWith(ParameterizedRunner.class)
@UseRunnerDelegate(BlockJUnit4RunnerDelegate.class)
public class ExampleParameterizedTest {
    @ClassParameter
    private static List<ParameterSet> sClassParams =
            Arrays.asList(new ParameterSet().value("hello", "world").name("HelloWorld"),
                    new ParameterSet().value("Xxxx", "Yyyy").name("XxxxYyyy"),
                    new ParameterSet().value("aa", "yy").name("AaYy"));

    public static class MethodParamsA implements ParameterProvider {
        private static List<ParameterSet> sMethodParamA =
                Arrays.asList(new ParameterSet().value(1, 2).name("OneTwo"),
                        new ParameterSet().value(2, 3).name("TwoThree"),
                        new ParameterSet().value(3, 4).name("ThreeFour"));

        @Override
        public List<ParameterSet> getParameters() {
            return sMethodParamA;
        }
    }

    public static class MethodParamsB implements ParameterProvider {
        private static List<ParameterSet> sMethodParamB =
                Arrays.asList(new ParameterSet().value("a", "b").name("Ab"),
                        new ParameterSet().value("b", "c").name("Bc"),
                        new ParameterSet().value("c", "d").name("Cd"),
                        new ParameterSet().value("d", "e").name("De"));

        @Override
        public List<ParameterSet> getParameters() {
            return sMethodParamB;
        }
    }

    private String mStringA;
    private String mStringB;

    public ExampleParameterizedTest(String a, String b) {
        mStringA = a;
        mStringB = b;
    }

    @Test
    public void testSimple() {
        Assert.assertEquals(
                "A and B string length aren't equal", mStringA.length(), mStringB.length());
    }

    @Rule
    public MethodRule mMethodParamAnnotationProcessor = new MethodParamAnnotationRule();

    private Integer mSum;

    @UseMethodParameterBefore(MethodParamsA.class)
    public void setupWithOnlyA(int intA, int intB) {
        mSum = intA + intB;
    }

    @Test
    @UseMethodParameter(MethodParamsA.class)
    public void testWithOnlyA(int intA, int intB) {
        Assert.assertEquals(intA + 1, intB);
        Assert.assertEquals(mSum, Integer.valueOf(intA + intB));
        mSum = null;
    }

    private String mConcatenation;

    @Test
    @UseMethodParameter(MethodParamsB.class)
    public void testWithOnlyB(String a, String b) {
        Assert.assertTrue(!a.equals(b));
        mConcatenation = a + b;
    }

    @UseMethodParameterAfter(MethodParamsB.class)
    public void teardownWithOnlyB(String a, String b) {
        Assert.assertEquals(mConcatenation, a + b);
        mConcatenation = null;
    }
}